<s> """<STR_LIT>""" <EOL> import json <EOL> try : <EOL> import nbformat as nbf <EOL> from nbformat . v4 . nbbase import validate <EOL> except ImportError : <EOL> import IPython . nbformat as nbf <EOL> from IPython . nbformat . v4 . nbbase import validate <EOL> from . . lib . markdown import MarkdownFilter <EOL> from . . lib . python import PythonFilter <EOL> from . . ext . six import string_types <EOL> from . . utils . utils import _ensure_string <EOL> def _cell_input ( cell ) : <EOL> """<STR_LIT>""" <EOL> return _ensure_string ( cell . get ( '<STR_LIT:source>' , [ ] ) ) <EOL> def _cell_output ( cell ) : <EOL> """<STR_LIT>""" <EOL> outputs = cell . get ( '<STR_LIT>' , [ ] ) <EOL> stdout = ( '<STR_LIT:\n>' . join ( _ensure_string ( output . get ( '<STR_LIT:text>' , '<STR_LIT>' ) ) <EOL> for output in outputs ) ) . rstrip ( ) <EOL> text_outputs = [ ] <EOL> for output in outputs : <EOL> out = output . get ( '<STR_LIT:data>' , { } ) . get ( '<STR_LIT>' , [ ] ) <EOL> out = _ensure_string ( out ) <EOL> if out . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> text_outputs . append ( out ) <EOL> return stdout + '<STR_LIT:\n>' . join ( text_outputs ) . rstrip ( ) <EOL> def _compare_notebook_cells ( cell_0 , cell_1 ) : <EOL> return all ( ( cell_0 [ '<STR_LIT>' ] == cell_1 [ '<STR_LIT>' ] , <EOL> _cell_input ( cell_0 ) == _cell_input ( cell_1 ) , <EOL> _cell_output ( cell_0 ) == _cell_output ( cell_1 ) ) ) <EOL> def _compare_notebooks ( nb_0 , nb_1 ) : <EOL> return all ( _compare_notebook_cells ( cell_0 , cell_1 ) <EOL> for cell_0 , cell_1 in zip ( nb_0 [ '<STR_LIT>' ] , nb_1 [ '<STR_LIT>' ] ) ) <EOL> class NotebookReader ( object ) : <EOL> """<STR_LIT>""" <EOL> ignore_meta = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def __init__ ( self ) : <EOL> self . _notebook_metadata = { } <EOL> def read ( self , nb ) : <EOL> assert nb [ '<STR_LIT>' ] >= <NUM_LIT:4> <EOL> yield { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> "<STR_LIT>" : nb [ '<STR_LIT>' ] <EOL> } <EOL> for cell in nb [ '<STR_LIT>' ] : <EOL> ipymd_cell = { } <EOL> metadata = self . clean_meta ( cell ) <EOL> if metadata : <EOL> ipymd_cell [ '<STR_LIT>' ] = metadata <EOL> ctype = cell [ '<STR_LIT>' ] <EOL> ipymd_cell [ '<STR_LIT>' ] = ctype <EOL> if ctype == '<STR_LIT:code>' : <EOL> ipymd_cell [ '<STR_LIT:input>' ] = _cell_input ( cell ) <EOL> ipymd_cell [ '<STR_LIT>' ] = _cell_output ( cell ) <EOL> elif ctype == '<STR_LIT>' : <EOL> ipymd_cell [ '<STR_LIT:source>' ] = _ensure_string ( cell [ '<STR_LIT:source>' ] ) <EOL> else : <EOL> continue <EOL> yield ipymd_cell <EOL> def clean_meta ( self , cell ) : <EOL> metadata = cell . get ( '<STR_LIT>' , { } ) <EOL> for key in self . ignore_meta : <EOL> metadata . pop ( key , None ) <EOL> return metadata <EOL> class NotebookWriter ( object ) : <EOL> def __init__ ( self , keep_markdown = None , ipymd_skip = False ) : <EOL> self . _nb = nbf . v4 . new_notebook ( ) <EOL> self . _count = <NUM_LIT:1> <EOL> self . _markdown_filter = MarkdownFilter ( keep_markdown ) <EOL> self . _code_filter = PythonFilter ( ipymd_skip = ipymd_skip ) <EOL> def append_markdown ( self , source , metadata = None ) : <EOL> source = self . _markdown_filter ( source ) <EOL> if not source : <EOL> return <EOL> self . _nb [ '<STR_LIT>' ] . append ( <EOL> nbf . v4 . new_markdown_cell ( source , <EOL> metadata = metadata ) ) <EOL> def append_code ( self , input , output = None , image = None , metadata = None ) : <EOL> input = self . _code_filter ( input ) <EOL> cell = nbf . v4 . new_code_cell ( input , <EOL> execution_count = self . _count , <EOL> metadata = metadata ) <EOL> if output : <EOL> cell . outputs . append ( nbf . v4 . new_output ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : output } , <EOL> execution_count = self . _count , <EOL> metadata = { } , <EOL> ) ) <EOL> if image : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> self . _nb [ '<STR_LIT>' ] . append ( cell ) <EOL> self . _count += <NUM_LIT:1> <EOL> def write_notebook_metadata ( self , metadata ) : <EOL> self . _nb . metadata . update ( metadata ) <EOL> def write ( self , cell ) : <EOL> metadata = cell . get ( "<STR_LIT>" , { } ) <EOL> if cell [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> self . append_markdown ( cell [ '<STR_LIT:source>' ] , metadata = metadata ) <EOL> elif cell [ '<STR_LIT>' ] == '<STR_LIT:code>' : <EOL> self . append_code ( cell [ '<STR_LIT:input>' ] , cell [ '<STR_LIT>' ] , metadata = metadata ) <EOL> @ property <EOL> def contents ( self ) : <EOL> validate ( self . _nb ) <EOL> return self . _nb <EOL> NOTEBOOK_FORMAT = dict ( <EOL> reader = NotebookReader , <EOL> writer = NotebookWriter , <EOL> file_extension = '<STR_LIT>' , <EOL> file_type = '<STR_LIT>' , <EOL> ) </s>
