<s> """<STR_LIT>""" <EOL> import re <EOL> import cmd <EOL> import sys <EOL> import copy <EOL> import shlex <EOL> import logging <EOL> from Bcfg2 . Compat import StringIO <EOL> from Bcfg2 . Options import PositionalArgument , _debug <EOL> from Bcfg2 . Options . OptionGroups import Subparser <EOL> from Bcfg2 . Options . Parser import Parser , setup as master_setup <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> class Subcommand ( object ) : <EOL> """<STR_LIT>""" <EOL> options = [ ] <EOL> help = None <EOL> interactive = True <EOL> only_interactive = False <EOL> aliases = [ ] <EOL> _ws_re = re . compile ( r'<STR_LIT>' , flags = re . MULTILINE ) <EOL> def __init__ ( self ) : <EOL> self . core = None <EOL> description = "<STR_LIT>" % ( self . __class__ . __name__ . lower ( ) , <EOL> self . __class__ . __doc__ ) <EOL> self . parser = Parser ( <EOL> prog = self . __class__ . __name__ . lower ( ) , <EOL> description = description , <EOL> components = [ self ] , <EOL> add_base_options = False , <EOL> epilog = self . help ) <EOL> self . _usage = None <EOL> self . logger = logging . getLogger ( self . __class__ . __name__ . lower ( ) ) <EOL> def __call__ ( self , args = None ) : <EOL> """<STR_LIT>""" <EOL> if args is not None : <EOL> self . parser . namespace = copy . copy ( master_setup ) <EOL> self . parser . parsed = False <EOL> alist = shlex . split ( args ) <EOL> try : <EOL> setup = self . parser . parse ( alist ) <EOL> except SystemExit : <EOL> return sys . exc_info ( ) [ <NUM_LIT:1> ] . code <EOL> return self . run ( setup ) <EOL> else : <EOL> return self . run ( master_setup ) <EOL> def usage ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _usage is None : <EOL> sio = StringIO ( ) <EOL> self . parser . print_usage ( file = sio ) <EOL> usage = self . _ws_re . sub ( '<STR_LIT:U+0020>' , sio . getvalue ( ) ) . strip ( ) [ <NUM_LIT:7> : ] <EOL> doc = self . _ws_re . sub ( '<STR_LIT:U+0020>' , getattr ( self , "<STR_LIT>" ) or "<STR_LIT>" ) . strip ( ) <EOL> if not doc : <EOL> self . _usage = usage <EOL> else : <EOL> self . _usage = "<STR_LIT>" % ( usage , doc ) <EOL> return self . _usage <EOL> def run ( self , setup ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def shutdown ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Help ( Subcommand ) : <EOL> """<STR_LIT>""" <EOL> options = [ PositionalArgument ( "<STR_LIT>" , nargs = '<STR_LIT:?>' ) ] <EOL> interactive = False <EOL> def __init__ ( self , registry ) : <EOL> Subcommand . __init__ ( self ) <EOL> self . _registry = registry <EOL> def run ( self , setup ) : <EOL> commands = dict ( ( name , cmd ) <EOL> for ( name , cmd ) in self . _registry . commands . items ( ) <EOL> if not cmd . only_interactive ) <EOL> if setup . command : <EOL> try : <EOL> commands [ setup . command ] . parser . print_help ( ) <EOL> return <NUM_LIT:0> <EOL> except KeyError : <EOL> print ( "<STR_LIT>" % setup . command ) <EOL> return <NUM_LIT:1> <EOL> for command in sorted ( commands . keys ( ) ) : <EOL> print ( commands [ command ] . usage ( ) ) <EOL> class CommandRegistry ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . commands = dict ( ) <EOL> self . subcommand_options = [ ] <EOL> self . help = Help ( self ) <EOL> self . register_command ( self . help ) <EOL> def runcommand ( self ) : <EOL> """<STR_LIT>""" <EOL> _debug ( "<STR_LIT>" % master_setup . subcommand ) <EOL> try : <EOL> return self . commands [ master_setup . subcommand ] . run ( master_setup ) <EOL> finally : <EOL> self . shutdown ( ) <EOL> def shutdown ( self ) : <EOL> """<STR_LIT>""" <EOL> _debug ( "<STR_LIT>" % master_setup . subcommand ) <EOL> self . commands [ master_setup . subcommand ] . shutdown ( ) <EOL> def register_command ( self , cls_or_obj ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( cls_or_obj , type ) : <EOL> cmdcls = cls_or_obj <EOL> cmd_obj = cmdcls ( ) <EOL> else : <EOL> cmd_obj = cls_or_obj <EOL> cmdcls = cmd_obj . __class__ <EOL> names = [ cmdcls . __name__ . lower ( ) ] <EOL> if cmdcls . aliases : <EOL> names . extend ( cmdcls . aliases ) <EOL> for name in names : <EOL> self . commands [ name ] = cmd_obj <EOL> if not cmdcls . only_interactive : <EOL> self . subcommand_options . append ( <EOL> Subparser ( * cmdcls . options , ** dict ( name = name , <EOL> help = cmdcls . __doc__ ) ) ) <EOL> if issubclass ( self . __class__ , cmd . Cmd ) and cmdcls . interactive : <EOL> setattr ( self , "<STR_LIT>" % name , cmd_obj ) <EOL> setattr ( self , "<STR_LIT>" % name , cmd_obj . parser . print_help ) <EOL> return cmd_obj <EOL> def register_commands ( self , candidates , parent = Subcommand ) : <EOL> """<STR_LIT>""" <EOL> for attr in candidates : <EOL> if ( isinstance ( attr , type ) and <EOL> issubclass ( attr , parent ) and <EOL> attr != parent and <EOL> not attr . __name__ . startswith ( "<STR_LIT:_>" ) ) : <EOL> self . register_command ( attr ) </s>
