Nuke cliutils from oslo-incubator

Oslo team won't create new libary to include functions of module cliutils.
Nova only use two methods in cmd/manage.py, so copy these two methods
to nova/utils.py and clean up oslo-incubator related stuff.

Note:There is tiny change of method validate_args to make it Python 3
compatible.

Change-Id: Ibef5076f9556fa3224c2ca32e8c7fe2c4c1f739b
This commit is contained in:
ChangBo Guo(gcb) 2016-03-03 20:13:16 +08:00
parent 209bf96605
commit 504112582e
2 changed files with 66 additions and 2 deletions

View File

@ -34,6 +34,7 @@ import socket
import struct
import sys
import tempfile
import textwrap
import time
from xml.sax import saxutils
@ -51,6 +52,7 @@ from oslo_utils import importutils
from oslo_utils import strutils
from oslo_utils import timeutils
from oslo_utils import units
import prettytable
import six
from six.moves import range
@ -1473,3 +1475,65 @@ def isotime(at=None):
def strtime(at):
return at.strftime("%Y-%m-%dT%H:%M:%S.%f")
def print_dict(dct, dict_property="Property", wrap=0, dict_value='Value'):
"""Print a `dict` as a table of two columns.
:param dct: `dict` to print
:param dict_property: name of the first column
:param wrap: wrapping for the second column
:param dict_value: header label for the value (second) column
"""
pt = prettytable.PrettyTable([dict_property, dict_value])
pt.align = 'l'
for k, v in sorted(dct.items()):
# convert dict to str to check length
if isinstance(v, dict):
v = six.text_type(v)
if wrap > 0:
v = textwrap.fill(six.text_type(v), wrap)
# if value has a newline, add in multiple rows
# e.g. fault with stacktrace
if v and isinstance(v, six.string_types) and r'\n' in v:
lines = v.strip().split(r'\n')
col1 = k
for line in lines:
pt.add_row([col1, line])
col1 = ''
else:
pt.add_row([k, v])
if six.PY2:
print(encodeutils.safe_encode(pt.get_string()))
else:
print(encodeutils.safe_encode(pt.get_string()).decode())
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: Missing argument(s): a
>>> validate_args(lambda a, b, c, d: None, 0, c=1)
Traceback (most recent call last):
...
MissingArgs: Missing argument(s): 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]
if six.get_method_self(fn) is not None:
required_args.pop(0)
missing = [arg for arg in required_args if arg not in kwargs]
missing = missing[len(args):]
return missing

View File

@ -75,7 +75,7 @@ commands =
python setup.py testr --coverage \
--testr-args='{posargs}'
coverage combine
coverage html --include='nova/*' --omit='nova/openstack/common/*' -d covhtml -i
coverage html --include='nova/*' -d covhtml -i
[testenv:venv]
# NOTE(jaegerandi): This target does not use constraints because
@ -136,7 +136,7 @@ commands =
# E251 Skipped due to https://github.com/jcrocholl/pep8/issues/301
ignore = E121,E122,E123,E124,E125,E126,E127,E128,E129,E131,E251,H405
exclude = .venv,.git,.tox,dist,doc,*openstack/common/*,*lib/python*,*egg,build,tools/xenserver*,releasenotes
exclude = .venv,.git,.tox,dist,doc,*lib/python*,*egg,build,tools/xenserver*,releasenotes
# To get a list of functions that are more complex than 25, set max-complexity
# to 25 and run 'tox -epep8'.
# 34 is currently the most complex thing we have