<s> from plumbum . lib import six , getdoc <EOL> from plumbum import local <EOL> from abc import abstractmethod <EOL> class SwitchError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class PositionalArgumentsError ( SwitchError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class SwitchCombinationError ( SwitchError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class UnknownSwitch ( SwitchError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class MissingArgument ( SwitchError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class MissingMandatorySwitch ( SwitchError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class WrongArgumentType ( SwitchError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class SubcommandError ( SwitchError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class SwitchInfo ( object ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> for k , v in kwargs . items ( ) : <EOL> setattr ( self , k , v ) <EOL> def switch ( names , argtype = None , argname = None , list = False , mandatory = False , requires = ( ) , <EOL> excludes = ( ) , help = None , overridable = False , group = "<STR_LIT>" , envname = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( names , six . string_types ) : <EOL> names = [ names ] <EOL> names = [ n . lstrip ( "<STR_LIT:->" ) for n in names ] <EOL> requires = [ n . lstrip ( "<STR_LIT:->" ) for n in requires ] <EOL> excludes = [ n . lstrip ( "<STR_LIT:->" ) for n in excludes ] <EOL> def deco ( func ) : <EOL> if argname is None : <EOL> argspec = six . getfullargspec ( func ) . args <EOL> if len ( argspec ) == <NUM_LIT:2> : <EOL> argname2 = argspec [ <NUM_LIT:1> ] <EOL> else : <EOL> argname2 = "<STR_LIT>" <EOL> else : <EOL> argname2 = argname <EOL> help2 = getdoc ( func ) if help is None else help <EOL> if not help2 : <EOL> help2 = str ( func ) <EOL> func . _switch_info = SwitchInfo ( names = names , envname = envname , argtype = argtype , list = list , func = func , <EOL> mandatory = mandatory , overridable = overridable , group = group , <EOL> requires = requires , excludes = excludes , argname = argname2 , help = help2 ) <EOL> return func <EOL> return deco <EOL> def autoswitch ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> def deco ( func ) : <EOL> return switch ( func . __name__ . replace ( "<STR_LIT:_>" , "<STR_LIT:->" ) , * args , ** kwargs ) ( func ) <EOL> return deco <EOL> class SwitchAttr ( object ) : <EOL> """<STR_LIT>""" <EOL> ATTR_NAME = '<STR_LIT>' <EOL> def __init__ ( self , names , argtype = str , default = None , list = False , argname = "<STR_LIT>" , ** kwargs ) : <EOL> self . __doc__ = "<STR_LIT>" <EOL> if "<STR_LIT>" in kwargs and default and argtype is not None : <EOL> kwargs [ "<STR_LIT>" ] += "<STR_LIT>" % ( default , ) <EOL> switch ( names , argtype = argtype , argname = argname , list = list , ** kwargs ) ( self ) <EOL> listtype = type ( [ ] ) <EOL> if list : <EOL> if default is None : <EOL> self . _default_value = [ ] <EOL> elif isinstance ( default , ( tuple , listtype ) ) : <EOL> self . _default_value = listtype ( default ) <EOL> else : <EOL> self . _default_value = [ default ] <EOL> else : <EOL> self . _default_value = default <EOL> def __call__ ( self , inst , val ) : <EOL> self . __set__ ( inst , val ) <EOL> def __get__ ( self , inst , cls ) : <EOL> if inst is None : <EOL> return self <EOL> else : <EOL> return getattr ( inst , self . ATTR_NAME , { } ) . get ( self , self . _default_value ) <EOL> def __set__ ( self , inst , val ) : <EOL> if inst is None : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> else : <EOL> if not hasattr ( inst , self . ATTR_NAME ) : <EOL> setattr ( inst , self . ATTR_NAME , { self : val } ) <EOL> else : <EOL> getattr ( inst , self . ATTR_NAME ) [ self ] = val <EOL> class Flag ( SwitchAttr ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , names , default = False , ** kwargs ) : <EOL> SwitchAttr . __init__ ( self , names , argtype = None , default = default , list = False , ** kwargs ) <EOL> def __call__ ( self , inst ) : <EOL> self . __set__ ( inst , not self . _default_value ) <EOL> class CountOf ( SwitchAttr ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , names , default = <NUM_LIT:0> , ** kwargs ) : <EOL> SwitchAttr . __init__ ( self , names , argtype = None , default = default , list = True , ** kwargs ) <EOL> self . _default_value = default <EOL> def __call__ ( self , inst , v ) : <EOL> self . __set__ ( inst , len ( v ) ) <EOL> class positional ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kargs ) : <EOL> self . args = args <EOL> self . kargs = kargs <EOL> def __call__ ( self , function ) : <EOL> m = six . getfullargspec ( function ) <EOL> args_names = list ( m . args [ <NUM_LIT:1> : ] ) <EOL> positional = [ None ] * len ( args_names ) <EOL> varargs = None <EOL> for i in range ( min ( len ( positional ) , len ( self . args ) ) ) : <EOL> positional [ i ] = self . args [ i ] <EOL> if len ( args_names ) + <NUM_LIT:1> == len ( self . args ) : <EOL> varargs = self . args [ - <NUM_LIT:1> ] <EOL> for item in self . kargs : <EOL> if item == m . varargs : <EOL> varargs = self . kargs [ item ] <EOL> else : <EOL> positional [ args_names . index ( item ) ] = self . kargs [ item ] <EOL> function . positional = positional <EOL> function . positional_varargs = varargs <EOL> return function <EOL> class Validator ( six . ABC ) : <EOL> __slots__ = ( '<STR_LIT>' ) <EOL> @ abstractmethod <EOL> def __call__ ( self , obj ) : <EOL> "<STR_LIT>" <EOL> def choices ( self , partial = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> return set ( ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> slots = { } <EOL> for cls in self . __mro__ : <EOL> for prop in getattr ( cls , "<STR_LIT>" , ( ) ) : <EOL> if prop [ <NUM_LIT:0> ] != '<STR_LIT:_>' : <EOL> slots [ prop ] = getattr ( self , prop ) <EOL> mystrs = ( "<STR_LIT>" . format ( name , slots [ name ] ) for name in slots ) <EOL> return "<STR_LIT>" . format ( self . __class__ . __name__ , "<STR_LIT:U+002CU+0020>" . join ( mystrs ) ) <EOL> class Range ( Validator ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( "<STR_LIT:start>" , "<STR_LIT:end>" ) <EOL> def __init__ ( self , start , end ) : <EOL> self . start = start <EOL> self . end = end <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . start , self . end ) <EOL> def __call__ ( self , obj ) : <EOL> obj = int ( obj ) <EOL> if obj < self . start or obj > self . end : <EOL> raise ValueError ( "<STR_LIT>" % ( self . start , self . end ) ) <EOL> return obj <EOL> def choices ( self , partial = "<STR_LIT>" ) : <EOL> return set ( range ( self . start , self . end + <NUM_LIT:1> ) ) <EOL> class Set ( Validator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * values , ** kwargs ) : <EOL> self . case_sensitive = kwargs . pop ( "<STR_LIT>" , False ) <EOL> if kwargs : <EOL> raise TypeError ( "<STR_LIT>" % ( kwargs . keys ( ) , ) ) <EOL> self . values = dict ( ( ( v if self . case_sensitive else v . lower ( ) ) , v ) for v in values ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( "<STR_LIT:U+002CU+0020>" . join ( repr ( v ) for v in self . values . values ( ) ) ) <EOL> def __call__ ( self , obj ) : <EOL> if not self . case_sensitive : <EOL> obj = obj . lower ( ) <EOL> if obj not in self . values : <EOL> raise ValueError ( "<STR_LIT>" % ( list ( self . values . values ( ) ) , ) ) <EOL> return self . values [ obj ] <EOL> def choices ( self , partial = "<STR_LIT>" ) : <EOL> return set ( self . values ) <EOL> class Predicate ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func ) : <EOL> self . func = func <EOL> def __str__ ( self ) : <EOL> return self . func . __name__ <EOL> def __call__ ( self , val ) : <EOL> return self . func ( val ) <EOL> def choices ( self , partial = "<STR_LIT>" ) : <EOL> return set ( ) <EOL> @ Predicate <EOL> def ExistingDirectory ( val ) : <EOL> """<STR_LIT>""" <EOL> p = local . path ( val ) <EOL> if not p . is_dir ( ) : <EOL> raise ValueError ( "<STR_LIT>" % ( val , ) ) <EOL> return p <EOL> @ Predicate <EOL> def ExistingFile ( val ) : <EOL> """<STR_LIT>""" <EOL> p = local . path ( val ) <EOL> if not p . is_file ( ) : <EOL> raise ValueError ( "<STR_LIT>" % ( val , ) ) <EOL> return p <EOL> @ Predicate <EOL> def NonexistentPath ( val ) : <EOL> """<STR_LIT>""" <EOL> p = local . path ( val ) <EOL> if p . exists ( ) : <EOL> raise ValueError ( "<STR_LIT>" % ( val , ) ) <EOL> return p </s>
