<s> """<STR_LIT>""" <EOL> import io <EOL> import os <EOL> import os . path as op <EOL> from tornado import web <EOL> try : <EOL> import nbformat <EOL> except ImportError : <EOL> from IPython import nbformat <EOL> try : <EOL> from traitlets import Unicode , Bool <EOL> from traitlets . config import Configurable <EOL> except ImportError : <EOL> from IPython . utils . traitlets import Unicode , Bool <EOL> from IPython . config . configurable import Configurable <EOL> try : <EOL> from notebook . services . contents . filemanager import FileContentsManager <EOL> except ImportError : <EOL> from IPython . html . services . contents . filemanager import FileContentsManager <EOL> from . format_manager import convert , format_manager <EOL> from ipymd . ext . six . moves . urllib . error import HTTPError <EOL> def _file_extension ( os_path ) : <EOL> return op . splitext ( os_path ) [ <NUM_LIT:1> ] <EOL> class IPymdContentsManager ( FileContentsManager , Configurable ) : <EOL> format = Unicode ( '<STR_LIT>' , config = True ) <EOL> default_kernel_name = Unicode ( config = True ) <EOL> verbose_metadata = Bool ( False , config = True ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( IPymdContentsManager , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _fm = format_manager ( ) <EOL> self . _fm . default_kernel_name = self . default_kernel_name <EOL> self . _fm . verbose_metadata = self . verbose_metadata <EOL> def get ( self , path , content = True , type = None , format = None ) : <EOL> """<STR_LIT>""" <EOL> path = path . strip ( '<STR_LIT:/>' ) <EOL> file_extension = format_manager ( ) . file_extension ( self . format ) <EOL> if not self . exists ( path ) : <EOL> raise web . HTTPError ( <NUM_LIT> , u'<STR_LIT>' % path ) <EOL> os_path = self . _get_os_path ( path ) <EOL> if os . path . isdir ( os_path ) : <EOL> if type not in ( None , '<STR_LIT>' ) : <EOL> raise web . HTTPError ( <NUM_LIT> , <EOL> u'<STR_LIT>' % ( path , type ) , reason = '<STR_LIT>' ) <EOL> model = self . _dir_model ( path , content = content ) <EOL> elif type == '<STR_LIT>' or ( type is None and <EOL> ( path . endswith ( '<STR_LIT>' ) or <EOL> path . endswith ( file_extension ) ) ) : <EOL> model = self . _notebook_model ( path , content = content ) <EOL> else : <EOL> if type == '<STR_LIT>' : <EOL> raise web . HTTPError ( <NUM_LIT> , <EOL> u'<STR_LIT>' , reason = '<STR_LIT>' ) <EOL> model = self . _file_model ( path , content = content , format = format ) <EOL> return model <EOL> def _read_notebook ( self , os_path , as_version = <NUM_LIT:4> ) : <EOL> """<STR_LIT>""" <EOL> with self . open ( os_path , '<STR_LIT:r>' , encoding = '<STR_LIT:utf-8>' ) as f : <EOL> try : <EOL> file_ext = _file_extension ( os_path ) <EOL> if file_ext == '<STR_LIT>' : <EOL> return nbformat . read ( f , as_version = as_version ) <EOL> else : <EOL> return convert ( os_path , from_ = self . format , to = '<STR_LIT>' ) <EOL> except Exception as e : <EOL> raise HTTPError ( <EOL> <NUM_LIT> , <EOL> u"<STR_LIT>" % ( os_path , e ) , <EOL> ) <EOL> def save ( self , model , path = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> path = path . strip ( '<STR_LIT:/>' ) <EOL> if '<STR_LIT:type>' not in model : <EOL> raise web . HTTPError ( <NUM_LIT> , u'<STR_LIT>' ) <EOL> if '<STR_LIT:content>' not in model and model [ '<STR_LIT:type>' ] != '<STR_LIT>' : <EOL> raise web . HTTPError ( <NUM_LIT> , u'<STR_LIT>' ) <EOL> self . run_pre_save_hook ( model = model , path = path ) <EOL> os_path = self . _get_os_path ( path ) <EOL> self . log . debug ( "<STR_LIT>" , os_path ) <EOL> try : <EOL> if model [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> file_ext = _file_extension ( os_path ) <EOL> if file_ext == '<STR_LIT>' : <EOL> nb = nbformat . from_dict ( model [ '<STR_LIT:content>' ] ) <EOL> self . check_and_sign ( nb , path ) <EOL> self . _save_notebook ( os_path , nb ) <EOL> else : <EOL> contents = convert ( model [ '<STR_LIT:content>' ] , <EOL> from_ = '<STR_LIT>' , <EOL> to = self . format ) <EOL> if ( format_manager ( ) . file_type ( self . format ) in <EOL> ( '<STR_LIT:text>' , '<STR_LIT>' ) ) : <EOL> self . _save_file ( os_path , contents , '<STR_LIT:text>' ) <EOL> else : <EOL> format_manager ( ) . save ( os_path , contents , <EOL> name = self . format , <EOL> overwrite = True ) <EOL> if not self . checkpoints . list_checkpoints ( path ) : <EOL> self . create_checkpoint ( path ) <EOL> elif model [ '<STR_LIT:type>' ] == '<STR_LIT:file>' : <EOL> self . _save_file ( os_path , model [ '<STR_LIT:content>' ] , model . get ( '<STR_LIT>' ) ) <EOL> elif model [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> self . _save_directory ( os_path , model , path ) <EOL> else : <EOL> raise web . HTTPError ( <NUM_LIT> , "<STR_LIT>" % model [ '<STR_LIT:type>' ] ) <EOL> except web . HTTPError : <EOL> raise <EOL> except Exception as e : <EOL> self . log . error ( u'<STR_LIT>' , path , e , exc_info = True ) <EOL> raise web . HTTPError ( <NUM_LIT> , u'<STR_LIT>' % ( path , e ) ) <EOL> validation_message = None <EOL> if model [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> self . validate_notebook_model ( model ) <EOL> validation_message = model . get ( '<STR_LIT:message>' , None ) <EOL> model = self . get ( path , content = False ) <EOL> if validation_message : <EOL> model [ '<STR_LIT:message>' ] = validation_message <EOL> self . run_post_save_hook ( model = model , os_path = os_path ) <EOL> return model </s>
