<s> """<STR_LIT>""" <EOL> from copy import deepcopy <EOL> import inspect <EOL> import types <EOL> from . vendor import six <EOL> from . context import Context <EOL> from . parser import Argument , translate_underscores <EOL> if six . PY3 : <EOL> from itertools import zip_longest <EOL> else : <EOL> from itertools import izip_longest as zip_longest <EOL> NO_DEFAULT = object ( ) <EOL> class Task ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> body , <EOL> name = None , <EOL> contextualized = False , <EOL> aliases = ( ) , <EOL> positional = None , <EOL> optional = ( ) , <EOL> default = False , <EOL> auto_shortflags = True , <EOL> help = None , <EOL> pre = None , <EOL> post = None , <EOL> autoprint = False , <EOL> ) : <EOL> self . body = body <EOL> self . __doc__ = getattr ( body , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . __name__ = getattr ( body , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . contextualized = contextualized <EOL> self . _name = name <EOL> self . aliases = aliases <EOL> self . is_default = default <EOL> self . positional = self . fill_implicit_positionals ( positional ) <EOL> self . optional = optional <EOL> self . auto_shortflags = auto_shortflags <EOL> self . help = help or { } <EOL> self . pre = pre or [ ] <EOL> self . post = post or [ ] <EOL> self . times_called = <NUM_LIT:0> <EOL> self . autoprint = autoprint <EOL> @ property <EOL> def name ( self ) : <EOL> return self . _name or self . __name__ <EOL> def __str__ ( self ) : <EOL> aliases = "<STR_LIT>" <EOL> if self . aliases : <EOL> aliases = "<STR_LIT>" . format ( '<STR_LIT:U+002CU+0020>' . join ( self . aliases ) ) <EOL> return "<STR_LIT>" . format ( self . name , aliases ) <EOL> def __repr__ ( self ) : <EOL> return str ( self ) <EOL> def __eq__ ( self , other ) : <EOL> if self . name != other . name : <EOL> return False <EOL> if self . body == other . body : <EOL> return True <EOL> else : <EOL> try : <EOL> return ( <EOL> six . get_function_code ( self . body ) == <EOL> six . get_function_code ( other . body ) <EOL> ) <EOL> except AttributeError : <EOL> return False <EOL> def __hash__ ( self ) : <EOL> return hash ( self . name ) + hash ( self . body ) <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> if self . contextualized and not isinstance ( args [ <NUM_LIT:0> ] , Context ) : <EOL> err = "<STR_LIT>" <EOL> raise TypeError ( err . format ( type ( args [ <NUM_LIT:0> ] ) ) ) <EOL> result = self . body ( * args , ** kwargs ) <EOL> self . times_called += <NUM_LIT:1> <EOL> return result <EOL> @ property <EOL> def called ( self ) : <EOL> return self . times_called > <NUM_LIT:0> <EOL> def argspec ( self , body ) : <EOL> """<STR_LIT>""" <EOL> func = body if isinstance ( body , types . FunctionType ) else body . __call__ <EOL> spec = inspect . getargspec ( func ) <EOL> arg_names = spec . args [ : ] <EOL> matched_args = [ reversed ( x ) for x in [ spec . args , spec . defaults or [ ] ] ] <EOL> spec_dict = dict ( zip_longest ( * matched_args , fillvalue = NO_DEFAULT ) ) <EOL> if self . contextualized : <EOL> context_arg = arg_names . pop ( <NUM_LIT:0> ) <EOL> del spec_dict [ context_arg ] <EOL> return arg_names , spec_dict <EOL> def fill_implicit_positionals ( self , positional ) : <EOL> args , spec_dict = self . argspec ( self . body ) <EOL> if positional is None : <EOL> positional = [ ] <EOL> for name in args : <EOL> default = spec_dict [ name ] <EOL> if default is NO_DEFAULT : <EOL> positional . append ( name ) <EOL> return positional <EOL> def arg_opts ( self , name , default , taken_names ) : <EOL> opts = { } <EOL> opts [ '<STR_LIT>' ] = name in self . positional <EOL> opts [ '<STR_LIT>' ] = name in self . optional <EOL> if '<STR_LIT:_>' in name : <EOL> opts [ '<STR_LIT>' ] = name <EOL> name = translate_underscores ( name ) <EOL> names = [ name ] <EOL> if self . auto_shortflags : <EOL> for char in name : <EOL> if not ( char == name or char in taken_names ) : <EOL> names . append ( char ) <EOL> break <EOL> opts [ '<STR_LIT>' ] = names <EOL> if default not in ( None , NO_DEFAULT ) : <EOL> opts [ '<STR_LIT>' ] = type ( default ) <EOL> opts [ '<STR_LIT:default>' ] = default <EOL> if name in self . help : <EOL> opts [ '<STR_LIT>' ] = self . help [ name ] <EOL> return opts <EOL> def get_arguments ( self ) : <EOL> """<STR_LIT>""" <EOL> arg_names , spec_dict = self . argspec ( self . body ) <EOL> tuples = [ ( x , spec_dict [ x ] ) for x in arg_names ] <EOL> taken_names = set ( x [ <NUM_LIT:0> ] for x in tuples ) <EOL> args = [ ] <EOL> for name , default in tuples : <EOL> new_arg = Argument ( ** self . arg_opts ( name , default , taken_names ) ) <EOL> args . append ( new_arg ) <EOL> taken_names . update ( set ( new_arg . names ) ) <EOL> for posarg in reversed ( self . positional ) : <EOL> for i , arg in enumerate ( args ) : <EOL> if arg . name == posarg : <EOL> args . insert ( <NUM_LIT:0> , args . pop ( i ) ) <EOL> break <EOL> return args <EOL> def task ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if len ( args ) == <NUM_LIT:1> and callable ( args [ <NUM_LIT:0> ] ) and not isinstance ( args [ <NUM_LIT:0> ] , Task ) : <EOL> return Task ( args [ <NUM_LIT:0> ] , ** kwargs ) <EOL> if args : <EOL> if '<STR_LIT>' in kwargs : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> kwargs [ '<STR_LIT>' ] = args <EOL> name = kwargs . pop ( '<STR_LIT:name>' , None ) <EOL> contextualized = kwargs . pop ( '<STR_LIT>' , False ) <EOL> aliases = kwargs . pop ( '<STR_LIT>' , ( ) ) <EOL> positional = kwargs . pop ( '<STR_LIT>' , None ) <EOL> optional = tuple ( kwargs . pop ( '<STR_LIT>' , ( ) ) ) <EOL> default = kwargs . pop ( '<STR_LIT:default>' , False ) <EOL> auto_shortflags = kwargs . pop ( '<STR_LIT>' , True ) <EOL> help = kwargs . pop ( '<STR_LIT>' , { } ) <EOL> pre = kwargs . pop ( '<STR_LIT>' , [ ] ) <EOL> post = kwargs . pop ( '<STR_LIT>' , [ ] ) <EOL> autoprint = kwargs . pop ( '<STR_LIT>' , False ) <EOL> if kwargs : <EOL> kwarg = ( "<STR_LIT>" . format ( kwargs ) ) if kwargs else "<STR_LIT>" <EOL> raise TypeError ( "<STR_LIT>" + kwarg ) <EOL> def inner ( obj ) : <EOL> obj = Task ( <EOL> obj , <EOL> name = name , <EOL> contextualized = contextualized , <EOL> aliases = aliases , <EOL> positional = positional , <EOL> optional = optional , <EOL> default = default , <EOL> auto_shortflags = auto_shortflags , <EOL> help = help , <EOL> pre = pre , <EOL> post = post , <EOL> autoprint = autoprint , <EOL> ) <EOL> return obj <EOL> return inner <EOL> def ctask ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> kwargs . setdefault ( '<STR_LIT>' , True ) <EOL> return task ( * args , ** kwargs ) <EOL> class Call ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( <EOL> self , <EOL> task , <EOL> called_as = None , <EOL> args = None , <EOL> kwargs = None , <EOL> context = None , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> self . task = task <EOL> self . called_as = called_as <EOL> self . args = args or tuple ( ) <EOL> self . kwargs = kwargs or dict ( ) <EOL> self . context = context <EOL> def __getattr__ ( self , name ) : <EOL> return getattr ( self . task , name ) <EOL> def __deepcopy__ ( self , memo ) : <EOL> return self . clone ( ) <EOL> def __str__ ( self ) : <EOL> aka = "<STR_LIT>" <EOL> if self . called_as is not None and self . called_as != self . task . name : <EOL> aka = "<STR_LIT>" . format ( self . called_as ) <EOL> return "<STR_LIT>" . format ( <EOL> self . task . name , aka , self . args , self . kwargs <EOL> ) <EOL> def __repr__ ( self ) : <EOL> return str ( self ) <EOL> def __eq__ ( self , other ) : <EOL> for attr in "<STR_LIT>" . split ( ) : <EOL> if getattr ( self , attr ) != getattr ( other , attr ) : <EOL> return False <EOL> return True <EOL> def clone ( self ) : <EOL> """<STR_LIT>""" <EOL> context = None <EOL> if self . context is not None : <EOL> context = Context ( config = self . context . config . clone ( ) ) <EOL> return Call ( <EOL> task = self . task , <EOL> called_as = self . called_as , <EOL> args = deepcopy ( self . args ) , <EOL> kwargs = deepcopy ( self . kwargs ) , <EOL> context = context <EOL> ) <EOL> def call ( task , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return Call ( task = task , args = args , kwargs = kwargs ) </s>
