<s> """<STR_LIT>""" <EOL> import re <EOL> import textwrap <EOL> import argparse <EOL> from . . core import exc , interface , handler <EOL> from . . utils . misc import minimal_logger <EOL> LOG = minimal_logger ( __name__ ) <EOL> def controller_validator ( klass , obj ) : <EOL> """<STR_LIT>""" <EOL> members = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> meta = [ <EOL> '<STR_LIT:label>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> interface . validate ( IController , obj , members , meta = meta ) <EOL> errmsg = "<STR_LIT>" + "<STR_LIT>" <EOL> if obj . _meta . arguments is not None : <EOL> if type ( obj . _meta . arguments ) is not list : <EOL> raise exc . InterfaceError ( errmsg ) <EOL> for item in obj . _meta . arguments : <EOL> if type ( item ) is not tuple : <EOL> raise exc . InterfaceError ( errmsg ) <EOL> if type ( item [ <NUM_LIT:0> ] ) is not list : <EOL> raise exc . InterfaceError ( errmsg ) <EOL> if type ( item [ <NUM_LIT:1> ] ) is not dict : <EOL> raise exc . InterfaceError ( errmsg ) <EOL> if not obj . _meta . label == '<STR_LIT>' and obj . _meta . stacked_on is None : <EOL> errmsg = "<STR_LIT>" % obj . __class__ . __name__ <EOL> raise exc . InterfaceError ( errmsg ) <EOL> if not obj . _meta . label == '<STR_LIT>' and obj . _meta . stacked_type not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise exc . InterfaceError ( <EOL> "<STR_LIT>" % obj . _meta . label + <EOL> "<STR_LIT>" % <EOL> obj . _meta . stacked_type <EOL> ) <EOL> class IController ( interface . Interface ) : <EOL> """<STR_LIT>""" <EOL> class IMeta : <EOL> """<STR_LIT>""" <EOL> label = '<STR_LIT>' <EOL> validator = controller_validator <EOL> Meta = interface . Attribute ( '<STR_LIT>' ) <EOL> def _setup ( app_obj ) : <EOL> """<STR_LIT>""" <EOL> def _dispatch ( self ) : <EOL> """<STR_LIT>""" <EOL> class expose ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , help = '<STR_LIT>' , hide = False , aliases = [ ] , aliases_only = False ) : <EOL> self . hide = hide <EOL> self . help = help <EOL> self . aliases = aliases <EOL> self . aliases_only = aliases_only <EOL> def __call__ ( self , func ) : <EOL> metadict = { } <EOL> metadict [ '<STR_LIT:label>' ] = re . sub ( '<STR_LIT:_>' , '<STR_LIT:->' , func . __name__ ) <EOL> metadict [ '<STR_LIT>' ] = func . __name__ <EOL> metadict [ '<STR_LIT>' ] = True <EOL> metadict [ '<STR_LIT>' ] = self . hide <EOL> metadict [ '<STR_LIT>' ] = self . help <EOL> metadict [ '<STR_LIT>' ] = self . aliases <EOL> metadict [ '<STR_LIT>' ] = self . aliases_only <EOL> metadict [ '<STR_LIT>' ] = None <EOL> func . __cement_meta__ = metadict <EOL> return func <EOL> class CementBaseController ( handler . CementBaseHandler ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> """<STR_LIT>""" <EOL> interface = IController <EOL> """<STR_LIT>""" <EOL> label = None <EOL> """<STR_LIT>""" <EOL> aliases = [ ] <EOL> """<STR_LIT>""" <EOL> aliases_only = False <EOL> """<STR_LIT>""" <EOL> description = None <EOL> """<STR_LIT>""" <EOL> config_section = None <EOL> """<STR_LIT>""" <EOL> config_defaults = { } <EOL> """<STR_LIT>""" <EOL> arguments = [ ] <EOL> """<STR_LIT>""" <EOL> stacked_on = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> stacked_type = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> hide = False <EOL> """<STR_LIT>""" <EOL> epilog = None <EOL> """<STR_LIT>""" <EOL> usage = None <EOL> """<STR_LIT>""" <EOL> argument_formatter = argparse . RawDescriptionHelpFormatter <EOL> """<STR_LIT>""" <EOL> default_func = '<STR_LIT:default>' <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kw ) : <EOL> super ( CementBaseController , self ) . __init__ ( * args , ** kw ) <EOL> self . app = None <EOL> self . _commands = { } <EOL> self . _visible_commands = [ ] <EOL> self . _arguments = [ ] <EOL> self . _dispatch_map = { } <EOL> self . _dispatch_command = None <EOL> def _setup ( self , app_obj ) : <EOL> """<STR_LIT>""" <EOL> super ( CementBaseController , self ) . _setup ( app_obj ) <EOL> if getattr ( self . _meta , '<STR_LIT:description>' , None ) is None : <EOL> self . _meta . description = "<STR_LIT>" % self . _meta . label . capitalize ( ) <EOL> self . app = app_obj <EOL> def _collect ( self ) : <EOL> LOG . debug ( "<STR_LIT>" % self ) <EOL> arguments = [ ] <EOL> commands = [ ] <EOL> arguments = list ( self . _meta . arguments ) <EOL> for member in dir ( self . __class__ ) : <EOL> if member . startswith ( '<STR_LIT:_>' ) : <EOL> continue <EOL> try : <EOL> func = getattr ( self . __class__ , member ) . __cement_meta__ <EOL> except AttributeError : <EOL> continue <EOL> else : <EOL> func [ '<STR_LIT>' ] = self <EOL> commands . append ( func ) <EOL> for contr in handler . list ( '<STR_LIT>' ) : <EOL> if contr == self . __class__ : <EOL> continue <EOL> contr = contr ( ) <EOL> contr . _setup ( self . app ) <EOL> if contr . _meta . stacked_on == self . _meta . label : <EOL> if contr . _meta . stacked_type == '<STR_LIT>' : <EOL> contr_arguments , contr_commands = contr . _collect ( ) <EOL> for arg in contr_arguments : <EOL> arguments . append ( arg ) <EOL> for func in contr_commands : <EOL> commands . append ( func ) <EOL> elif contr . _meta . stacked_type == '<STR_LIT>' : <EOL> metadict = { } <EOL> metadict [ '<STR_LIT:label>' ] = re . sub ( '<STR_LIT:_>' , '<STR_LIT:->' , contr . _meta . label ) <EOL> metadict [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> metadict [ '<STR_LIT>' ] = True <EOL> metadict [ '<STR_LIT>' ] = contr . _meta . hide <EOL> metadict [ '<STR_LIT>' ] = contr . _meta . description <EOL> metadict [ '<STR_LIT>' ] = contr . _meta . aliases <EOL> metadict [ '<STR_LIT>' ] = contr . _meta . aliases_only <EOL> metadict [ '<STR_LIT>' ] = contr <EOL> commands . append ( metadict ) <EOL> return ( arguments , commands ) <EOL> def _process_arguments ( self ) : <EOL> for _arg , _kw in self . _arguments : <EOL> try : <EOL> self . app . args . add_argument ( * _arg , ** _kw ) <EOL> except argparse . ArgumentError as e : <EOL> raise exc . FrameworkError ( e . __str__ ( ) ) <EOL> def _process_commands ( self ) : <EOL> self . _dispatch_map = { } <EOL> self . _visible_commands = [ ] <EOL> for cmd in self . _commands : <EOL> if cmd [ '<STR_LIT:label>' ] in self . _dispatch_map . keys ( ) : <EOL> raise exc . FrameworkError ( <EOL> "<STR_LIT>" % cmd [ '<STR_LIT:label>' ] + <EOL> "<STR_LIT>" % cmd [ '<STR_LIT>' ] <EOL> ) <EOL> self . _dispatch_map [ cmd [ '<STR_LIT:label>' ] ] = cmd <EOL> if not cmd [ '<STR_LIT>' ] : <EOL> self . _visible_commands . append ( cmd [ '<STR_LIT:label>' ] ) <EOL> for alias in cmd [ '<STR_LIT>' ] : <EOL> if alias in self . _dispatch_map . keys ( ) : <EOL> raise exc . FrameworkError ( <EOL> "<STR_LIT>" % alias + <EOL> "<STR_LIT>" % cmd [ '<STR_LIT>' ] + <EOL> "<STR_LIT>" <EOL> ) <EOL> self . _dispatch_map [ alias ] = cmd <EOL> self . _visible_commands . sort ( ) <EOL> def _get_dispatch_command ( self ) : <EOL> default_func = self . _meta . default_func <EOL> default_func_key = re . sub ( '<STR_LIT:_>' , '<STR_LIT:->' , self . _meta . default_func ) <EOL> if ( len ( self . app . argv ) <= <NUM_LIT:0> ) or ( self . app . argv [ <NUM_LIT:0> ] . startswith ( '<STR_LIT:->' ) ) : <EOL> if default_func_key in self . _dispatch_map . keys ( ) : <EOL> self . _dispatch_command = self . _dispatch_map [ default_func_key ] <EOL> elif self . app . argv [ <NUM_LIT:0> ] in self . _dispatch_map . keys ( ) : <EOL> self . _dispatch_command = self . _dispatch_map [ self . app . argv [ <NUM_LIT:0> ] ] <EOL> self . app . argv . pop ( <NUM_LIT:0> ) <EOL> else : <EOL> if default_func_key in self . _dispatch_map . keys ( ) : <EOL> self . _dispatch_command = self . _dispatch_map [ default_func_key ] <EOL> def _parse_args ( self ) : <EOL> self . app . args . description = self . _help_text <EOL> self . app . args . usage = self . _usage_text <EOL> self . app . args . formatter_class = self . _meta . argument_formatter <EOL> self . app . _parse_args ( ) <EOL> def _dispatch ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self . _meta , '<STR_LIT>' ) : <EOL> if self . _meta . epilog is not None : <EOL> self . app . args . epilog = self . _meta . epilog <EOL> self . _arguments , self . _commands = self . _collect ( ) <EOL> self . _process_commands ( ) <EOL> self . _get_dispatch_command ( ) <EOL> if self . _dispatch_command : <EOL> if self . _dispatch_command [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> func = getattr ( self . _dispatch_command [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> return func ( ) <EOL> else : <EOL> self . _process_arguments ( ) <EOL> self . _parse_args ( ) <EOL> func = getattr ( self . _dispatch_command [ '<STR_LIT>' ] , <EOL> self . _dispatch_command [ '<STR_LIT>' ] ) <EOL> return func ( ) <EOL> else : <EOL> self . _process_arguments ( ) <EOL> self . _parse_args ( ) <EOL> @ property <EOL> def _usage_text ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _meta . usage is not None : <EOL> return self . _meta . usage <EOL> txt = "<STR_LIT>" % self . app . args . prog <EOL> return txt <EOL> @ property <EOL> def _help_text ( self ) : <EOL> """<STR_LIT>""" <EOL> cmd_txt = '<STR_LIT>' <EOL> for label in self . _visible_commands : <EOL> cmd = self . _dispatch_map [ label ] <EOL> if len ( cmd [ '<STR_LIT>' ] ) > <NUM_LIT:0> and cmd [ '<STR_LIT>' ] : <EOL> if len ( cmd [ '<STR_LIT>' ] ) > <NUM_LIT:1> : <EOL> first = cmd [ '<STR_LIT>' ] . pop ( <NUM_LIT:0> ) <EOL> cmd_txt = cmd_txt + "<STR_LIT>" % ( first , '<STR_LIT:U+002CU+0020>' . join ( cmd [ '<STR_LIT>' ] ) ) <EOL> else : <EOL> cmd_txt = cmd_txt + "<STR_LIT>" % cmd [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> elif len ( cmd [ '<STR_LIT>' ] ) > <NUM_LIT:0> : <EOL> cmd_txt = cmd_txt + "<STR_LIT>" % ( label , '<STR_LIT:U+002CU+0020>' . join ( cmd [ '<STR_LIT>' ] ) ) <EOL> else : <EOL> cmd_txt = cmd_txt + "<STR_LIT>" % label <EOL> if cmd [ '<STR_LIT>' ] : <EOL> cmd_txt = cmd_txt + "<STR_LIT>" % cmd [ '<STR_LIT>' ] <EOL> else : <EOL> cmd_txt = cmd_txt + "<STR_LIT:\n>" <EOL> if len ( cmd_txt ) > <NUM_LIT:0> : <EOL> txt = '''<STR_LIT>''' % ( self . _meta . description , cmd_txt ) <EOL> else : <EOL> txt = self . _meta . description <EOL> return textwrap . dedent ( txt ) </s>
