Lazy import oslo_config as required

Openstackclient is taking the hit for importing oslo.config (which is
significant) just because we are doing an opportunistic load at import
time.

Change the opportunistic load to also happen at runtime to remove this
import. This has no user facing effect.

Change-Id: I3983a19fbe82cb1d360b5b5aec75b2697efa232d
This commit is contained in:
Jamie Lennox 2016-04-26 07:14:44 +10:00
parent 9d51a2ae60
commit 2962d9d871
3 changed files with 45 additions and 21 deletions

View File

@ -0,0 +1,40 @@
# 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.
cfg = None
_NOT_FOUND = object()
def get_oslo_config():
"""Runtime load the oslo.config object.
In performance optimization of openstackclient it was determined that even
optimistically loading oslo.config if available had a performance cost.
Given that we used to only raise the ImportError when the function was
called also attempt to do the import to do everything at runtime.
"""
global cfg
# First Call
if not cfg:
try:
from oslo_config import cfg
except ImportError:
cfg = _NOT_FOUND
if cfg is _NOT_FOUND:
raise ImportError("oslo.config is not an automatic dependency of "
"keystoneauth. If you wish to use oslo.config "
"you need to import it into your application's "
"requirements file. ")
return cfg

View File

@ -15,10 +15,7 @@ import os
from positional import positional
try:
from oslo_config import cfg
except ImportError:
cfg = None
from keystoneauth1.loading import _utils
__all__ = ('Opt',)
@ -99,12 +96,7 @@ class Opt(object):
return '<Opt: %s>' % self.name
def _to_oslo_opt(self):
if not cfg:
raise ImportError("oslo.config is not an automatic dependency of "
"keystoneauth. If you wish to use oslo.config "
"you need to import it into your application's "
"requirements file. ")
cfg = _utils.get_oslo_config()
deprecated_opts = [cfg.DeprecatedOpt(o.name) for o in self.deprecated]
return cfg.Opt(name=self.name,

View File

@ -15,11 +15,7 @@ import os
from positional import positional
try:
from oslo_config import cfg
except ImportError:
cfg = None
from keystoneauth1.loading import _utils
from keystoneauth1.loading import base
from keystoneauth1 import session
@ -160,11 +156,7 @@ class Session(base.BaseLoader):
:returns: A list of oslo_config options.
"""
if not cfg:
raise ImportError("oslo.config is not an automatic dependency of "
"keystoneauth. If you wish to use oslo.config "
"you need to import it into your application's "
"requirements file. ")
cfg = _utils.get_oslo_config()
if deprecated_opts is None:
deprecated_opts = {}
@ -214,7 +206,7 @@ class Session(base.BaseLoader):
:returns: The list of options that was registered.
"""
opts = self.get_conf_options(deprecated_opts=deprecated_opts)
conf.register_group(cfg.OptGroup(group))
conf.register_group(_utils.get_oslo_config().OptGroup(group))
conf.register_opts(opts, group=group)
return opts