Created
September 19, 2012 16:47
-
-
Save akx/3750718 to your computer and use it in GitHub Desktop.
Django configuration framework idea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This would be your settings module (settings/__init__.py) | |
class Setup(object): | |
def __init__(self, load_from=None): | |
self.commit(load_from) | |
def is_valid_key(self, key): | |
return (key == key.upper() and not key.startswith("_")) | |
def commit(self, source): | |
if source: | |
if not hasattr(source, "iteritems"): | |
source = vars(source) | |
for key, value in source.iteritems(): | |
if self.is_valid_key(key): | |
setattr(self, key, value) | |
def values(self): | |
for key, value in self.__dict__.iteritems(): | |
if self.is_valid_key(key): | |
yield (key, value) | |
def configure(): | |
setup = Setup() | |
from projectname.settings.global_settings import global_configure | |
from projectname.settings.site_settings import site_configure | |
global_configure(setup) | |
site_configure(setup) | |
try: | |
from projectname.settings import local_settings | |
setup.commit(local_settings) | |
except ImportError: | |
pass | |
return setup.values() | |
globals().update(configure()) | |
del configure, Setup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def global_configure(setup): | |
TIME_ZONE = "Europe/Turku" | |
LANGUAGES = (("fi", "Suomi"),) | |
setup.commit(locals()) # put locals (only CONSTANT_LIKE values) into setup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OLD_SCHOOL = "y35!" # This will also work (Setup.commit() is able to read modules just as well) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def global_configure(setup): | |
LANGUAGES = getattr(setup, "LANGUAGES", ()) + (("en", "English"),) # Can read values from setup: | |
DERP = True # and set new ones like usual | |
setup.commit(locals()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment