libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject [04/15] libcloud git commit: Reduce code duplication by moving commonon functionality into new libcloud.common.providers module.
Date Wed, 06 Apr 2016 15:17:36 GMT
Reduce code duplication by moving commonon functionality into new
libcloud.common.providers module.

Add an alias to the old locations in libcloud.utils.misc and update affected
code.


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/2f73fdf4
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/2f73fdf4
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/2f73fdf4

Branch: refs/heads/trunk
Commit: 2f73fdf4e9955509d99645ef786faf67ab783415
Parents: a7337fe
Author: Tomaz Muraus <tomaz@tomaz.me>
Authored: Tue Mar 15 15:17:16 2016 -0700
Committer: Tomaz Muraus <tomaz@tomaz.me>
Committed: Thu Mar 31 18:32:19 2016 -0700

----------------------------------------------------------------------
 libcloud/backup/providers.py            |   9 ++-
 libcloud/common/providers.py            | 107 +++++++++++++++++++++++++++
 libcloud/compute/providers.py           |  35 ++-------
 libcloud/container/providers.py         |   9 ++-
 libcloud/dns/providers.py               |   9 ++-
 libcloud/loadbalancer/providers.py      |   9 ++-
 libcloud/storage/providers.py           |   9 ++-
 libcloud/test/compute/test_rackspace.py |   1 +
 libcloud/utils/misc.py                  |  49 ++----------
 9 files changed, 146 insertions(+), 91 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/2f73fdf4/libcloud/backup/providers.py
----------------------------------------------------------------------
diff --git a/libcloud/backup/providers.py b/libcloud/backup/providers.py
index dea0cf5..16cd610 100644
--- a/libcloud/backup/providers.py
+++ b/libcloud/backup/providers.py
@@ -13,9 +13,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from libcloud.utils.misc import get_driver as get_provider_driver
-from libcloud.utils.misc import set_driver as set_provider_driver
 from libcloud.backup.types import Provider
+from libcloud.common.providers import get_driver as _get_provider_driver
+from libcloud.common.providers import set_driver as _set_provider_driver
 
 DRIVERS = {
     Provider.DUMMY:
@@ -30,8 +30,9 @@ DRIVERS = {
 
 
 def get_driver(provider):
-    return get_provider_driver(DRIVERS, provider)
+    return _get_provider_driver(drivers=DRIVERS, provider=provider)
 
 
 def set_driver(provider, module, klass):
-    return set_provider_driver(DRIVERS, provider, module, klass)
+    return _set_provider_driver(drivers=DRIVERS, provider=provider,
+                                module=module, klass=klass)

http://git-wip-us.apache.org/repos/asf/libcloud/blob/2f73fdf4/libcloud/common/providers.py
----------------------------------------------------------------------
diff --git a/libcloud/common/providers.py b/libcloud/common/providers.py
new file mode 100644
index 0000000..b8ad271
--- /dev/null
+++ b/libcloud/common/providers.py
@@ -0,0 +1,107 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+
+"""
+Common methods for obtaining a reference to the provider driver class.
+"""
+
+import sys
+
+__all__ = [
+    'get_driver',
+    'set_driver'
+]
+
+
+def get_driver(drivers, provider, deprecated_providers=None,
+               deprecated_constants=None):
+    """
+    Get a driver.
+
+    :param drivers: Dictionary containing valid providers.
+    :type drivers: ``dict``
+
+    :param provider: Id (constant) of provider to get the driver for.
+    :type provider: :class:`libcloud.types.Provider`
+
+    :param: deprecated_providers: Dictionary with information about the
+            deprecated drivers.
+    :type deprecated_providers: ``dict``
+
+    :param: deprecated_constants: Dictionary with information about the
+            deprecated provider constants.
+    :type deprecated_constants: ``dict``
+    """
+    # Those providers have been shut down or similar.
+    deprecated_providers = deprecated_providers or {}
+    if provider in deprecated_providers:
+        url = deprecated_providers[provider]['url']
+        reason = deprecated_providers[provider]['reason']
+        msg = ('Provider no longer supported: %s, please visit: %s' %
+               (url, reason))
+        raise Exception(msg)
+
+    # Those drivers have moved to "region" constructor argument model
+    deprecated_constants = deprecated_constants or {}
+    if provider in deprecated_constants:
+        old_name = provider.upper()
+        new_name = deprecated_constants[provider].upper()
+
+        url = 'https://s.apache.org/lc0140un'
+        msg = ('Provider constant "%s" has been removed. New constant '
+               'is now called "%s".\n'
+               'For more information on this change and how to modify your '
+               'code to work with it, please visit: %s' %
+               (old_name, new_name, url))
+        raise Exception(msg)
+
+    if provider in drivers:
+        mod_name, driver_name = drivers[provider]
+        _mod = __import__(mod_name, globals(), locals(), [driver_name])
+        return getattr(_mod, driver_name)
+
+    raise AttributeError('Provider %s does not exist' % (provider))
+
+
+def set_driver(drivers, provider, module, klass):
+    """
+    Sets a driver.
+
+    :param drivers: Dictionary to store providers.
+    :param provider: Id of provider to set driver for
+
+    :type provider: :class:`libcloud.types.Provider`
+    :param module: The module which contains the driver
+
+    :type module: L
+    :param klass: The driver class name
+
+    :type klass:
+    """
+
+    if provider in drivers:
+        raise AttributeError('Provider %s already registered' % (provider))
+
+    drivers[provider] = (module, klass)
+
+    # Check if this driver is valid
+    try:
+        driver = get_driver(drivers, provider)
+    except (ImportError, AttributeError):
+        exp = sys.exc_info()[1]
+        drivers.pop(provider)
+        raise exp
+
+    return driver

http://git-wip-us.apache.org/repos/asf/libcloud/blob/2f73fdf4/libcloud/compute/providers.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/providers.py b/libcloud/compute/providers.py
index 687a653..b186757 100644
--- a/libcloud/compute/providers.py
+++ b/libcloud/compute/providers.py
@@ -16,9 +16,9 @@
 Provider related utilities
 """
 
-from libcloud.utils.misc import get_driver as _get_provider_driver
-from libcloud.utils.misc import set_driver as _set_provider_driver
 from libcloud.compute.types import Provider
+from libcloud.common.providers import get_driver as _get_provider_driver
+from libcloud.common.providers import set_driver as _set_provider_driver
 from libcloud.compute.types import OLD_CONSTANT_TO_NEW_MAPPING
 from libcloud.compute.deprecated import DEPRECATED_DRIVERS
 
@@ -143,34 +143,13 @@ DRIVERS = {
     ('libcloud.compute.drivers.ecs', 'ECSDriver'),
 }
 
-DEPRECTATED_PROVIDER_CONSTANTS = OLD_CONSTANT_TO_NEW_MAPPING.keys()
-
 
 def get_driver(provider):
-    # Those providers have been shut down or similar.
-    if provider in DEPRECATED_DRIVERS:
-        url = DEPRECATED_DRIVERS[provider]['url']
-        reason = DEPRECATED_DRIVERS[provider]['reason']
-        msg = ('Provider no longer supported: %s, please visit: %s' %
-               (url, reason))
-        raise Exception(msg)
-
-    # Those drivers have moved to "region" constructor argument model
-    if provider in DEPRECTATED_PROVIDER_CONSTANTS:
-        id_to_name_map = dict([(v, k) for k, v in Provider.__dict__.items()])
-        old_name = id_to_name_map[provider]
-        new_name = id_to_name_map[OLD_CONSTANT_TO_NEW_MAPPING[provider]]
-
-        url = 'http://s.apache.org/lc0140un'
-        msg = ('Provider constant %s has been removed. New constant '
-               'is now called %s.\n'
-               'For more information on this change and how to modify your '
-               'code to work with it, please visit: %s' %
-               (old_name, new_name, url))
-        raise Exception(msg)
-
-    return _get_provider_driver(DRIVERS, provider)
+    return _get_provider_driver(drivers=DRIVERS, provider=provider,
+                                deprecated_providers=DEPRECATED_DRIVERS,
+                                deprecated_constants=OLD_CONSTANT_TO_NEW_MAPPING)
 
 
 def set_driver(provider, module, klass):
-    return _set_provider_driver(DRIVERS, provider, module, klass)
+    return _set_provider_driver(drivers=DRIVERS, provider=provider,
+                                module=module, klass=klass)

http://git-wip-us.apache.org/repos/asf/libcloud/blob/2f73fdf4/libcloud/container/providers.py
----------------------------------------------------------------------
diff --git a/libcloud/container/providers.py b/libcloud/container/providers.py
index 47897ed..16ab58c 100644
--- a/libcloud/container/providers.py
+++ b/libcloud/container/providers.py
@@ -13,9 +13,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from libcloud.utils.misc import get_driver as get_provider_driver
-from libcloud.utils.misc import set_driver as set_provider_driver
 from libcloud.container.types import Provider
+from libcloud.common.providers import get_driver as _get_provider_driver
+from libcloud.common.providers import set_driver as _set_provider_driver
 
 DRIVERS = {
     Provider.DUMMY:
@@ -32,8 +32,9 @@ DRIVERS = {
 
 
 def get_driver(provider):
-    return get_provider_driver(DRIVERS, provider)
+    return _get_provider_driver(drivers=DRIVERS, provider=provider)
 
 
 def set_driver(provider, module, klass):
-    return set_provider_driver(DRIVERS, provider, module, klass)
+    return _set_provider_driver(drivers=DRIVERS, provider=provider,
+                                module=module, klass=klass)

http://git-wip-us.apache.org/repos/asf/libcloud/blob/2f73fdf4/libcloud/dns/providers.py
----------------------------------------------------------------------
diff --git a/libcloud/dns/providers.py b/libcloud/dns/providers.py
index 5f2e56a..c7ad4e4 100644
--- a/libcloud/dns/providers.py
+++ b/libcloud/dns/providers.py
@@ -13,9 +13,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from libcloud.utils.misc import get_driver as get_provider_driver
-from libcloud.utils.misc import set_driver as set_provider_driver
 from libcloud.dns.types import Provider
+from libcloud.common.providers import get_driver as _get_provider_driver
+from libcloud.common.providers import set_driver as _set_provider_driver
 
 DRIVERS = {
     Provider.DUMMY:
@@ -69,8 +69,9 @@ DRIVERS = {
 
 
 def get_driver(provider):
-    return get_provider_driver(DRIVERS, provider)
+    return _get_provider_driver(drivers=DRIVERS, provider=provider)
 
 
 def set_driver(provider, module, klass):
-    return set_provider_driver(DRIVERS, provider, module, klass)
+    return _set_provider_driver(drivers=DRIVERS, provider=provider,
+                                module=module, klass=klass)

http://git-wip-us.apache.org/repos/asf/libcloud/blob/2f73fdf4/libcloud/loadbalancer/providers.py
----------------------------------------------------------------------
diff --git a/libcloud/loadbalancer/providers.py b/libcloud/loadbalancer/providers.py
index f60e6d3..ef0f26b 100644
--- a/libcloud/loadbalancer/providers.py
+++ b/libcloud/loadbalancer/providers.py
@@ -13,9 +13,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from libcloud.utils.misc import get_driver as get_provider_driver
-from libcloud.utils.misc import set_driver as set_provider_driver
 from libcloud.loadbalancer.types import Provider
+from libcloud.common.providers import get_driver as _get_provider_driver
+from libcloud.common.providers import set_driver as _set_provider_driver
 
 __all__ = [
     "Provider",
@@ -54,8 +54,9 @@ DRIVERS = {
 
 
 def get_driver(provider):
-    return get_provider_driver(DRIVERS, provider)
+    return _get_provider_driver(drivers=DRIVERS, provider=provider)
 
 
 def set_driver(provider, module, klass):
-    return set_provider_driver(DRIVERS, provider, module, klass)
+    return _set_provider_driver(drivers=DRIVERS, provider=provider,
+                                module=module, klass=klass)

http://git-wip-us.apache.org/repos/asf/libcloud/blob/2f73fdf4/libcloud/storage/providers.py
----------------------------------------------------------------------
diff --git a/libcloud/storage/providers.py b/libcloud/storage/providers.py
index 1d38558..0eaaf8c 100644
--- a/libcloud/storage/providers.py
+++ b/libcloud/storage/providers.py
@@ -13,9 +13,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from libcloud.utils.misc import get_driver as get_provider_driver
-from libcloud.utils.misc import set_driver as set_provider_driver
 from libcloud.storage.types import Provider
+from libcloud.common.providers import get_driver as _get_provider_driver
+from libcloud.common.providers import set_driver as _set_provider_driver
 
 DRIVERS = {
     Provider.DUMMY:
@@ -74,8 +74,9 @@ DRIVERS = {
 
 
 def get_driver(provider):
-    return get_provider_driver(DRIVERS, provider)
+    return _get_provider_driver(drivers=DRIVERS, provider=provider)
 
 
 def set_driver(provider, module, klass):
-    return set_provider_driver(DRIVERS, provider, module, klass)
+    return _set_provider_driver(drivers=DRIVERS, provider=provider,
+                                module=module, klass=klass)

http://git-wip-us.apache.org/repos/asf/libcloud/blob/2f73fdf4/libcloud/test/compute/test_rackspace.py
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/test_rackspace.py b/libcloud/test/compute/test_rackspace.py
index d5e46cb..581b198 100644
--- a/libcloud/test/compute/test_rackspace.py
+++ b/libcloud/test/compute/test_rackspace.py
@@ -51,6 +51,7 @@ class RackspaceusFirstGenUsTests(OpenStack_1_0_Tests):
                 get_driver(provider)
             except Exception:
                 e = sys.exc_info()[1]
+                print str(e)
                 self.assertTrue(str(e).find('has been removed') != -1)
             else:
                 self.fail('Exception was not thrown')

http://git-wip-us.apache.org/repos/asf/libcloud/blob/2f73fdf4/libcloud/utils/misc.py
----------------------------------------------------------------------
diff --git a/libcloud/utils/misc.py b/libcloud/utils/misc.py
index 01acba1..c6d6f7b 100644
--- a/libcloud/utils/misc.py
+++ b/libcloud/utils/misc.py
@@ -24,6 +24,8 @@ from functools import wraps
 
 from libcloud.utils.py3 import httplib
 from libcloud.common.exceptions import RateLimitReachedError
+from libcloud.common.providers import get_driver as _get_driver
+from libcloud.common.providers import set_driver as _set_driver
 
 __all__ = [
     'find',
@@ -65,49 +67,10 @@ def find(l, predicate):
     return results[0] if len(results) > 0 else None
 
 
-def get_driver(drivers, provider):
-    """
-    Get a driver.
-
-    :param drivers: Dictionary containing valid providers.
-    :param provider: Id of provider to get driver
-    :type provider: :class:`libcloud.types.Provider`
-    """
-    if provider in drivers:
-        mod_name, driver_name = drivers[provider]
-        _mod = __import__(mod_name, globals(), locals(), [driver_name])
-        return getattr(_mod, driver_name)
-
-    raise AttributeError('Provider %s does not exist' % (provider))
-
-
-def set_driver(drivers, provider, module, klass):
-    """
-    Sets a driver.
-
-    :param drivers: Dictionary to store providers.
-    :param provider: Id of provider to set driver for
-    :type provider: :class:`libcloud.types.Provider`
-    :param module: The module which contains the driver
-    :type module: L
-    :param klass: The driver class name
-    :type klass:
-    """
-
-    if provider in drivers:
-        raise AttributeError('Provider %s already registered' % (provider))
-
-    drivers[provider] = (module, klass)
-
-    # Check if this driver is valid
-    try:
-        driver = get_driver(drivers, provider)
-    except (ImportError, AttributeError):
-        exp = sys.exc_info()[1]
-        drivers.pop(provider)
-        raise exp
-
-    return driver
+# Note: Those are aliases for backward-compatibility for functions which have
+# been moved to "libcloud.common.providers" module
+get_driver = _get_driver
+set_driver = _set_driver
 
 
 def merge_valid_keys(params, valid_keys, extra):


Mime
View raw message