Files
python-keystoneclient/keystoneclient/utils.py
Dan Prince 11258a06c2 Don't call PrettyTable add_row with a tuple.
Updates the print_dict function in utils.py so that it doesn't
try to append a tuple with add_row. According to pydoc add_row
should get passed a list (not a tuple):

  Arguments:

    row - row of data, should be a list with as many elements as
    the table has fields

This fixes a TypeError which can occur with the existing code:

TypeError: can only concatenate list (not "tuple") to list

Fixes LP Bug #1019409.

Change-Id: I16f745afa872106c3bc44c33d88db2a5aacd776c
2012-06-29 16:32:32 -04:00

103 lines
2.7 KiB
Python

import uuid
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)
def print_list(objs, fields, formatters={}):
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, '')
if data is None:
data = ''
row.append(data)
pt.add_row(row)
print pt.get_string(sortby=fields[0])
def print_dict(d):
pt = prettytable.PrettyTable(['Property', 'Value'], caching=False)
pt.aligns = ['l', 'l']
for (prop, value) in d.iteritems():
if value is None:
value = ''
pt.add_row([prop, value])
print pt.get_string(sortby='Property')
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:
msg = ("No %s with a name or ID of '%s' exists." %
(manager.resource_class.__name__.lower(), name_or_id))
raise exceptions.CommandError(msg)
def unauthenticated(f):
""" Adds 'unauthenticated' attribute to decorated function.
Usage:
@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)
def string_to_bool(arg):
if isinstance(arg, bool):
return arg
return arg.strip().lower() in ('t', 'true', 'yes', '1')