2
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3
# not use this file except in compliance with the License. You may obtain
4
# a copy of the License at
6
# http://www.apache.org/licenses/LICENSE-2.0
8
# Unless required by applicable law or agreed to in writing, software
9
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11
# License for the specific language governing permissions and limitations
21
from seamicroclient import exceptions
22
from seamicroclient.openstack.common import strutils
25
def env(*args, **kwargs):
27
returns the first environment variable set
28
if none are non-empty, defaults to '' or keyword arg default
31
value = os.environ.get(arg, None)
34
return kwargs.get('default', '')
37
def find_resource(manager, name_or_id, **find_args):
38
"""Helper for the _find_* methods."""
39
# first try to get entity as integer id
41
return manager.get(int(name_or_id))
42
except (TypeError, ValueError, exceptions.NotFound):
45
# now try to get entity as uuid
47
tmp_id = strutils.safe_encode(name_or_id)
49
tmp_id = tmp_id.decode()
51
return manager.get(tmp_id)
52
except (TypeError, ValueError, exceptions.NotFound):
55
# for str id which is not uuid (for Flavor search currently)
56
if getattr(manager, 'is_alphanum_id_allowed', False):
58
return manager.get(name_or_id)
59
except exceptions.NotFound:
64
return manager.find(human_id=name_or_id, **find_args)
65
except exceptions.NotFound:
68
# finally try to find entity by name
70
resource = getattr(manager, 'resource_class', None)
71
name_attr = resource.NAME_ATTR if resource else 'name'
72
kwargs = {name_attr: name_or_id}
73
kwargs.update(find_args)
74
return manager.find(**kwargs)
75
except exceptions.NotFound:
76
msg = "No %s with a name or ID of '%s' exists." % \
77
(manager.resource_class.__name__.lower(), name_or_id)
78
raise exceptions.CommandError(msg)
79
except exceptions.NoUniqueMatch:
80
msg = ("Multiple %s matches found for '%s', use an ID to be more"
81
" specific." % (manager.resource_class.__name__.lower(),
83
raise exceptions.CommandError(msg)
86
def _format_field_name(attr):
87
"""Format an object attribute in a human-friendly way."""
88
# Split at ':' and leave the extension name as-is.
89
parts = attr.rsplit(':', 1)
90
name = parts[-1].replace('_', ' ')
91
# Don't title() on mixed case
92
if name.isupper() or name.islower():
95
return ': '.join(parts)
98
def _make_field_formatter(attr, filters=None):
100
Given an object attribute, return a formatted field name and a
101
formatter suitable for passing to print_list.
103
Optionally pass a dict mapping attribute names to a function. The function
104
will be passed the value of the attribute and should return the string to
109
filter_ = filters.get(attr)
112
field = getattr(obj, attr, '')
113
if field and filter_:
114
field = filter_(field)
117
name = _format_field_name(attr)
118
formatter = get_field
119
return name, formatter
122
class HookableMixin(object):
124
"""Mixin so classes can register and run hooks."""
128
def add_hook(cls, hook_type, hook_func):
129
if hook_type not in cls._hooks_map:
130
cls._hooks_map[hook_type] = []
132
cls._hooks_map[hook_type].append(hook_func)
135
def run_hooks(cls, hook_type, *args, **kwargs):
136
hook_funcs = cls._hooks_map.get(hook_type) or []
137
for hook_func in hook_funcs:
138
hook_func(*args, **kwargs)
141
def safe_issubclass(*args):
142
"""Like issubclass, but will just return False if not a class."""
145
if issubclass(*args):
153
def import_class(import_str):
154
"""Returns a class from a string including module and class."""
155
mod_str, _sep, class_str = import_str.rpartition('.')
157
return getattr(sys.modules[mod_str], class_str)
160
def _load_entry_point(ep_name, name=None):
161
"""Try to load the entry point ep_name that matches name."""
162
for ep in pkg_resources.iter_entry_points(ep_name, name=name):
165
except (ImportError, pkg_resources.UnknownExtra, AttributeError):
169
def is_integer_like(val):
170
"""Returns validation of a value as an integer."""
174
except (TypeError, ValueError, AttributeError):