<s> import argparse <EOL> import os <EOL> import sys <EOL> from opcdiag . controller import OpcController <EOL> class CommandController ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parser , app_controller ) : <EOL> self . _parser = parser <EOL> self . _app_controller = app_controller <EOL> @ staticmethod <EOL> def new ( ) : <EOL> """<STR_LIT>""" <EOL> parser = Command . parser ( ) <EOL> app_controller = OpcController ( ) <EOL> return CommandController ( parser , app_controller ) <EOL> def execute ( self , argv = None ) : <EOL> """<STR_LIT>""" <EOL> arg_count = len ( argv if argv else sys . argv ) <EOL> if arg_count < <NUM_LIT:2> : <EOL> self . _parser . print_help ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> args = self . _parser . parse_args ( argv ) <EOL> command = args . command <EOL> command . validate ( args ) <EOL> command . execute ( args , self . _app_controller ) <EOL> class Command ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parser ) : <EOL> super ( Command , self ) . __init__ ( ) <EOL> self . _parser = parser <EOL> @ staticmethod <EOL> def parser ( ) : <EOL> """<STR_LIT>""" <EOL> desc = ( <EOL> '<STR_LIT>' <EOL> ) <EOL> epilog = "<STR_LIT>" <EOL> parser = argparse . ArgumentParser ( <EOL> prog = '<STR_LIT>' , description = desc , epilog = epilog <EOL> ) <EOL> subparsers = parser . add_subparsers ( title = '<STR_LIT>' ) <EOL> for command_cls in Command . __subclasses__ ( ) : <EOL> command_parser = command_cls . add_command_parser_to ( subparsers ) <EOL> command = command_cls ( command_parser ) <EOL> command_parser . set_defaults ( command = command ) <EOL> return parser <EOL> def execute ( self , args , app_controller ) : <EOL> """<STR_LIT>""" <EOL> msg = '<STR_LIT>' <EOL> raise NotImplementedError ( msg ) <EOL> def validate ( self , args ) : <EOL> """<STR_LIT>""" <EOL> msg = '<STR_LIT>' <EOL> raise NotImplementedError ( msg ) <EOL> class BrowseCommand ( Command ) : <EOL> def __init__ ( self , parser ) : <EOL> super ( BrowseCommand , self ) . __init__ ( parser ) <EOL> @ staticmethod <EOL> def add_command_parser_to ( subparsers ) : <EOL> parser = subparsers . add_parser ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT:filename>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> return parser <EOL> def execute ( self , args , app_controller ) : <EOL> app_controller . browse ( args . pkg_path , args . filename ) <EOL> def validate ( self , args ) : <EOL> try : <EOL> msg = "<STR_LIT>" % args . pkg_path <EOL> assert os . path . exists ( args . pkg_path ) , msg <EOL> except AssertionError as e : <EOL> self . _parser . error ( str ( e ) ) <EOL> class DiffCommand ( Command ) : <EOL> def __init__ ( self , parser ) : <EOL> super ( DiffCommand , self ) . __init__ ( parser ) <EOL> @ staticmethod <EOL> def add_command_parser_to ( subparsers ) : <EOL> parser = subparsers . add_parser ( <EOL> '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> return parser <EOL> def execute ( self , args , app_controller ) : <EOL> app_controller . diff_pkg ( args . pkg_1_path , args . pkg_2_path ) <EOL> def validate ( self , args ) : <EOL> paths_that_should_exist = ( <EOL> ( args . pkg_1_path , '<STR_LIT>' ) , <EOL> ( args . pkg_2_path , '<STR_LIT>' ) , <EOL> ) <EOL> try : <EOL> for path , metavar in paths_that_should_exist : <EOL> msg = "<STR_LIT>" % ( metavar , path ) <EOL> assert os . path . exists ( path ) , msg <EOL> except AssertionError as e : <EOL> self . _parser . error ( str ( e ) ) <EOL> class DiffItemCommand ( Command ) : <EOL> def __init__ ( self , parser ) : <EOL> super ( DiffItemCommand , self ) . __init__ ( parser ) <EOL> @ staticmethod <EOL> def add_command_parser_to ( subparsers ) : <EOL> parser = subparsers . add_parser ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT:filename>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> return parser <EOL> def execute ( self , args , app_controller ) : <EOL> app_controller . diff_item ( <EOL> args . pkg_1_path , args . pkg_2_path , args . filename ) <EOL> def validate ( self , args ) : <EOL> paths_that_should_exist = ( <EOL> ( args . pkg_1_path , '<STR_LIT>' ) , <EOL> ( args . pkg_2_path , '<STR_LIT>' ) , <EOL> ) <EOL> try : <EOL> for path , metavar in paths_that_should_exist : <EOL> msg = "<STR_LIT>" % ( metavar , path ) <EOL> assert os . path . exists ( path ) , msg <EOL> except AssertionError as e : <EOL> self . _parser . error ( str ( e ) ) <EOL> class ExtractCommand ( Command ) : <EOL> def __init__ ( self , parser ) : <EOL> super ( ExtractCommand , self ) . __init__ ( parser ) <EOL> @ staticmethod <EOL> def add_command_parser_to ( subparsers ) : <EOL> parser = subparsers . add_parser ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> return parser <EOL> def validate ( self , args ) : <EOL> try : <EOL> msg = "<STR_LIT>" % args . pkg_path <EOL> assert os . path . exists ( args . pkg_path ) , msg <EOL> except AssertionError as e : <EOL> self . _parser . error ( str ( e ) ) <EOL> def execute ( self , args , app_controller ) : <EOL> app_controller . extract_package ( args . pkg_path , args . dirpath ) <EOL> class RepackageCommand ( Command ) : <EOL> def __init__ ( self , parser ) : <EOL> super ( RepackageCommand , self ) . __init__ ( parser ) <EOL> @ staticmethod <EOL> def add_command_parser_to ( subparsers ) : <EOL> parser = subparsers . add_parser ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> return parser <EOL> def validate ( self , args ) : <EOL> try : <EOL> msg = "<STR_LIT>" % args . dirpath <EOL> assert os . path . isdir ( args . dirpath ) , msg <EOL> except AssertionError as e : <EOL> self . _parser . error ( str ( e ) ) <EOL> def execute ( self , args , app_controller ) : <EOL> app_controller . repackage ( args . dirpath , args . new_package ) <EOL> class SubstituteCommand ( Command ) : <EOL> def __init__ ( self , parser ) : <EOL> super ( SubstituteCommand , self ) . __init__ ( parser ) <EOL> @ staticmethod <EOL> def add_command_parser_to ( subparsers ) : <EOL> parser = subparsers . add_parser ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT:filename>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> return parser <EOL> def validate ( self , args ) : <EOL> paths_that_should_exist = ( <EOL> ( args . src_pkg_path , '<STR_LIT>' ) , <EOL> ( args . tgt_pkg_path , '<STR_LIT>' ) , <EOL> ) <EOL> try : <EOL> for path , metavar in paths_that_should_exist : <EOL> msg = "<STR_LIT>" % ( metavar , path ) <EOL> assert os . path . exists ( path ) , msg <EOL> except AssertionError as e : <EOL> self . _parser . error ( str ( e ) ) <EOL> def execute ( self , args , app_controller ) : <EOL> app_controller . substitute ( <EOL> args . filename , args . src_pkg_path , args . tgt_pkg_path , <EOL> args . result_pkg_path ) <EOL> def main ( argv = None ) : <EOL> command_controller = CommandController . new ( ) <EOL> command_controller . execute ( argv ) </s>
