From d5be3e42d5ea5e2ea97e8b490a70996abe45ad34 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Tue, 6 Sep 2011 12:31:28 +0200 Subject: [PATCH] Use django-appconf. --- dbtemplates/__init__.py | 18 +----- dbtemplates/conf.py | 17 +++--- dbtemplates/utils/settings.py | 106 ---------------------------------- setup.py | 1 + 4 files changed, 11 insertions(+), 131 deletions(-) delete mode 100644 dbtemplates/utils/settings.py diff --git a/dbtemplates/__init__.py b/dbtemplates/__init__.py index 5a7cfda..458270e 100644 --- a/dbtemplates/__init__.py +++ b/dbtemplates/__init__.py @@ -1,16 +1,2 @@ -VERSION = (1, 2, 0, "f", 0) # following PEP 386 -DEV_N = None - - -def get_version(): - version = "%s.%s" % (VERSION[0], VERSION[1]) - if VERSION[2]: - version = "%s.%s" % (version, VERSION[2]) - if VERSION[3] != "f": - version = "%s%s%s" % (version, VERSION[3], VERSION[4]) - if DEV_N: - version = "%s.dev%s" % (version, DEV_N) - return version - - -__version__ = get_version() +# following PEP 386, versiontools will pick it up +__version__ = (1, 2, 1, "final", 0) \ No newline at end of file diff --git a/dbtemplates/conf.py b/dbtemplates/conf.py index f245983..e1075a2 100644 --- a/dbtemplates/conf.py +++ b/dbtemplates/conf.py @@ -1,11 +1,12 @@ import posixpath from django.core.exceptions import ImproperlyConfigured +from django.conf import settings -from dbtemplates.utils.settings import AppSettings +from appconf import AppConf -class DbTemplatesSettings(AppSettings): +class DbTemplatesConf(AppConf): USE_CODEMIRROR = False USE_REVERSION = False ADD_DEFAULT_SITE = True @@ -15,16 +16,16 @@ class DbTemplatesSettings(AppSettings): def configure_media_prefix(self, value): if value is None: - base_url = getattr(self, "STATIC_URL", None) + base_url = getattr(settings, "STATIC_URL", None) if base_url is None: - base_url = self.MEDIA_URL + base_url = settings.MEDIA_URL value = posixpath.join(base_url, "dbtemplates/") return value def configure_cache_backend(self, value): # If we are on Django 1.3 AND using the new CACHES setting.. - if hasattr(self, "CACHES"): - if "dbtemplates" in self.CACHES: + if hasattr(settings, "CACHES"): + if "dbtemplates" in settings.CACHES: return "dbtemplates" else: return "default" @@ -35,9 +36,7 @@ class DbTemplatesSettings(AppSettings): return value def configure_use_reversion(self, value): - if value and 'reversion' not in self.INSTALLED_APPS: + if value and 'reversion' not in settings.INSTALLED_APPS: raise ImproperlyConfigured("Please add 'reversion' to your " "INSTALLED_APPS setting to make use of it in dbtemplates.") return value - -settings = DbTemplatesSettings("DBTEMPLATES") diff --git a/dbtemplates/utils/settings.py b/dbtemplates/utils/settings.py deleted file mode 100644 index 0431888..0000000 --- a/dbtemplates/utils/settings.py +++ /dev/null @@ -1,106 +0,0 @@ -from inspect import getmembers -from django.conf import settings - - -class AppSettings(object): - """ - An app setting object to be used for handling app setting defaults - gracefully and providing a nice API for them. 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:: - - from path.to.utils import AppSettings - - class MyAppSettings(AppSettings): - SETTING_1 = "one" - SETTING_2 = ( - "two", - ) - - Then initialize the setting with the correct prefix in the location of - of your choice, e.g. ``conf.py`` of the app module:: - - settings = MyAppSettings(prefix="MYAPP") - - The ``MyAppSettings`` instance will automatically look at Django's - global setting to determine each of the settings and respect the - provided ``prefix``. E.g. adding this to your site's ``settings.py`` - will set the ``SETTING_1`` setting accordingly:: - - MYAPP_SETTING_1 = "uno" - - Usage - ----- - - Instead of using ``from django.conf import settings`` as you would - usually do, you can switch to using your apps own settings module - to access the app settings:: - - from myapp.conf import settings - - print myapp_settings.MYAPP_SETTING_1 - - ``AppSettings`` instances also work as pass-throughs for other - global settings that aren't related to the app. For example the - following code is perfectly valid:: - - from myapp.conf import settings - - if "myapp" in settings.INSTALLED_APPS: - print "yay, myapp is installed!" - - Custom handling - --------------- - - Each of the settings can be individually configured with callbacks. - 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:: - - from django.conf import settings - - class MyCustomAppSettings(AppSettings): - ENABLED = True - - def configure_enabled(self, value): - return value and not self.DEBUG - - custom_settings = MyCustomAppSettings("MYAPP") - - The value of ``custom_settings.MYAPP_ENABLED`` will vary depending on the - value of the global ``DEBUG`` setting. - - Each of the app settings can be customized by providing - a method ``configure_`` 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. - """ - def __dir__(self): - return sorted(list(set(self.__dict__.keys() + dir(settings)))) - - __members__ = lambda self: self.__dir__() - - def __getattr__(self, name): - if name.startswith(self._prefix): - raise AttributeError("%r object has no attribute %r" % - (self.__class__.__name__, name)) - return getattr(settings, name) - - def __setattr__(self, name, value): - super(AppSettings, self).__setattr__(name, value) - if name in dir(settings): - setattr(settings, name, value) - - def __init__(self, prefix): - super(AppSettings, self).__setattr__('_prefix', prefix) - for setting, class_value in getmembers(self.__class__): - if setting == setting.upper(): - prefixed = "%s_%s" % (prefix.upper(), setting.upper()) - configured_value = getattr(settings, prefixed, class_value) - callback_name = "configure_%s" % setting.lower() - callback = getattr(self, callback_name, None) - if callable(callback): - configured_value = callback(configured_value) - delattr(self.__class__, setting) - setattr(self, prefixed, configured_value) diff --git a/setup.py b/setup.py index 7ecb97b..37bb161 100644 --- a/setup.py +++ b/setup.py @@ -30,4 +30,5 @@ setup( 'Programming Language :: Python :: 2.7', 'Framework :: Django', ], + install_requires=['django-appconf >= 0.4'], )