From e98815718dc29ea6e39e50a318d7e1e8bc4d4698 Mon Sep 17 00:00:00 2001 From: "ChangBo Guo(gcb)" Date: Sat, 24 Sep 2016 22:47:09 +0800 Subject: [PATCH] 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 --- oslo_utils/importutils.py | 22 +++++++++++++++++----- oslo_utils/tests/fake/v2/__init__.py | 0 oslo_utils/tests/fake/v2/dummpy.py | 28 ++++++++++++++++++++++++++++ oslo_utils/tests/test_importutils.py | 19 +++++++++++++++++++ 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 oslo_utils/tests/fake/v2/__init__.py create mode 100644 oslo_utils/tests/fake/v2/dummpy.py diff --git a/oslo_utils/importutils.py b/oslo_utils/importutils.py index 5156cab9..489eb745 100644 --- a/oslo_utils/importutils.py +++ b/oslo_utils/importutils.py @@ -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): diff --git a/oslo_utils/tests/fake/v2/__init__.py b/oslo_utils/tests/fake/v2/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/oslo_utils/tests/fake/v2/dummpy.py b/oslo_utils/tests/fake/v2/dummpy.py new file mode 100644 index 00000000..fdad2cf1 --- /dev/null +++ b/oslo_utils/tests/fake/v2/dummpy.py @@ -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__") diff --git a/oslo_utils/tests/test_importutils.py b/oslo_utils/tests/test_importutils.py index 0fde3805..14fe1622 100644 --- a/oslo_utils/tests/test_importutils.py +++ b/oslo_utils/tests/test_importutils.py @@ -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)