2011-10-25 16:50:08 -07:00
|
|
|
import uuid
|
2012-11-12 19:40:21 +00:00
|
|
|
import hashlib
|
2011-10-25 16:50:08 -07:00
|
|
|
|
|
|
|
import prettytable
|
|
|
|
|
|
|
|
from keystoneclient import exceptions
|
|
|
|
|
|
|
|
|
|
|
|
# Decorator for cli-args
|
|
|
|
def arg(*args, **kwargs):
|
|
|
|
def _decorator(func):
|
|
|
|
# Because of the sematics of decorator composition if we just append
|
|
|
|
# to the options list positional options will appear to be backwards.
|
|
|
|
func.__dict__.setdefault('arguments', []).insert(0, (args, kwargs))
|
|
|
|
return func
|
|
|
|
return _decorator
|
|
|
|
|
|
|
|
|
|
|
|
def pretty_choice_list(l):
|
|
|
|
return ', '.join("'%s'" % i for i in l)
|
|
|
|
|
|
|
|
|
2012-12-10 19:18:02 +09:00
|
|
|
def print_list(objs, fields, formatters={}, order_by=None):
|
2011-10-25 16:50:08 -07:00
|
|
|
pt = prettytable.PrettyTable([f for f in fields], caching=False)
|
|
|
|
pt.aligns = ['l' for f in fields]
|
|
|
|
|
|
|
|
for o in objs:
|
|
|
|
row = []
|
|
|
|
for field in fields:
|
|
|
|
if field in formatters:
|
|
|
|
row.append(formatters[field](o))
|
|
|
|
else:
|
|
|
|
field_name = field.lower().replace(' ', '_')
|
|
|
|
data = getattr(o, field_name, '')
|
2012-06-26 11:08:24 +02:00
|
|
|
if data is None:
|
|
|
|
data = ''
|
2011-10-25 16:50:08 -07:00
|
|
|
row.append(data)
|
|
|
|
pt.add_row(row)
|
|
|
|
|
2012-12-10 19:18:02 +09:00
|
|
|
if order_by is None:
|
|
|
|
order_by = fields[0]
|
|
|
|
print pt.get_string(sortby=order_by)
|
2011-10-25 16:50:08 -07:00
|
|
|
|
|
|
|
|
2012-09-20 23:54:20 +00:00
|
|
|
def _word_wrap(string, max_length=0):
|
|
|
|
"""wrap long strings to be no longer then max_length"""
|
|
|
|
if max_length <= 0:
|
|
|
|
return string
|
|
|
|
return '\n'.join([string[i:i + max_length] for i in
|
2012-09-29 14:59:35 -07:00
|
|
|
range(0, len(string), max_length)])
|
2012-09-20 23:54:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def print_dict(d, wrap=0):
|
|
|
|
"""pretty table prints dictionaries.
|
|
|
|
|
|
|
|
Wrap values to max_length wrap if wrap>0
|
|
|
|
"""
|
2011-10-25 16:50:08 -07:00
|
|
|
pt = prettytable.PrettyTable(['Property', 'Value'], caching=False)
|
|
|
|
pt.aligns = ['l', 'l']
|
2012-06-26 11:08:24 +02:00
|
|
|
for (prop, value) in d.iteritems():
|
|
|
|
if value is None:
|
|
|
|
value = ''
|
2012-09-20 23:54:20 +00:00
|
|
|
value = _word_wrap(value, max_length=wrap)
|
2012-06-29 16:27:25 -04:00
|
|
|
pt.add_row([prop, value])
|
2012-05-07 10:14:21 -05:00
|
|
|
print pt.get_string(sortby='Property')
|
2011-10-25 16:50:08 -07:00
|
|
|
|
|
|
|
|
|
|
|
def find_resource(manager, name_or_id):
|
|
|
|
"""Helper for the _find_* methods."""
|
|
|
|
# first try to get entity as integer id
|
|
|
|
try:
|
|
|
|
if isinstance(name_or_id, int) or name_or_id.isdigit():
|
|
|
|
return manager.get(int(name_or_id))
|
|
|
|
except exceptions.NotFound:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# now try to get entity as uuid
|
|
|
|
try:
|
|
|
|
uuid.UUID(str(name_or_id))
|
|
|
|
return manager.get(name_or_id)
|
|
|
|
except (ValueError, exceptions.NotFound):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# finally try to find entity by name
|
|
|
|
try:
|
|
|
|
return manager.find(name=name_or_id)
|
|
|
|
except exceptions.NotFound:
|
2012-02-29 11:31:10 +08:00
|
|
|
msg = ("No %s with a name or ID of '%s' exists." %
|
|
|
|
(manager.resource_class.__name__.lower(), name_or_id))
|
2011-10-25 16:50:08 -07:00
|
|
|
raise exceptions.CommandError(msg)
|
2013-01-17 18:11:27 +09:00
|
|
|
except exceptions.NoUniqueMatch:
|
|
|
|
msg = ("Multiple %s matches found for '%s', use an ID to be more"
|
|
|
|
" specific." % (manager.resource_class.__name__.lower(),
|
|
|
|
name_or_id))
|
|
|
|
raise exc.CommandError(msg)
|
2011-12-28 00:23:31 -06:00
|
|
|
|
|
|
|
|
|
|
|
def unauthenticated(f):
|
2012-09-29 15:28:08 -07:00
|
|
|
"""Adds 'unauthenticated' attribute to decorated function.
|
|
|
|
|
|
|
|
Usage::
|
2011-12-28 00:23:31 -06:00
|
|
|
|
|
|
|
@unauthenticated
|
|
|
|
def mymethod(f):
|
|
|
|
...
|
|
|
|
"""
|
|
|
|
f.unauthenticated = True
|
|
|
|
return f
|
|
|
|
|
|
|
|
|
|
|
|
def isunauthenticated(f):
|
|
|
|
"""
|
|
|
|
Checks to see if the function is marked as not requiring authentication
|
|
|
|
with the @unauthenticated decorator. Returns True if decorator is
|
|
|
|
set to True, False otherwise.
|
|
|
|
"""
|
|
|
|
return getattr(f, 'unauthenticated', False)
|
2012-02-21 15:02:41 -08:00
|
|
|
|
|
|
|
|
|
|
|
def string_to_bool(arg):
|
2012-03-13 15:51:23 -05:00
|
|
|
if isinstance(arg, bool):
|
|
|
|
return arg
|
|
|
|
|
2012-02-21 15:02:41 -08:00
|
|
|
return arg.strip().lower() in ('t', 'true', 'yes', '1')
|
2012-11-12 19:40:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def hash_signed_token(signed_text):
|
|
|
|
hash_ = hashlib.md5()
|
|
|
|
hash_.update(signed_text)
|
|
|
|
return hash_.hexdigest()
|