<s> __credits__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> import os <EOL> from copy import copy <EOL> from glob import glob <EOL> from os . path import abspath , exists , isdir , isfile , split <EOL> from optparse import ( Option , OptionParser , OptionGroup , OptionValueError , <EOL> OptionError ) <EOL> from pyqi . core . interface import ( Interface , InterfaceInputOption , <EOL> InterfaceOutputOption , InterfaceUsageExample ) <EOL> from pyqi . core . factory import general_factory <EOL> from pyqi . core . exception import IncompetentDeveloperError <EOL> from pyqi . core . command import Parameter <EOL> class OptparseResult ( InterfaceOutputOption ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( OptparseResult , self ) . __init__ ( ** kwargs ) <EOL> def _validate_option ( self ) : <EOL> pass <EOL> class OptparseOption ( InterfaceInputOption ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( OptparseOption , self ) . __init__ ( ** kwargs ) <EOL> def _validate_option ( self ) : <EOL> pass <EOL> def __str__ ( self ) : <EOL> if self . ShortName is None : <EOL> return '<STR_LIT>' % self . Name <EOL> else : <EOL> return '<STR_LIT>' % ( self . ShortName , self . Name ) <EOL> def getOptparseOption ( self ) : <EOL> if self . Required : <EOL> help_text = self . Help <EOL> if not help_text . strip ( ) . endswith ( '<STR_LIT>' ) : <EOL> help_text += '<STR_LIT>' <EOL> if self . ShortName is None : <EOL> option = PyqiOption ( '<STR_LIT>' + self . Name , type = self . Type , <EOL> action = self . Action , help = help_text ) <EOL> else : <EOL> option = PyqiOption ( '<STR_LIT:->' + self . ShortName , <EOL> '<STR_LIT>' + self . Name , type = self . Type , <EOL> action = self . Action , help = help_text ) <EOL> else : <EOL> if self . DefaultDescription is None : <EOL> help_text = '<STR_LIT>' % self . Help <EOL> else : <EOL> help_text = '<STR_LIT>' % ( self . Help , <EOL> self . DefaultDescription ) <EOL> if self . ShortName is None : <EOL> option = PyqiOption ( '<STR_LIT>' + self . Name , type = self . Type , <EOL> action = self . Action , help = help_text , <EOL> default = self . Default ) <EOL> else : <EOL> option = PyqiOption ( '<STR_LIT:->' + self . ShortName , <EOL> '<STR_LIT>' + self . Name , type = self . Type , <EOL> action = self . Action , help = help_text , <EOL> default = self . Default ) <EOL> return option <EOL> class OptparseUsageExample ( InterfaceUsageExample ) : <EOL> """<STR_LIT>""" <EOL> def _validate_usage_example ( self ) : <EOL> if self . ShortDesc is None : <EOL> raise IncompetentDeveloperError ( "<STR_LIT>" ) <EOL> if self . LongDesc is None : <EOL> raise IncompetentDeveloperError ( "<STR_LIT>" ) <EOL> if self . Ex is None : <EOL> raise IncompetentDeveloperError ( "<STR_LIT>" ) <EOL> class OptparseInterface ( Interface ) : <EOL> """<STR_LIT>""" <EOL> DisallowPositionalArguments = True <EOL> HelpOnNoArguments = True <EOL> OptionalInputLine = '<STR_LIT>' <EOL> RequiredInputLine = '<STR_LIT>' <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( OptparseInterface , self ) . __init__ ( ** kwargs ) <EOL> def _validate_usage_examples ( self , usage_examples ) : <EOL> super ( OptparseInterface , self ) . _validate_usage_examples ( usage_examples ) <EOL> if len ( usage_examples ) < <NUM_LIT:1> : <EOL> raise IncompetentDeveloperError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def _the_in_validator ( self , in_ ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( in_ , list ) : <EOL> raise IncompetentDeveloperError ( "<STR_LIT>" <EOL> "<STR_LIT>" % in_ ) <EOL> def _the_out_validator ( self , out_ ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( out_ , dict ) : <EOL> raise IncompetentDeveloperError ( "<STR_LIT>" <EOL> "<STR_LIT>" % out_ ) <EOL> def _input_handler ( self , in_ , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> required_opts = [ opt for opt in self . _get_inputs ( ) if opt . Required ] <EOL> optional_opts = [ opt for opt in self . _get_inputs ( ) if not opt . Required ] <EOL> usage = self . _build_usage_lines ( required_opts ) <EOL> version = '<STR_LIT>' + self . _get_version ( ) <EOL> parser = OptionParser ( usage = usage , version = version ) <EOL> if len ( required_opts ) > <NUM_LIT:0> and self . HelpOnNoArguments and len ( in_ ) == <NUM_LIT:0> : <EOL> parser . print_usage ( ) <EOL> return parser . exit ( - <NUM_LIT:1> ) <EOL> if required_opts : <EOL> required = OptionGroup ( parser , "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> for ro in required_opts : <EOL> required . add_option ( ro . getOptparseOption ( ) ) <EOL> parser . add_option_group ( required ) <EOL> for oo in optional_opts : <EOL> parser . add_option ( oo . getOptparseOption ( ) ) <EOL> opts , args = parser . parse_args ( in_ ) <EOL> if self . DisallowPositionalArguments and len ( args ) != <NUM_LIT:0> : <EOL> parser . error ( "<STR_LIT>" % str ( args [ <NUM_LIT:0> ] ) + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> if required_opts : <EOL> required_option_ids = [ ( o . dest , o . get_opt_string ( ) ) <EOL> for o in required . option_list ] <EOL> for required_dest , required_name in required_option_ids : <EOL> if getattr ( opts , required_dest ) is None : <EOL> parser . error ( '<STR_LIT>' % required_name ) <EOL> self . _optparse_input = opts . __dict__ <EOL> cmd_input_kwargs = { } <EOL> for option in self . _get_inputs ( ) : <EOL> if option . Parameter is not None : <EOL> param_name = option . getParameterName ( ) <EOL> optparse_clean_name = self . _get_optparse_clean_name ( option . Name ) <EOL> if option . Handler is None : <EOL> value = self . _optparse_input [ optparse_clean_name ] <EOL> else : <EOL> value = option . Handler ( <EOL> self . _optparse_input [ optparse_clean_name ] ) <EOL> cmd_input_kwargs [ param_name ] = value <EOL> return cmd_input_kwargs <EOL> def _build_usage_lines ( self , required_options ) : <EOL> """<STR_LIT>""" <EOL> line1 = '<STR_LIT>' + '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' % ( str ( rp ) , rp . Name . upper ( ) ) <EOL> for rp in required_options ] ) <EOL> formatted_usage_examples = [ ] <EOL> for usage_example in self . _get_usage_examples ( ) : <EOL> short_description = usage_example . ShortDesc . strip ( '<STR_LIT::>' ) . strip ( ) <EOL> long_description = usage_example . LongDesc . strip ( '<STR_LIT::>' ) . strip ( ) <EOL> example = usage_example . Ex . strip ( ) <EOL> if short_description : <EOL> formatted_usage_examples . append ( '<STR_LIT>' % <EOL> ( short_description , <EOL> long_description , example ) ) <EOL> else : <EOL> formatted_usage_examples . append ( '<STR_LIT>' % <EOL> ( long_description , example ) ) <EOL> formatted_usage_examples = '<STR_LIT>' . join ( formatted_usage_examples ) <EOL> lines = ( line1 , <EOL> '<STR_LIT>' , <EOL> self . OptionalInputLine , <EOL> self . RequiredInputLine , <EOL> '<STR_LIT>' , <EOL> self . CmdInstance . LongDescription , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> formatted_usage_examples ) <EOL> return '<STR_LIT:\n>' . join ( lines ) <EOL> def _output_handler ( self , results ) : <EOL> """<STR_LIT>""" <EOL> handled_results = { } <EOL> for output in self . _get_outputs ( ) : <EOL> rk = output . Name <EOL> if output . InputName is None : <EOL> handled_results [ rk ] = output . Handler ( rk , results [ rk ] ) <EOL> else : <EOL> optparse_clean_name = self . _get_optparse_clean_name ( output . InputName ) <EOL> opt_value = self . _optparse_input [ optparse_clean_name ] <EOL> handled_results [ rk ] = output . Handler ( rk , results [ rk ] , <EOL> opt_value ) <EOL> return handled_results <EOL> def _get_optparse_clean_name ( self , name ) : <EOL> return name . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> def optparse_factory ( command_constructor , usage_examples , inputs , outputs , <EOL> version ) : <EOL> """<STR_LIT>""" <EOL> return general_factory ( command_constructor , usage_examples , inputs , <EOL> outputs , version , OptparseInterface ) <EOL> def optparse_main ( interface_object , local_argv ) : <EOL> """<STR_LIT>""" <EOL> optparse_cmd = interface_object ( ) <EOL> result = optparse_cmd ( local_argv [ <NUM_LIT:1> : ] ) <EOL> return <NUM_LIT:0> <EOL> def check_existing_filepath ( option , opt , value ) : <EOL> if not exists ( value ) : <EOL> raise OptionValueError ( <EOL> "<STR_LIT>" % ( opt , value ) ) <EOL> elif not isfile ( value ) : <EOL> raise OptionValueError ( <EOL> "<STR_LIT>" % ( opt , value ) ) <EOL> else : <EOL> return value <EOL> def check_existing_filepaths ( option , opt , value ) : <EOL> paths = [ ] <EOL> for v in value . split ( '<STR_LIT:U+002C>' ) : <EOL> fps = glob ( v ) <EOL> if len ( fps ) == <NUM_LIT:0> : <EOL> raise OptionValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % v ) <EOL> else : <EOL> paths . extend ( fps ) <EOL> values = [ ] <EOL> for v in paths : <EOL> check_existing_filepath ( option , opt , v ) <EOL> values . append ( v ) <EOL> return values <EOL> def check_existing_dirpath ( option , opt , value ) : <EOL> if not exists ( value ) : <EOL> raise OptionValueError ( <EOL> "<STR_LIT>" % ( opt , value ) ) <EOL> elif not isdir ( value ) : <EOL> raise OptionValueError ( <EOL> "<STR_LIT>" % ( opt , value ) ) <EOL> else : <EOL> return value <EOL> def check_existing_dirpaths ( option , opt , value ) : <EOL> paths = [ ] <EOL> for v in value . split ( '<STR_LIT:U+002C>' ) : <EOL> dps = glob ( v ) <EOL> if len ( dps ) == <NUM_LIT:0> : <EOL> raise OptionValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % v ) <EOL> else : <EOL> paths . extend ( dps ) <EOL> values = [ ] <EOL> for v in paths : <EOL> check_existing_dirpath ( option , opt , v ) <EOL> values . append ( v ) <EOL> return values <EOL> def check_new_filepath ( option , opt , value ) : <EOL> if exists ( value ) : <EOL> if isdir ( value ) : <EOL> raise OptionValueError ( <EOL> "<STR_LIT>" % ( opt , <EOL> value ) ) <EOL> return value <EOL> def check_new_dirpath ( option , opt , value ) : <EOL> if exists ( value ) : <EOL> if isfile ( value ) : <EOL> raise OptionValueError ( <EOL> "<STR_LIT>" % ( opt , <EOL> value ) ) <EOL> return value <EOL> def check_existing_path ( option , opt , value ) : <EOL> if not exists ( value ) : <EOL> raise OptionValueError ( <EOL> "<STR_LIT>" % ( opt , value ) ) <EOL> return value <EOL> def check_new_path ( option , opt , value ) : <EOL> return value <EOL> def check_multiple_choice ( option , opt , value ) : <EOL> values = value . split ( option . split_char ) <EOL> for v in values : <EOL> if v not in option . mchoices : <EOL> choices = "<STR_LIT:U+002C>" . join ( map ( repr , option . mchoices ) ) <EOL> raise OptionValueError ( <EOL> "<STR_LIT>" <EOL> % ( opt , v , choices ) ) <EOL> return values <EOL> def check_blast_db ( option , opt , value ) : <EOL> db_dir , db_name = split ( abspath ( value ) ) <EOL> if not exists ( db_dir ) : <EOL> raise OptionValueError ( <EOL> "<STR_LIT>" % ( opt , db_dir ) ) <EOL> elif not isdir ( db_dir ) : <EOL> raise OptionValueError ( <EOL> "<STR_LIT>" % ( opt , db_dir ) ) <EOL> return value <EOL> class PyqiOption ( Option ) : <EOL> ATTRS = Option . ATTRS + [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> TYPES = Option . TYPES + ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> TYPE_CHECKER = copy ( Option . TYPE_CHECKER ) <EOL> TYPE_CHECKER [ "<STR_LIT>" ] = check_existing_path <EOL> TYPE_CHECKER [ "<STR_LIT>" ] = check_new_path <EOL> TYPE_CHECKER [ "<STR_LIT>" ] = check_existing_filepath <EOL> TYPE_CHECKER [ "<STR_LIT>" ] = check_existing_filepaths <EOL> TYPE_CHECKER [ "<STR_LIT>" ] = check_new_filepath <EOL> TYPE_CHECKER [ "<STR_LIT>" ] = check_existing_dirpath <EOL> TYPE_CHECKER [ "<STR_LIT>" ] = check_existing_dirpaths <EOL> TYPE_CHECKER [ "<STR_LIT>" ] = check_new_dirpath <EOL> TYPE_CHECKER [ "<STR_LIT>" ] = check_multiple_choice <EOL> TYPE_CHECKER [ "<STR_LIT>" ] = check_blast_db <EOL> def _check_multiple_choice ( self ) : <EOL> if self . type == "<STR_LIT>" : <EOL> if self . mchoices is None : <EOL> raise OptionError ( <EOL> "<STR_LIT>" % self . type , self ) <EOL> elif type ( self . mchoices ) not in ( tuple , list ) : <EOL> raise OptionError ( <EOL> "<STR_LIT>" <EOL> % str ( type ( self . mchoices ) ) . split ( "<STR_LIT:'>" ) [ <NUM_LIT:1> ] , self ) <EOL> if self . split_char is None : <EOL> self . split_char = '<STR_LIT:U+002C>' <EOL> elif self . mchoices is not None : <EOL> raise OptionError ( <EOL> "<STR_LIT>" % self . type , self ) <EOL> CHECK_METHODS = Option . CHECK_METHODS + [ _check_multiple_choice ] </s>
