Selaa lähdekoodia

Ensure consistency of variable type when read from config file or dict

This ensures 'unicode' type is used for py2 and 'str' for py3.

Partially addresses #140 (os.environ.get's still todo as they are str in py2).
Enis Afgan 7 vuotta sitten
vanhempi
sitoutus
ea614d3982
1 muutettua tiedostoa jossa 10 lisäystä ja 4 poistoa
  1. 10 4
      cloudbridge/cloud/base/provider.py

+ 10 - 4
cloudbridge/cloud/base/provider.py

@@ -8,6 +8,8 @@ try:
 except ImportError:  # Python 2
     from ConfigParser import SafeConfigParser as ConfigParser
 
+import six
+
 from cloudbridge.cloud.interfaces import CloudProvider
 from cloudbridge.cloud.interfaces.exceptions import ProviderConnectionException
 from cloudbridge.cloud.interfaces.resources import Configuration
@@ -150,11 +152,15 @@ class BaseCloudProvider(CloudProvider):
         """
         log.debug("Getting config key %s, with supplied default value: %s",
                   key, default_value)
+        value = default_value
         if isinstance(self.config, dict) and self.config.get(key):
-            return self.config.get(key, default_value)
+            value = self.config.get(key, default_value)
         elif hasattr(self.config, key) and getattr(self.config, key):
-            return getattr(self.config, key)
+            value = getattr(self.config, key)
         elif (self._config_parser.has_option(self.PROVIDER_ID, key) and
               self._config_parser.get(self.PROVIDER_ID, key)):
-            return self._config_parser.get(self.PROVIDER_ID, key)
-        return default_value
+            value = self._config_parser.get(self.PROVIDER_ID, key)
+        if isinstance(value, six.string_types) and not isinstance(
+                value, six.text_type):
+            return six.u(value)
+        return value