]> git-server-git.apps.pok.os.sepia.ceph.com Git - teuthology.git/commitdiff
task/: add args.py
authorSamuel Just <sam.just@inktank.com>
Tue, 4 Jun 2013 21:11:29 +0000 (14:11 -0700)
committerSamuel Just <sam.just@inktank.com>
Wed, 19 Jun 2013 22:16:28 +0000 (15:16 -0700)
The usage doc string for a task is tedious to write and
hard to keep reconciled with the code as defaults are changed.
args.py includes a helper to put it all in one place.

Signed-off-by: Samuel Just <sam.just@inktank.com>
teuthology/task/args.py [new file with mode: 0644]

diff --git a/teuthology/task/args.py b/teuthology/task/args.py
new file mode 100644 (file)
index 0000000..fe88b06
--- /dev/null
@@ -0,0 +1,37 @@
+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