From 2962d9d871225e92cf8c02219872a904fcdaed11 Mon Sep 17 00:00:00 2001 From: Jamie Lennox Date: Tue, 26 Apr 2016 07:14:44 +1000 Subject: [PATCH] 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 --- keystoneauth1/loading/_utils.py | 40 ++++++++++++++++++++++++++++++++ keystoneauth1/loading/opts.py | 12 ++-------- keystoneauth1/loading/session.py | 14 +++-------- 3 files changed, 45 insertions(+), 21 deletions(-) create mode 100644 keystoneauth1/loading/_utils.py diff --git a/keystoneauth1/loading/_utils.py b/keystoneauth1/loading/_utils.py new file mode 100644 index 00000000..2cb796cf --- /dev/null +++ b/keystoneauth1/loading/_utils.py @@ -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 diff --git a/keystoneauth1/loading/opts.py b/keystoneauth1/loading/opts.py index 32697b8e..2220040f 100644 --- a/keystoneauth1/loading/opts.py +++ b/keystoneauth1/loading/opts.py @@ -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 '' % 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, diff --git a/keystoneauth1/loading/session.py b/keystoneauth1/loading/session.py index aa0830ae..4e5ee3e6 100644 --- a/keystoneauth1/loading/session.py +++ b/keystoneauth1/loading/session.py @@ -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