Keystone should use openstack.common.importutils

Implements blueprint use-common-importutils

Change-Id: I597f71dc72aa3b87a454c4a23ca1b5328e222f76
This commit is contained in:
Zhongyue Luo 2012-06-04 13:30:35 +08:00
parent 4bfa203ac4
commit 17723a6b6d
6 changed files with 53 additions and 29 deletions

View File

@ -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()

View File

@ -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."""

View File

@ -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.

View File

@ -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]

View File

@ -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.

View File

@ -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