Commits

Omer Katz committed 48d6919

Made the environment_settings module load settings from subclasses

  • Participants
  • Parent commits f31d8d7

Comments (0)

Files changed (9)

environment.py

-from django.utils.importlib import import_module
-import sys
-from inspect import ismodule
-
-class EnvironmentResolver(object):
-    def __init__(self, project_name):
-        self.project_name = project_name
-
-    def resolve_environment(self):
-        try:
-            environment = import_module('environment')
-
-            self.get_environment_settings(environment)
-        except:
-            self.settings = self.load_settings('settings')
-
-    def get_environment_settings(self, environment):
-        try:
-            environment_attribute = environment.getattr("ENVIRONMENT")
-            self.settings_module_name = environment_attribute() if callable(environment_attribute) else environment_attribute
-        except:
-            raise AttributeError("Could not find settings environment.")
-
-        self.settings = self.load_settings(self.settings_module_name)
-
-    def load_settings(self, module_name, environment = None):
-            try:
-                return import_module(module_name, self.project_name)
-            except ImportError:
-                message = "Could not import settings."
-
-                if environment:
-                    message += "from %s" % environment
-
-                raise ImportError(message)
-
-class EnvironmentLoader(object):
-    def load_environment(self):
-        pass

environment/__init__.py

Empty file added.

environment/environment_settings.py

+class SettingsBase(object):
+    def __init__(self):
+        self.overwrite = False
+        self.load_settings()
+
+    def load_settings(self):
+        for setting in self.get_current_settings():
+            self.set_setting(setting['attribute_name'],
+                             self.get_setting(setting['class'], setting['attribute_name']),
+                             setting['should_overwrite'])
+
+    def get_current_settings(self):
+        current_settings = []
+        for cls in self.__class__.__mro__:
+            for setting in dir(cls):
+                if not setting.startswith('__') and setting != 'overwrite' and not callable(self.__getattribute__(setting)):
+                    current_settings.append(
+                                                {
+                                                 'attribute_name': setting,
+                                                 'should_overwrite': cls.overwrite,
+                                                 'class': cls
+                                                 }
+                                            )
+        return current_settings
+
+    def set_setting(self, name, value, should_overwrite):
+        print name
+
+        if (not hasattr(self, name) or should_overwrite):
+            self.__setattr__(name, value)
+
+    def get_setting(self, cls, name):
+        return cls.__getattribute__(cls.__new__(cls), name)
+
+class A(SettingsBase):
+    FOO = 5
+
+    overwrite = True
+
+    def __init__(self):
+        super(A, self).__init__()
+
+class B(A):
+    FOO = 50
+
+    overwrite = False
+
+    def __init__(self):
+        super(B, self).__init__()
+

environment/loaders.py

+class EnvironmentLoader(object):
+    def load_environment(self):
+        pass

environment/resolvers.py

+from django.utils.importlib import import_module
+import sys
+from inspect import ismodule
+
+class EnvironmentResolver(object):
+    def __init__(self, project_name):
+        self.project_name = project_name
+
+    def resolve_environment(self):
+        try:
+            environment = import_module('environment')
+
+            self.get_environment_settings(environment)
+        except:
+            self.settings = self.load_settings('settings')
+
+    def get_environment_settings(self, environment):
+        try:
+            environment_attribute = environment.getattr("ENVIRONMENT")
+            self.settings_module_name = environment_attribute() if callable(environment_attribute) else environment_attribute
+        except:
+            raise AttributeError("Could not find settings environment.")
+
+        self.settings = self.load_settings(self.settings_module_name)
+
+    def load_settings(self, module_name, environment = None):
+            try:
+                return import_module(module_name, self.project_name)
+            except ImportError:
+                message = "Could not import settings."
+
+                if environment:
+                    message += "from %s" % environment
+
+                raise ImportError(message)

management/commands/startenv.py

             pass
         else:
             raise CommandError("%r conflicts with the name of an existing Python module and cannot be used as an environment name. Please try another name." % env_name)
-	os.mkdir(directory)
+	    os.mkdir(directory)
         utils.copy_helper(self.style, 'environment', env_name, directory)
 
 class ProjectCommand(Command):

tests.py

-import os
-
-from django.test import TestCase
-from environment import EnvironmentResolver
-
-class EnvironmentResolverTests(TestCase):
-
-    def setUp(self):
-        self.environment_resolver = EnvironmentResolver()
-        self.settings_suite = os.environ['SETTINGS_SUITE']
-
-    def test_load_settings(self):
-        self.environment_resolver.load_settings('settings')
-
-    def test_resolve_environment(self):
-        self.environment_resolver.resolve_environment()
-
-__test__ = {}

tests/__init__.py

Empty file added.
+import os
+
+from django.test import TestCase
+from envconf.environment import EnvironmentResolver
+
+class EnvironmentResolverTests(TestCase):
+
+    def setUp(self):
+        self.environment_resolver = EnvironmentResolver()
+        self.settings_suite = os.environ['SETTINGS_SUITE']
+
+    def test_load_settings(self):
+        self.environment_resolver.load_settings('settings')
+
+    def test_resolve_environment(self):
+        self.environment_resolver.resolve_environment()
+
+__test__ = {}