<s> '''<STR_LIT>''' <EOL> from __future__ import print_function <EOL> import argparse <EOL> from . merge import notebook_merge <EOL> from . notebook_parser import NotebookParser <EOL> import sys <EOL> from . notebook_diff import notebook_diff <EOL> from . adapter . git_adapter import GitAdapter <EOL> from . adapter . hg_adapter import HgAdapter <EOL> from . adapter . vcs_adapter import NoVCSError <EOL> from . server . local_server import app <EOL> import threading <EOL> import webbrowser <EOL> import IPython . nbformat . current as nbformat <EOL> import IPython . nbformat . reader <EOL> try : <EOL> NotJSONError = IPython . nbformat . current . NotJSONError <EOL> except AttributeError : <EOL> NotJSONError = IPython . nbformat . reader . NotJSONError <EOL> def diff ( ) : <EOL> description = '''<STR_LIT>''' <EOL> usage = '<STR_LIT>' + '<STR_LIT>' <EOL> parser = argparse . ArgumentParser ( <EOL> description = description , <EOL> usage = usage , <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> default = None , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:-c>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> default = False , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> default = False , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> parser . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:?>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:?>' , <EOL> help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> parser = NotebookParser ( ) <EOL> if args . before and args . after : <EOL> invalid_notebooks = [ ] <EOL> try : <EOL> notebook1 = parser . parse ( open ( args . before ) ) <EOL> except NotJSONError : <EOL> invalid_notebooks . append ( args . before ) <EOL> try : <EOL> notebook2 = parser . parse ( open ( args . after ) ) <EOL> except NotJSONError : <EOL> invalid_notebooks . append ( args . after ) <EOL> if ( len ( invalid_notebooks ) == <NUM_LIT:0> ) : <EOL> result = notebook_diff ( notebook1 , notebook2 ) <EOL> filename_placeholder = "<STR_LIT>" . format ( args . before , args . after ) <EOL> app . add_notebook ( result , filename_placeholder ) <EOL> if not args . check : <EOL> open_browser ( args . browser ) <EOL> app . run ( debug = args . debug ) <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' <EOL> + '<STR_LIT>' + '<STR_LIT:\n>' . join ( invalid_notebooks ) ) <EOL> return - <NUM_LIT:1> <EOL> elif not ( args . before or args . after ) : <EOL> try : <EOL> vcs = HgAdapter ( ) <EOL> except NoVCSError as hg_err : <EOL> try : <EOL> vcs = GitAdapter ( ) <EOL> except NoVCSError : <EOL> print ( hg_err . value ) <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> modified_notebooks = vcs . get_modified_notebooks ( ) <EOL> if not len ( modified_notebooks ) == <NUM_LIT:0> : <EOL> invalid_notebooks = [ ] <EOL> for nbook in modified_notebooks : <EOL> try : <EOL> filename = nbook [ <NUM_LIT:2> ] <EOL> current_notebook = parser . parse ( nbook [ <NUM_LIT:0> ] ) <EOL> head_version = parser . parse ( nbook [ <NUM_LIT:1> ] ) <EOL> result = notebook_diff ( head_version , current_notebook ) <EOL> app . add_notebook ( result , filename ) <EOL> except NotJSONError : <EOL> invalid_notebooks . append ( filename ) <EOL> if ( len ( invalid_notebooks ) > <NUM_LIT:0> ) : <EOL> print ( '<STR_LIT>' <EOL> + '<STR_LIT>' + '<STR_LIT:\n>' . join ( invalid_notebooks ) ) <EOL> if ( len ( modified_notebooks ) == len ( invalid_notebooks ) ) : <EOL> print ( "<STR_LIT>" ) <EOL> return - <NUM_LIT:1> <EOL> if not args . check : <EOL> open_browser ( args . browser ) <EOL> app . run ( debug = False ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> return <NUM_LIT:0> <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> return - <NUM_LIT:1> <EOL> def merge ( ) : <EOL> description = '''<STR_LIT>''' <EOL> usage = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> parser = argparse . ArgumentParser ( <EOL> description = description , <EOL> usage = usage , <EOL> ) <EOL> parser . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:*>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:-c>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> default = False , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> default = False , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> default = None , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> args = parser . parse_args ( ) <EOL> length = len ( args . notebook ) <EOL> parser = NotebookParser ( ) <EOL> valid_notebooks = False <EOL> if length == <NUM_LIT:0> : <EOL> try : <EOL> vcs = HgAdapter ( ) <EOL> except NoVCSError as hg_err : <EOL> try : <EOL> vcs = GitAdapter ( ) <EOL> except NoVCSError : <EOL> print ( hg_err . value ) <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> unmerged_notebooks = vcs . get_unmerged_notebooks ( ) <EOL> if not len ( unmerged_notebooks ) == <NUM_LIT:0> : <EOL> invalid_notebooks = [ ] <EOL> for nbook in unmerged_notebooks : <EOL> try : <EOL> filename = nbook [ <NUM_LIT:3> ] <EOL> nb_local = parser . parse ( nbook [ <NUM_LIT:0> ] ) <EOL> nb_base = parser . parse ( nbook [ <NUM_LIT:1> ] ) <EOL> nb_remote = parser . parse ( nbook [ <NUM_LIT:2> ] ) <EOL> pre_merged_notebook = notebook_merge ( nb_local , <EOL> nb_base , nb_remote ) <EOL> app . add_notebook ( pre_merged_notebook , filename ) <EOL> except NotJSONError : <EOL> invalid_notebooks . append ( filename ) <EOL> if ( len ( invalid_notebooks ) > <NUM_LIT:0> ) : <EOL> print ( '<STR_LIT>' <EOL> + '<STR_LIT>' + '<STR_LIT:\n>' . join ( invalid_notebooks ) ) <EOL> if ( len ( unmerged_notebooks ) == len ( invalid_notebooks ) ) : <EOL> print ( "<STR_LIT>" ) <EOL> return - <NUM_LIT:1> <EOL> else : <EOL> valid_notebooks = True <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> return - <NUM_LIT:1> <EOL> elif length == <NUM_LIT:3> or length == <NUM_LIT:4> : <EOL> invalid_notebooks = [ ] <EOL> if length == <NUM_LIT:3> : <EOL> filename = args . notebook [ <NUM_LIT:0> ] <EOL> elif length == <NUM_LIT:4> : <EOL> filename = args . notebook [ <NUM_LIT:3> ] <EOL> try : <EOL> nb_local = parser . parse ( open ( args . notebook [ <NUM_LIT:0> ] ) ) <EOL> except NotJSONError : <EOL> invalid_notebooks . append ( args . notebook [ <NUM_LIT:0> ] ) <EOL> try : <EOL> nb_base = parser . parse ( open ( args . notebook [ <NUM_LIT:1> ] ) ) <EOL> except NotJSONError : <EOL> invalid_notebooks . append ( args . notebook [ <NUM_LIT:1> ] ) <EOL> try : <EOL> nb_remote = parser . parse ( open ( args . notebook [ <NUM_LIT:2> ] ) ) <EOL> except NotJSONError : <EOL> invalid_notebooks . append ( args . notebook [ <NUM_LIT:2> ] ) <EOL> if ( len ( invalid_notebooks ) == <NUM_LIT:0> ) : <EOL> pre_merged_notebook = notebook_merge ( nb_local , nb_base , nb_remote ) <EOL> app . add_notebook ( pre_merged_notebook , filename ) <EOL> valid_notebooks = True <EOL> elif ( len ( invalid_notebooks ) > <NUM_LIT:0> ) : <EOL> print ( '<STR_LIT>' <EOL> + '<STR_LIT>' + '<STR_LIT:\n>' . join ( invalid_notebooks ) ) <EOL> print ( "<STR_LIT>" ) <EOL> return - <NUM_LIT:1> <EOL> else : <EOL> sys . stderr . write ( '<STR_LIT>' ) <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> def save_notebook ( notebook_result , filename ) : <EOL> parsed = nbformat . reads ( notebook_result , '<STR_LIT>' ) <EOL> with open ( filename , '<STR_LIT:w>' ) as targetfile : <EOL> nbformat . write ( parsed , targetfile , '<STR_LIT>' ) <EOL> if ( valid_notebooks ) : <EOL> if not args . check : <EOL> app . shutdown_callback ( save_notebook ) <EOL> open_browser ( args . browser ) <EOL> app . run ( debug = args . debug ) <EOL> def open_browser ( browser_exe ) : <EOL> try : <EOL> browser = webbrowser . get ( browser_exe ) <EOL> except webbrowser . Error : <EOL> browser = None <EOL> if browser : <EOL> b = lambda : browser . open ( "<STR_LIT>" , new = <NUM_LIT:2> ) <EOL> threading . Thread ( target = b ) . start ( ) </s>
