diff options
| author | Jenkins <jenkins@review.openstack.org> | 2012-12-06 13:48:21 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2012-12-06 13:48:21 +0000 |
| commit | ed0b536e62dfc012e9c4bdc353efbda7d446b409 (patch) | |
| tree | 4038bc7fcf9f826d3f5d9a3ce90e9651182a2f9a | |
| parent | a7107f97d7b82f0820f1c3b7421a6142b6ff1b4f (diff) | |
| parent | eb39f01402117871f125023be0985ff90b277fb1 (diff) | |
| download | nova-ed0b536e62dfc012e9c4bdc353efbda7d446b409.tar.gz nova-ed0b536e62dfc012e9c4bdc353efbda7d446b409.tar.xz nova-ed0b536e62dfc012e9c4bdc353efbda7d446b409.zip | |
Merge "Add new cliutils code from oslo-incubator."
| -rw-r--r-- | nova/openstack/common/cliutils.py | 66 | ||||
| -rw-r--r-- | openstack-common.conf | 2 |
2 files changed, 67 insertions, 1 deletions
diff --git a/nova/openstack/common/cliutils.py b/nova/openstack/common/cliutils.py new file mode 100644 index 000000000..8f4dc44dd --- /dev/null +++ b/nova/openstack/common/cliutils.py @@ -0,0 +1,66 @@ +# 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 +import string + + +class MissingArgs(Exception): + + def __init__(self, missing): + self.missing = missing + + def __str__(self): + if len(self.missing) == 1: + return ("An argument is missing: %(missing)s" % + dict(missing=self.missing[0])) + else: + return ("%(num)d arguments are missing: %(missing)s" % + dict(num=len(self.missing), + missing=string.join(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: a + >>> validate_args(lambda a, b, c, d: None, 0, c=1) + Traceback (most recent call last): + ... + MissingArgs: 2 arguments are missing: b, d + + :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) diff --git a/openstack-common.conf b/openstack-common.conf index 5f64e6ee2..ea33ab235 100644 --- a/openstack-common.conf +++ b/openstack-common.conf @@ -1,7 +1,7 @@ [DEFAULT] # The list of modules to copy from openstack-common -modules=cfg,context,excutils,eventlet_backdoor,fileutils,gettextutils,importutils,iniparser,jsonutils,local,lockutils,log,network_utils,notifier,plugin,policy,setup,timeutils,rpc,uuidutils +modules=cfg,cliutils,context,excutils,eventlet_backdoor,fileutils,gettextutils,importutils,iniparser,jsonutils,local,lockutils,log,network_utils,notifier,plugin,policy,setup,timeutils,rpc,uuidutils # The base module to hold the copy of openstack.common base=nova |
