From a94050f446deb2cc58c069f2226037852deed96d Mon Sep 17 00:00:00 2001 From: Brant Knudson Date: Tue, 1 Jul 2014 15:44:38 -0500 Subject: [PATCH] Sync with oslo-incubator fd90c34a9 This syncs python-keystoneclient with oslo-incubator commit hash fd90c34a914ef98e0c7ccdbf823d9b6d04aa436b First, remove the existing code to cleanup: $ rm -r keystoneclient/openstack/* Then, sync from oslo-incubator: $ python update.py ../python-keystoneclient Commits since last sync (caed79d): ---------------------------------- de4adbc pep8: fixed multiple violations e42e77f Restore UUID and human-ID bash completion 250cd88 Fixed a new pep8 error and a small typo 9e88af1 fixed typos found by RETF rules 0d7296f Add kwargs to jsonutils.load(s) functions 822e09b Don't slugify names that don't exist Change-Id: I33378512593a0cdbc0db0def171b66911aba8708 --- .../openstack/common/apiclient/base.py | 24 ++++++++++-- .../openstack/common/apiclient/exceptions.py | 4 +- .../openstack/common/apiclient/fake_client.py | 2 +- .../openstack/common/gettextutils.py | 4 +- keystoneclient/openstack/common/jsonutils.py | 8 ++-- keystoneclient/openstack/common/timeutils.py | 4 +- keystoneclient/openstack/common/uuidutils.py | 37 +++++++++++++++++++ 7 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 keystoneclient/openstack/common/uuidutils.py diff --git a/keystoneclient/openstack/common/apiclient/base.py b/keystoneclient/openstack/common/apiclient/base.py index d958ae173..511fd732c 100644 --- a/keystoneclient/openstack/common/apiclient/base.py +++ b/keystoneclient/openstack/common/apiclient/base.py @@ -32,6 +32,7 @@ from six.moves.urllib import parse from keystoneclient.openstack.common.apiclient import exceptions from keystoneclient.openstack.common.gettextutils import _ from keystoneclient.openstack.common import strutils +from keystoneclient.openstack.common import uuidutils def getid(obj): @@ -436,6 +437,21 @@ class Resource(object): self._info = info self._add_details(info) self._loaded = loaded + self._init_completion_cache() + + def _init_completion_cache(self): + cache_write = getattr(self.manager, 'write_to_completion_cache', None) + if not cache_write: + return + + # NOTE(sirp): ensure `id` is already present because if it isn't we'll + # enter an infinite loop of __getattr__ -> get -> __init__ -> + # __getattr__ -> ... + if 'id' in self.__dict__ and uuidutils.is_uuid_like(self.id): + cache_write('uuid', self.id) + + if self.human_id: + cache_write('human_id', self.human_id) def __repr__(self): reprkeys = sorted(k @@ -448,8 +464,10 @@ class Resource(object): def human_id(self): """Human-readable ID which can be used for bash completion. """ - if self.NAME_ATTR in self.__dict__ and self.HUMAN_ID: - return strutils.to_slug(getattr(self, self.NAME_ATTR)) + if self.HUMAN_ID: + name = getattr(self, self.NAME_ATTR, None) + if name is not None: + return strutils.to_slug(name) return None def _add_details(self, info): @@ -463,7 +481,7 @@ class Resource(object): def __getattr__(self, k): if k not in self.__dict__: - #NOTE(bcwaldon): disallow lazy-loading if already loaded once + # NOTE(bcwaldon): disallow lazy-loading if already loaded once if not self.is_loaded(): self.get() return self.__getattr__(k) diff --git a/keystoneclient/openstack/common/apiclient/exceptions.py b/keystoneclient/openstack/common/apiclient/exceptions.py index f660564f3..d7e93704a 100644 --- a/keystoneclient/openstack/common/apiclient/exceptions.py +++ b/keystoneclient/openstack/common/apiclient/exceptions.py @@ -77,7 +77,7 @@ class AuthPluginOptionsMissing(AuthorizationFailure): class AuthSystemNotFound(AuthorizationFailure): - """User has specified a AuthSystem that is not installed.""" + """User has specified an AuthSystem that is not installed.""" def __init__(self, auth_system): super(AuthSystemNotFound, self).__init__( _("AuthSystemNotFound: %s") % repr(auth_system)) @@ -427,7 +427,7 @@ def from_response(response, method, url): """ req_id = response.headers.get("x-openstack-request-id") - #NOTE(hdd) true for older versions of nova and cinder + # NOTE(hdd) true for older versions of nova and cinder if not req_id: req_id = response.headers.get("x-compute-request-id") kwargs = { diff --git a/keystoneclient/openstack/common/apiclient/fake_client.py b/keystoneclient/openstack/common/apiclient/fake_client.py index 943595912..47894e305 100644 --- a/keystoneclient/openstack/common/apiclient/fake_client.py +++ b/keystoneclient/openstack/common/apiclient/fake_client.py @@ -79,7 +79,7 @@ class FakeHTTPClient(client.HTTPClient): def __init__(self, *args, **kwargs): self.callstack = [] self.fixtures = kwargs.pop("fixtures", None) or {} - if not args and not "auth_plugin" in kwargs: + if not args and "auth_plugin" not in kwargs: args = (None, ) super(FakeHTTPClient, self).__init__(*args, **kwargs) diff --git a/keystoneclient/openstack/common/gettextutils.py b/keystoneclient/openstack/common/gettextutils.py index 883a0b907..d57d468a8 100644 --- a/keystoneclient/openstack/common/gettextutils.py +++ b/keystoneclient/openstack/common/gettextutils.py @@ -373,8 +373,8 @@ def get_available_languages(domain): 'zh_Hant_HK': 'zh_HK', 'zh_Hant': 'zh_TW', 'fil': 'tl_PH'} - for (locale, alias) in six.iteritems(aliases): - if locale in language_list and alias not in language_list: + for (locale_, alias) in six.iteritems(aliases): + if locale_ in language_list and alias not in language_list: language_list.append(alias) _AVAILABLE_LANGUAGES[domain] = language_list diff --git a/keystoneclient/openstack/common/jsonutils.py b/keystoneclient/openstack/common/jsonutils.py index 7302ac8c6..3ae414a20 100644 --- a/keystoneclient/openstack/common/jsonutils.py +++ b/keystoneclient/openstack/common/jsonutils.py @@ -168,12 +168,12 @@ def dumps(value, default=to_primitive, **kwargs): return json.dumps(value, default=default, **kwargs) -def loads(s, encoding='utf-8'): - return json.loads(strutils.safe_decode(s, encoding)) +def loads(s, encoding='utf-8', **kwargs): + return json.loads(strutils.safe_decode(s, encoding), **kwargs) -def load(fp, encoding='utf-8'): - return json.load(codecs.getreader(encoding)(fp)) +def load(fp, encoding='utf-8', **kwargs): + return json.load(codecs.getreader(encoding)(fp), **kwargs) try: diff --git a/keystoneclient/openstack/common/timeutils.py b/keystoneclient/openstack/common/timeutils.py index 52688a026..c48da95f1 100644 --- a/keystoneclient/openstack/common/timeutils.py +++ b/keystoneclient/openstack/common/timeutils.py @@ -114,7 +114,7 @@ def utcnow(): def iso8601_from_timestamp(timestamp): - """Returns a iso8601 formatted date from timestamp.""" + """Returns an iso8601 formatted date from timestamp.""" return isotime(datetime.datetime.utcfromtimestamp(timestamp)) @@ -134,7 +134,7 @@ def set_time_override(override_time=None): def advance_time_delta(timedelta): """Advance overridden time using a datetime.timedelta.""" - assert(not utcnow.override_time is None) + assert utcnow.override_time is not None try: for dt in utcnow.override_time: dt += timedelta diff --git a/keystoneclient/openstack/common/uuidutils.py b/keystoneclient/openstack/common/uuidutils.py new file mode 100644 index 000000000..234b880c9 --- /dev/null +++ b/keystoneclient/openstack/common/uuidutils.py @@ -0,0 +1,37 @@ +# Copyright (c) 2012 Intel Corporation. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +""" +UUID related utilities and helper functions. +""" + +import uuid + + +def generate_uuid(): + return str(uuid.uuid4()) + + +def is_uuid_like(val): + """Returns validation of a value as a UUID. + + For our purposes, a UUID is a canonical form string: + aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa + + """ + try: + return str(uuid.UUID(val)) == val + except (TypeError, ValueError, AttributeError): + return False