]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Bring in oslo.cliutils for heat-manage
authorAngus Salkeld <asalkeld@redhat.com>
Mon, 27 May 2013 02:02:52 +0000 (12:02 +1000)
committerAngus Salkeld <asalkeld@redhat.com>
Mon, 27 May 2013 06:06:14 +0000 (16:06 +1000)
part of blueprint heat-manage
Change-Id: I526de1aacbcf930f3187914403031a1e6f9ff4ae

heat/openstack/common/cliutils.py [new file with mode: 0644]
openstack-common.conf

diff --git a/heat/openstack/common/cliutils.py b/heat/openstack/common/cliutils.py
new file mode 100644 (file)
index 0000000..411bd58
--- /dev/null
@@ -0,0 +1,63 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2012 Red Hat, Inc.
+#
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+
+import inspect
+
+
+class MissingArgs(Exception):
+
+    def __init__(self, missing):
+        self.missing = missing
+
+    def __str__(self):
+        if len(self.missing) == 1:
+            return "An argument is missing"
+        else:
+            return ("%(num)d arguments are missing" %
+                    dict(num=len(self.missing)))
+
+
+def validate_args(fn, *args, **kwargs):
+    """Check that the supplied args are sufficient for calling a function.
+
+    >>> validate_args(lambda a: None)
+    Traceback (most recent call last):
+        ...
+    MissingArgs: An argument is missing
+    >>> validate_args(lambda a, b, c, d: None, 0, c=1)
+    Traceback (most recent call last):
+        ...
+    MissingArgs: 2 arguments are missing
+
+    :param fn: the function to check
+    :param arg: the positional arguments supplied
+    :param kwargs: the keyword arguments supplied
+    """
+    argspec = inspect.getargspec(fn)
+
+    num_defaults = len(argspec.defaults or [])
+    required_args = argspec.args[:len(argspec.args) - num_defaults]
+
+    def isbound(method):
+        return getattr(method, 'im_self', None) is not None
+
+    if isbound(fn):
+        required_args.pop(0)
+
+    missing = [arg for arg in required_args if arg not in kwargs]
+    missing = missing[len(args):]
+    if missing:
+        raise MissingArgs(missing)
index c5e551c88638035f74f8df14d16b6bebd84a85ed..2b5cb3053693ff4bd2791dac0f1142d15b709b77 100644 (file)
@@ -4,6 +4,7 @@
 module=eventlet_backdoor
 module=exception
 module=excutils
+module=cliutils
 module=gettextutils
 module=importutils
 module=install_venv_common