Files
gantt/nova/openstack/common/cliutils.py
Alessio Ababilov 5aa543194b Synchronize code from oslo
Use commit eaab5fae2502198e9fa57d0d90a7204a2bd83b16:
Merge "sort options to make --help output prettier"
(Wed Feb 13 12:52:14 2013 +0000).

Changes:
  9669767 Fix PEP8 error in oslo-rootwrap
  e3e5e0e Fixes "is not", "not in" syntax usage
  d156150 Implements import_group
  0ce65aa sort options to make --help output prettier
  580c259 Make tox run doctests
  d8c4e0c Change Exception MissingArgs's string
  6d102bc Provide i18n to those messages without _()
  cf705c5 Make project pyflakes clean
  9e5912f Fix pep8 E125 errors
  4a1ec21 Support testing args for LocalhostMatchMaker
  9fd6437 Exchanges should return directed topics
  a4b6c31 Allow running test in uninstalled source tree
  1461135 timeutils: considers that now is soon
  a956f7a Import timeutils.is_soon from keystoneclient
  a4b6c31 Allow running test in uninstalled source tree
  076e9e5 Add support for directly stringifying VersionInfo

Change-Id: I427508f0882a528d040c89290ff9ca68a1e91bcd
Fixes: bug #1124213
2013-02-14 11:13:15 +02:00

64 lines
1.9 KiB
Python

# 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)