<s> import six <EOL> import logging <EOL> import argparse as _argparse <EOL> from . structs import AppSettings <EOL> from ... modules import IModule <EOL> from ... config import __banner__ <EOL> from ... data import GlobalExecutionParameters <EOL> log = logging . getLogger ( ) <EOL> def setup_cmd ( ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class STBArgumentParser ( _argparse . ArgumentParser ) : <EOL> def parse_args_and_run_hooks ( self , args = None , namespace = None ) : <EOL> parsed_args = super ( self . __class__ , self ) . parse_args ( args , namespace ) <EOL> self . run_hooks ( parsed_args ) <EOL> return parsed_args <EOL> def run_hooks ( self , parsed_args ) : <EOL> """<STR_LIT>""" <EOL> for hook in AppSettings . hooks [ "<STR_LIT>" ] : <EOL> if not hook . __name__ . startswith ( "<STR_LIT>" ) : <EOL> hook ( parsed_args ) <EOL> def build_arg_parser ( config = None , modules = None , parser = None ) : <EOL> """<STR_LIT>""" <EOL> if modules is not None : <EOL> if not isinstance ( modules , dict ) : <EOL> raise TypeError ( "<STR_LIT>" % type ( modules ) ) <EOL> else : <EOL> for x in six . itervalues ( modules ) : <EOL> if not issubclass ( x , IModule ) : <EOL> raise TypeError ( "<STR_LIT>" % type ( x ) ) <EOL> else : <EOL> modules = AppSettings . modules <EOL> if config is None : <EOL> config = GlobalExecutionParameters ( ) <EOL> if parser is None : <EOL> parser = STBArgumentParser ( description = '<STR_LIT:%s>' % __banner__ , <EOL> epilog = _build_examples ( modules ) , <EOL> formatter_class = _argparse . RawTextHelpFormatter ) <EOL> _extract_parameters ( config , parser ) <EOL> main_subparser = parser . add_subparsers ( help = '<STR_LIT>' , dest = '<STR_LIT>' ) <EOL> main_subparser . required = True <EOL> for mod_name , mod_class in six . iteritems ( modules ) : <EOL> mod_parser = main_subparser . add_parser ( mod_class . name , <EOL> help = mod_class . description ) <EOL> sub_modules_enabled = False <EOL> if hasattr ( mod_class , "<STR_LIT>" ) : <EOL> if len ( [ x for x in mod_class . __submodules__ if x != "<STR_LIT:default>" ] ) > <NUM_LIT:0> : <EOL> sub_modules_enabled = True <EOL> sub_module_actions = mod_parser . add_subparsers ( help = "<STR_LIT>" % mod_name , dest = "<STR_LIT>" % mod_name ) <EOL> sub_module_actions . required = True <EOL> for x , y in six . iteritems ( mod_class . __submodules__ ) : <EOL> if x == "<STR_LIT:default>" : <EOL> continue <EOL> sub_help = y . get ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> sub_action = y . get ( '<STR_LIT>' , None ) <EOL> sub_sub_parser = sub_module_actions . add_parser ( x , help = sub_help ) <EOL> if hasattr ( mod_class , "<STR_LIT>" ) : <EOL> _extract_parameters ( mod_class . __model__ ( ) , sub_sub_parser , mod_name ) <EOL> if sub_action is not None : <EOL> sub_action ( sub_sub_parser ) <EOL> if sub_modules_enabled is False : <EOL> if hasattr ( mod_class , "<STR_LIT>" ) : <EOL> _extract_parameters ( mod_class . __model__ ( ) , mod_parser , mod_name ) <EOL> return parser <EOL> def _extract_parameters ( config , parser , prefix = None ) : <EOL> """<STR_LIT>""" <EOL> used_opts = set ( ) <EOL> for x , v in six . iteritems ( config . vars ) : <EOL> params = [ "<STR_LIT>" % ( <EOL> "<STR_LIT>" % prefix if prefix is not None and AppSettings . parallel_running is True else "<STR_LIT>" , <EOL> x . replace ( "<STR_LIT:_>" , "<STR_LIT:->" ) ) <EOL> ] <EOL> if x [ <NUM_LIT:0> ] not in used_opts : <EOL> used_opts . add ( x [ <NUM_LIT:0> ] ) <EOL> if AppSettings . parallel_running is False or prefix is None : <EOL> params . append ( "<STR_LIT>" % x [ <NUM_LIT:0> ] ) <EOL> action , type_ = _resolve_action ( v ) <EOL> named_params = dict ( <EOL> dest = x , <EOL> help = str ( v . label ) , <EOL> default = v . default , <EOL> action = action , <EOL> required = v . required <EOL> ) <EOL> if type_ is not None : <EOL> named_params [ "<STR_LIT:type>" ] = type_ <EOL> if hasattr ( v , "<STR_LIT>" ) : <EOL> named_params [ "<STR_LIT>" ] = dict ( v . choices ) . keys ( ) <EOL> parser . add_argument ( * params , ** named_params ) <EOL> def _build_examples ( modules ) : <EOL> """<STR_LIT>""" <EOL> examples = "<STR_LIT>" . join ( [ "<STR_LIT>" % ( AppSettings . tool_name , x ) for x in list ( modules . keys ( ) ) ] ) <EOL> return '''<STR_LIT>''' % dict ( tool_name = str ( AppSettings . tool_name ) , <EOL> examples = examples ) <EOL> def _resolve_action ( field ) : <EOL> """<STR_LIT>""" <EOL> type_maps = { <EOL> '<STR_LIT>' : ( "<STR_LIT:store>" , float ) , <EOL> '<STR_LIT>' : ( "<STR_LIT:store>" , str ) , <EOL> '<STR_LIT>' : ( "<STR_LIT:store>" , str ) , <EOL> '<STR_LIT>' : ( "<STR_LIT:store>" , int ) , <EOL> '<STR_LIT>' : ( "<STR_LIT:store_true>" , None ) , <EOL> '<STR_LIT>' : ( "<STR_LIT:count>" , None ) <EOL> } <EOL> in_type = field . __class__ . __name__ <EOL> results = type_maps [ in_type ] <EOL> return results </s>
