From 17723a6b6dc047e6341bcfcda29120580f352b46 Mon Sep 17 00:00:00 2001 From: Zhongyue Luo Date: Mon, 4 Jun 2012 13:30:35 +0800 Subject: [PATCH] Keystone should use openstack.common.importutils Implements blueprint use-common-importutils Change-Id: I597f71dc72aa3b87a454c4a23ca1b5328e222f76 --- keystone/cli.py | 4 +-- keystone/common/manager.py | 4 +-- keystone/common/utils.py | 21 ----------- keystone/openstack/common/importutils.py | 44 ++++++++++++++++++++++++ keystone/test.py | 7 ++-- openstack-common.conf | 2 +- 6 files changed, 53 insertions(+), 29 deletions(-) create mode 100644 keystone/openstack/common/importutils.py diff --git a/keystone/cli.py b/keystone/cli.py index 81fb2af4b2..439ffb9e8a 100644 --- a/keystone/cli.py +++ b/keystone/cli.py @@ -21,7 +21,7 @@ import sys import textwrap from keystone import config -from keystone.common import utils +from keystone.openstack.common import importutils CONF = config.CONF @@ -52,7 +52,7 @@ class DbSync(BaseApp): def main(self): for k in ['identity', 'catalog', 'policy', 'token']: - driver = utils.import_object(getattr(CONF, k).driver) + driver = importutils.import_object(getattr(CONF, k).driver) if hasattr(driver, 'db_sync'): driver.db_sync() diff --git a/keystone/common/manager.py b/keystone/common/manager.py index 18f3c41f2c..75b34d1f6b 100644 --- a/keystone/common/manager.py +++ b/keystone/common/manager.py @@ -16,7 +16,7 @@ import functools -from keystone.common import utils +from keystone.openstack.common import importutils class Manager(object): @@ -33,7 +33,7 @@ class Manager(object): """ def __init__(self, driver_name): - self.driver = utils.import_object(driver_name) + self.driver = importutils.import_object(driver_name) def __getattr__(self, name): """Forward calls to the underlying driver.""" diff --git a/keystone/common/utils.py b/keystone/common/utils.py index adff875af5..c0d0ffd101 100644 --- a/keystone/common/utils.py +++ b/keystone/common/utils.py @@ -43,27 +43,6 @@ ISO_TIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ' MAX_PASSWORD_LENGTH = 4096 -def import_class(import_str): - """Returns a class from a string including module and class.""" - mod_str, _sep, class_str = import_str.rpartition('.') - try: - __import__(mod_str) - return getattr(sys.modules[mod_str], class_str) - except (ImportError, ValueError, AttributeError), exc: - LOG.debug('Inner Exception: %s', exc) - raise - - -def import_object(import_str, *args, **kw): - """Returns an object including a module or module and class.""" - try: - __import__(import_str) - return sys.modules[import_str] - except ImportError: - cls = import_class(import_str) - return cls(*args, **kw) - - def read_cached_file(filename, cache_info, reload_func=None): """Read from a file if it has been modified. diff --git a/keystone/openstack/common/importutils.py b/keystone/openstack/common/importutils.py new file mode 100644 index 0000000000..7654af5b95 --- /dev/null +++ b/keystone/openstack/common/importutils.py @@ -0,0 +1,44 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 OpenStack LLC. +# 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. + +""" +Import related utilities and helper functions. +""" + +import sys + + +def import_class(import_str): + """Returns a class from a string including module and class""" + mod_str, _sep, class_str = import_str.rpartition('.') + try: + __import__(mod_str) + return getattr(sys.modules[mod_str], class_str) + except (ImportError, ValueError, AttributeError), exc: + raise ImportError('Class %s cannot be found (%s)' % + (class_str, str(exc))) + + +def import_object(import_str, *args, **kwargs): + """Import a class and return an instance of it.""" + return import_class(import_str)(*args, **kwargs) + + +def import_module(import_str): + """Import a module.""" + __import__(import_str) + return sys.modules[import_str] diff --git a/keystone/test.py b/keystone/test.py index 3b4a9122d0..30053ae0fd 100644 --- a/keystone/test.py +++ b/keystone/test.py @@ -29,6 +29,7 @@ from keystone.common import kvs from keystone.common import logging from keystone.common import utils from keystone.common import wsgi +from keystone.openstack.common import importutils LOG = logging.getLogger(__name__) @@ -172,9 +173,9 @@ class TestCase(unittest.TestCase): def load_backends(self): """Hacky shortcut to load the backends for data manipulation.""" - self.identity_api = utils.import_object(CONF.identity.driver) - self.token_api = utils.import_object(CONF.token.driver) - self.catalog_api = utils.import_object(CONF.catalog.driver) + self.identity_api = importutils.import_object(CONF.identity.driver) + self.token_api = importutils.import_object(CONF.token.driver) + self.catalog_api = importutils.import_object(CONF.catalog.driver) def load_fixtures(self, fixtures): """Hacky basic and naive fixture loading based on a python module. diff --git a/openstack-common.conf b/openstack-common.conf index 44eda77356..4fc47f9a36 100644 --- a/openstack-common.conf +++ b/openstack-common.conf @@ -1,7 +1,7 @@ [DEFAULT] # The list of modules to copy from openstack-common -modules=cfg,iniparser,setup +modules=cfg,importutils,iniparser,setup # The base module to hold the copy of openstack.common base=keystone