Skip to content

Commit

Permalink
Merge branch 'release/0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
jezdez committed Aug 22, 2011
2 parents d7fc541 + ed030ad commit d7b3e6f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
45 changes: 41 additions & 4 deletions README.rst
Expand Up @@ -6,9 +6,9 @@ defaults of packaged apps gracefully. Say you have an app called ``myapp``
and want to define a few defaults, and refer to the defaults easily in the
apps code. Add a ``settings.py`` to your app's models.py::

import appconf
from appconf import AppConf

class MyAppConf(appconf.AppConf):
class MyAppConf(AppConf):
SETTING_1 = "one"
SETTING_2 = (
"two",
Expand Down Expand Up @@ -70,10 +70,10 @@ For example, in case a value of a setting depends on other settings
or other dependencies. The following example sets one setting to a
different value depending on a global setting::

import appconf
from django.conf import settings
from appconf import AppConf

class MyCustomAppConf(appconf.AppConf):
class MyCustomAppConf(AppConf):
ENABLED = True

def configure_enabled(self, value):
Expand All @@ -87,3 +87,40 @@ a method ``configure_<lower_setting_name>`` that takes the default
value as defined in the class attributes as the only parameter.
The method needs to return the value to be use for the setting in
question.

After each of the ``_configure`` method have be called, the ``AppConf``
class will additionally call a main ``configure`` method, which can
be used to do any further custom configuration handling, e.g. if multiple
settings depend on each other. For that a ``configured_data`` dictionary
is provided in the setting instance::


from django.conf import settings
from appconf import AppConf

class MyCustomAppConf(AppConf):
ENABLED = True
MODE = 'development'

def configure_enabled(self, value):
return value and not self.DEBUG

def configure(self):
mode = self.configured_data['MODE']
enabled = self.configured_data['ENABLED']
if not enabled and mode != 'development':
print "WARNING: app not enabled in %s mode!" % mode

Changelog
---------

0.2 (2011-08-22)
^^^^^^^^^^^^^^^^

* Added ``configure()`` API to ``AppConf`` class which is called after
configuring each setting.

0.1 (2011-08-22)
^^^^^^^^^^^^^^^^

* First public release.
20 changes: 15 additions & 5 deletions appconf.py
@@ -1,7 +1,7 @@
import sys

# following PEP 386, versiontools will pick it up
__version__ = (0, 1, 0, "final", 0)
__version__ = (0, 2, 0, "final", 0)


class AppConfOptions(object):
Expand Down Expand Up @@ -47,21 +47,25 @@ def __new__(cls, name, bases, attrs):

new_class.defaults = dict(defaults)
new_class.names = dict(names)
new_class.configured_data = dict()
new_class._configure()

def _configure(cls):
if not cls._meta.configured:
# the ad-hoc settings class instance used to configure each value
obj = cls()
from django.conf import settings
for name, prefixed_name in obj.names.items():
obj = cls()
for name, prefixed_name in obj.names.iteritems():
default_value = obj.defaults.get(prefixed_name)
value = getattr(settings, prefixed_name, default_value)
callback = getattr(obj, "configure_%s" % name.lower(), None)
if callable(callback):
value = callback(value)
# Finally, set the setting in the global setting object
setattr(settings, prefixed_name, value)
obj.configured_data[name] = value
obj.configured_data = obj.configure()
# Finally, set the setting in the global setting object
for name, value in obj.configured_data.iteritems():
setattr(settings, obj.names[name], value)
cls._meta.configured = True


Expand Down Expand Up @@ -90,3 +94,9 @@ def __getattr__(self, name):

def __setattr__(self, name, value):
setattr(self.__dict__['_holder'], name, value)

def configure(self):
"""
Hook for doing any extra configuration.
"""
return self.configured_data

0 comments on commit d7b3e6f

Please sign in to comment.