Make method import_versioned_module work

We hard-code module name in method import_versioned_module,
but downstream projects can't work and must provide specific
version of this method[1], and we don't use it in any project,
so we don't need consider backwards compatible issue.

[1] http://codesearch.openstack.org/?q=import_versioned_module&i=nope&files=&repos=

Closes-Bug: #1627313
Change-Id: I9eac6e63283a6b3a364307b32c567d27e97bdb6e
This commit is contained in:
ChangBo Guo(gcb) 2016-09-24 22:47:09 +08:00
parent 48be493fe3
commit e98815718d
4 changed files with 64 additions and 5 deletions

View File

@ -74,15 +74,27 @@ def import_module(import_str):
return sys.modules[import_str]
def import_versioned_module(version, submodule=None):
"""Import a versioned module.
def import_versioned_module(module, version, submodule=None):
"""Import a versioned module in format {module}.v{version][.{submodule}].
:param module: the module name.
:param version: the version number.
:param submodule: the submodule name.
:raises ValueError: For any invalid input.
.. versionadded:: 0.3
.. versionchanged:: 3.17
Added *module* parameter.
"""
module = 'oslo.v%s' % version
# NOTE(gcb) Disallow parameter version include character '.'
if '.' in '%s' % version:
raise ValueError("Parameter version shouldn't include character '.'.")
module_str = '%s.v%s' % (module, version)
if submodule:
module = '.'.join((module, submodule))
return import_module(module)
module_str = '.'.join((module_str, submodule))
return import_module(module_str)
def try_import(import_str, default=None):

View File

View File

@ -0,0 +1,28 @@
# Copyright 2016, EasyStack, Inc.
#
# 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.
class V2FakeDriver(object):
def __init__(self, first_arg=True):
self.first_arg = first_arg
class V2FakeDriver2(object):
def __init__(self, first_arg):
self.first_arg = first_arg
class V2FakeDriver3(object):
def __init__(self):
raise ImportError("ImportError occurs in __init__")

View File

@ -116,6 +116,25 @@ class ImportUtilsTest(test_base.BaseTestCase):
self.assertIsInstance(dt, sys.modules['datetime'].datetime)
self.assertEqual(dt, datetime.datetime(2012, 4, 5))
def test_import_versioned_module(self):
v2 = importutils.import_versioned_module('oslo_utils.tests.fake', 2)
self.assertEqual(sys.modules['oslo_utils.tests.fake.v2'], v2)
dummpy = importutils.import_versioned_module('oslo_utils.tests.fake',
2, 'dummpy')
self.assertEqual(sys.modules['oslo_utils.tests.fake.v2.dummpy'],
dummpy)
def test_import_versioned_module_wrong_version_parameter(self):
self.assertRaises(ValueError,
importutils.import_versioned_module,
'oslo_utils.tests.fake', "2.0", 'fake')
def test_import_versioned_module_error(self):
self.assertRaises(ImportError,
importutils.import_versioned_module,
'oslo_utils.tests.fake', 2, 'fake')
def test_try_import(self):
dt = importutils.try_import('datetime')
self.assertEqual(sys.modules['datetime'], dt)