<s> import sys <EOL> import os <EOL> import datetime <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class CommandNotFound ( AttributeError ) : pass <EOL> class ConverterError ( Exception ) : pass <EOL> def manage ( commands , argv = None , delim = '<STR_LIT::>' ) : <EOL> '''<STR_LIT>''' <EOL> def perform_auto_complete ( commands ) : <EOL> from lazy import LazyCli <EOL> cwords = os . environ [ '<STR_LIT>' ] . split ( ) [ <NUM_LIT:1> : ] <EOL> cword = int ( os . environ [ '<STR_LIT>' ] ) <EOL> try : <EOL> curr = cwords [ cword - <NUM_LIT:1> ] <EOL> except IndexError : <EOL> curr = '<STR_LIT>' <EOL> suggest = [ ] <EOL> if len ( cwords ) > <NUM_LIT:1> and cwords [ <NUM_LIT:0> ] in commands . keys ( ) : <EOL> value = commands [ cwords [ <NUM_LIT:0> ] ] <EOL> if isinstance ( value , LazyCli ) : <EOL> value = value . get_digest ( ) <EOL> for cmd_name , _ in value . get_funcs ( ) : <EOL> cmd_name = cmd_name [ <NUM_LIT:8> : ] <EOL> suggest . append ( cmd_name ) <EOL> if curr == "<STR_LIT::>" : <EOL> curr = '<STR_LIT>' <EOL> else : <EOL> suggest += commands . keys ( ) + [ x + "<STR_LIT::>" for x in commands . keys ( ) ] <EOL> suggest . sort ( ) <EOL> output = "<STR_LIT:U+0020>" . join ( filter ( lambda x : x . startswith ( curr ) , suggest ) ) <EOL> sys . stdout . write ( output ) <EOL> auto_complete = '<STR_LIT>' in os . environ or '<STR_LIT>' in os . environ <EOL> if auto_complete : <EOL> perform_auto_complete ( commands ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> argv = sys . argv if argv is None else argv <EOL> if len ( argv ) > <NUM_LIT:1> : <EOL> cmd_name = argv [ <NUM_LIT:1> ] <EOL> raw_args = argv [ <NUM_LIT:2> : ] <EOL> args , kwargs = [ ] , { } <EOL> for item in raw_args : <EOL> if item . startswith ( '<STR_LIT>' ) : <EOL> splited = item [ <NUM_LIT:2> : ] . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> if len ( splited ) == <NUM_LIT:2> : <EOL> k , v = splited <EOL> elif len ( splited ) == <NUM_LIT:1> : <EOL> k , v = splited [ <NUM_LIT:0> ] , True <EOL> kwargs [ k ] = v <EOL> else : <EOL> args . append ( item ) <EOL> if delim in cmd_name : <EOL> digest_name , command = cmd_name . split ( delim ) <EOL> else : <EOL> digest_name = cmd_name <EOL> command = None <EOL> try : <EOL> digest = commands [ digest_name ] <EOL> except KeyError : <EOL> _command_list ( commands ) <EOL> sys . exit ( '<STR_LIT>' . format ( digest_name ) ) <EOL> try : <EOL> if command is None : <EOL> if isinstance ( digest , Cli ) : <EOL> help_ = digest . description ( argv [ <NUM_LIT:0> ] , digest_name ) <EOL> sys . stdout . write ( help_ ) <EOL> sys . exit ( '<STR_LIT>' . format ( digest_name ) ) <EOL> digest ( * args , ** kwargs ) <EOL> else : <EOL> digest ( command , * args , ** kwargs ) <EOL> except CommandNotFound : <EOL> help_ = digest . description ( argv [ <NUM_LIT:0> ] , digest_name ) <EOL> sys . stdout . write ( help_ ) <EOL> sys . exit ( '<STR_LIT>' . format ( digest_name , command ) ) <EOL> else : <EOL> _command_list ( commands ) <EOL> sys . exit ( '<STR_LIT>' ) <EOL> def _command_list ( commands ) : <EOL> sys . stdout . write ( '<STR_LIT>' ) <EOL> for k in commands . keys ( ) : <EOL> sys . stdout . write ( str ( k ) ) <EOL> sys . stdout . write ( '<STR_LIT:\n>' ) <EOL> class Cli ( object ) : <EOL> '<STR_LIT>' <EOL> def _fix_docstring ( self , doc , indent = <NUM_LIT:0> ) : <EOL> doc = doc . lstrip ( '<STR_LIT:\n>' ) . rstrip ( ) <EOL> doc = doc . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . rstrip ( '<STR_LIT::>' ) <EOL> doc = '<STR_LIT:\n>' . join ( '<STR_LIT:\t>' * indent + x . lstrip ( ) <EOL> for x in doc . splitlines ( ) ) <EOL> doc += '<STR_LIT:\n>' <EOL> return doc <EOL> def get_funcs ( self ) : <EOL> return [ ( attr , getattr ( self , attr ) ) <EOL> for attr in dir ( self ) <EOL> if attr . startswith ( '<STR_LIT>' ) ] <EOL> def description ( self , argv0 = '<STR_LIT>' , command = None ) : <EOL> '''<STR_LIT>''' <EOL> command = command or self . __class__ . __name__ . lower ( ) <EOL> import inspect <EOL> _help = '<STR_LIT>' <EOL> _help += '<STR_LIT>' . format ( command ) <EOL> if self . __doc__ : <EOL> _help += self . _fix_docstring ( self . __doc__ ) + '<STR_LIT:\n>' <EOL> else : <EOL> _help += '<STR_LIT>' . format ( command ) <EOL> funcs = self . get_funcs ( ) <EOL> funcs . sort ( key = lambda x : x [ <NUM_LIT:1> ] . func_code . co_firstlineno ) <EOL> for attr , func in funcs : <EOL> func = getattr ( self , attr ) <EOL> comm = attr . replace ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> args = inspect . getargspec ( func ) . args [ <NUM_LIT:1> : ] <EOL> args = ( '<STR_LIT>' + '<STR_LIT>' . join ( args ) + '<STR_LIT:]>' ) if args else '<STR_LIT>' <EOL> _help += "<STR_LIT>" . format ( <EOL> argv0 , command , comm , args ) <EOL> if func . __doc__ : <EOL> _help += self . _fix_docstring ( func . __doc__ , <NUM_LIT:2> ) <EOL> return _help <EOL> def __call__ ( self , command_name , * args , ** kwargs ) : <EOL> if command_name == '<STR_LIT>' : <EOL> sys . stdout . write ( self . description ( ) ) <EOL> elif hasattr ( self , '<STR_LIT>' + command_name ) : <EOL> try : <EOL> getattr ( self , '<STR_LIT>' + command_name ) ( * args , ** kwargs ) <EOL> except ConverterError as e : <EOL> sys . stderr . write ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( command_name ) ) <EOL> sys . exit ( e ) <EOL> else : <EOL> raise CommandNotFound ( ) <EOL> class argument ( object ) : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , arg_id , * validators , ** kwargs ) : <EOL> assert type ( arg_id ) in ( int , str ) , '<STR_LIT>' '<STR_LIT>' . format ( arg_id ) <EOL> self . arg_id = arg_id <EOL> self . validators = validators <EOL> self . required = kwargs . get ( '<STR_LIT>' , True ) <EOL> def __call__ ( self , func ) : <EOL> def wrapper ( * args , ** kwargs ) : <EOL> args = list ( args ) <EOL> if isinstance ( self . arg_id , int ) : <EOL> try : <EOL> arg = args [ self . arg_id ] <EOL> except IndexError : <EOL> raise ConverterError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( len ( args ) , self . arg_id ) ) <EOL> arg = self . convert ( arg ) <EOL> args [ self . arg_id ] = arg <EOL> elif isinstance ( self . arg_id , str ) : <EOL> arg = kwargs . get ( self . arg_id ) <EOL> if arg : <EOL> arg = self . convert ( arg ) <EOL> kwargs [ self . arg_id ] = arg <EOL> if not arg and self . required : <EOL> raise ConverterError ( '<STR_LIT>' . format ( self . arg_id ) ) <EOL> return func ( * args , ** kwargs ) <EOL> return wrapper <EOL> def convert ( self , arg ) : <EOL> for v in self . validators : <EOL> arg = v ( arg ) <EOL> return arg <EOL> @ staticmethod <EOL> def to_int ( value ) : <EOL> try : <EOL> return int ( value ) <EOL> except Exception : <EOL> raise ConverterError ( '<STR_LIT>' . format ( value ) ) <EOL> @ staticmethod <EOL> def to_date ( value ) : <EOL> try : <EOL> return datetime . datetime . strptime ( value , '<STR_LIT>' ) . date ( ) <EOL> except Exception : <EOL> raise ConverterError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( value ) ) </s>
