ceph/teuthology/task/args.py
Zack Cerza 622ce1115d Don't redefine variable
(cherry picked from commit 083717c1b4022289806030faf23ff7c4aa371f19)
2013-09-26 14:02:41 -05:00

38 lines
1.2 KiB
Python

def gen_args(name, args):
usage = [""]
usage += [name + ':']
usage += \
[" {key}: <{usage}> ({default})".format(
key=key, usage=_usage, default=default)
for (key, _usage, default, _) in args]
usage.append('')
usage.append(name + ':')
usage += \
[" {key}: {default}".format(
key = key, default = default)
for (key, _, default, _) in args]
usage = '\n'.join(' ' + i for i in usage)
def ret(config):
class Object(object): pass
obj = Object()
for (key, usage, default, conv) in args:
if key in config:
setattr(obj, key, conv(config[key]))
else:
setattr(obj, key, conv(default))
return obj
return usage, ret
def argify(name, args):
(usage, config_func) = gen_args(name, args)
def ret1(f):
def ret2(**kwargs):
config = kwargs.get('config', {})
if config is None:
config = {}
kwargs['config'] = config_func(config)
return f(**kwargs)
ret2.__doc__ = f.__doc__ + usage
return ret2
return ret1