|
| 1 | +# Copyright 2012 OpenStack LLC. |
| 2 | +# All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | +# not use this file except in compliance with the License. You may obtain |
| 6 | +# a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations |
| 14 | +# under the License. |
| 15 | + |
| 16 | +import os |
| 17 | +import uuid |
| 18 | + |
| 19 | +import prettytable |
| 20 | + |
| 21 | +from glanceclient.common import exceptions |
| 22 | + |
| 23 | + |
| 24 | +# Decorator for cli-args |
| 25 | +def arg(*args, **kwargs): |
| 26 | + def _decorator(func): |
| 27 | + # Because of the sematics of decorator composition if we just append |
| 28 | + # to the options list positional options will appear to be backwards. |
| 29 | + func.__dict__.setdefault('arguments', []).insert(0, (args, kwargs)) |
| 30 | + return func |
| 31 | + return _decorator |
| 32 | + |
| 33 | + |
| 34 | +def pretty_choice_list(l): |
| 35 | + return ', '.join("'%s'" % i for i in l) |
| 36 | + |
| 37 | + |
| 38 | +def print_list(objs, fields, formatters={}): |
| 39 | + pt = prettytable.PrettyTable([f for f in fields], caching=False) |
| 40 | + pt.aligns = ['l' for f in fields] |
| 41 | + |
| 42 | + for o in objs: |
| 43 | + row = [] |
| 44 | + for field in fields: |
| 45 | + if field in formatters: |
| 46 | + row.append(formatters[field](o)) |
| 47 | + else: |
| 48 | + field_name = field.lower().replace(' ', '_') |
| 49 | + data = getattr(o, field_name, '') |
| 50 | + row.append(data) |
| 51 | + pt.add_row(row) |
| 52 | + |
| 53 | + pt.printt(sortby=fields[0]) |
| 54 | + |
| 55 | + |
| 56 | +def print_dict(d): |
| 57 | + pt = prettytable.PrettyTable(['Property', 'Value'], caching=False) |
| 58 | + pt.aligns = ['l', 'l'] |
| 59 | + [pt.add_row(list(r)) for r in d.iteritems()] |
| 60 | + pt.printt(sortby='Property') |
| 61 | + |
| 62 | + |
| 63 | +def find_resource(manager, name_or_id): |
| 64 | + """Helper for the _find_* methods.""" |
| 65 | + # first try to get entity as integer id |
| 66 | + try: |
| 67 | + if isinstance(name_or_id, int) or name_or_id.isdigit(): |
| 68 | + return manager.get(int(name_or_id)) |
| 69 | + except exceptions.NotFound: |
| 70 | + pass |
| 71 | + |
| 72 | + # now try to get entity as uuid |
| 73 | + try: |
| 74 | + uuid.UUID(str(name_or_id)) |
| 75 | + return manager.get(name_or_id) |
| 76 | + except (ValueError, exceptions.NotFound): |
| 77 | + pass |
| 78 | + |
| 79 | + # finally try to find entity by name |
| 80 | + try: |
| 81 | + return manager.find(name=name_or_id) |
| 82 | + except exceptions.NotFound: |
| 83 | + msg = "No %s with a name or ID of '%s' exists." % \ |
| 84 | + (manager.resource_class.__name__.lower(), name_or_id) |
| 85 | + raise exceptions.CommandError(msg) |
| 86 | + |
| 87 | + |
| 88 | +def skip_authentication(f): |
| 89 | + """Function decorator used to indicate a caller may be unauthenticated.""" |
| 90 | + f.require_authentication = False |
| 91 | + return f |
| 92 | + |
| 93 | + |
| 94 | +def is_authentication_required(f): |
| 95 | + """Checks to see if the function requires authentication. |
| 96 | +
|
| 97 | + Use the skip_authentication decorator to indicate a caller may |
| 98 | + skip the authentication step. |
| 99 | + """ |
| 100 | + return getattr(f, 'require_authentication', True) |
| 101 | + |
| 102 | + |
| 103 | +def string_to_bool(arg): |
| 104 | + return arg.strip().lower() in ('t', 'true', 'yes', '1') |
| 105 | + |
| 106 | + |
| 107 | +def env(*vars, **kwargs): |
| 108 | + """Search for the first defined of possibly many env vars |
| 109 | +
|
| 110 | + Returns the first environment variable defined in vars, or |
| 111 | + returns the default defined in kwargs. |
| 112 | + """ |
| 113 | + for v in vars: |
| 114 | + value = os.environ.get(v, None) |
| 115 | + if value: |
| 116 | + return value |
| 117 | + return kwargs.get('default', '') |
0 commit comments