summaryrefslogtreecommitdiffstats
path: root/forum/conf/settings_wrapper.py
blob: 305c0fd30bcfad427c6ae52db89875759a80e335 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Definition of a Singleton wrapper class for forum.deps.livesettings
with interface similar to django.conf.settings
that is each setting has unique key and is accessible
via dotted lookup.

for example to lookup value of setting BLAH you would do

from forum.conf import settings as forum_settings

forum_settings.BLAH

NOTE that at the moment there is distinction between settings
(django settings) and forum_settings (forum.deps.livesettings)

the value will be taken from forum.deps.livesettings database or cache
note that during compilation phase database is not accessible
for the most part, so actual values are reliably available only 
at run time

forum.deps.livesettings is a module developed for satchmo project
"""
from forum.deps.livesettings import SortedDotDict, config_register

class ConfigSettings(object):
    """A very simple Singleton wrapper for settings
    a limitation is that all settings names using this class
    must be distinct, even though they might belong
    to different settings groups
    """
    __instance = None

    def __init__(self):
        """assigns SortedDotDict to self.__instance if not set"""
        if ConfigSettings.__instance == None:
            ConfigSettings.__instance = SortedDotDict()
        self.__dict__['_ConfigSettings__instance'] = ConfigSettings.__instance
        self.__ordering_index = {}

    def __getattr__(self, key):
        """value lookup returns the actual value of setting
        not the object - this way only very minimal modifications
        will be required in code to convert an app
        depending on django.conf.settings to forum.deps.livesettings
        """
        return getattr(self.__instance, key).value

    def register(self, value):
        """registers the setting
        value must be a subclass of forum.deps.livesettings.Value
        """
        key = value.key
        group_key = value.group.key

        ordering = self.__ordering_index.get(group_key, None)
        if ordering:
            ordering += 1
            value.ordering = ordering
        else:
            ordering = 1
            value.ordering = ordering
        self.__ordering_index[group_key] = ordering

        if key in self.__instance:
            raise Exception('setting %s is already registered' % key)
        else:
            self.__instance[key] = config_register(value)

    def as_dict(self):
        out = dict()
        for key in self.__instance.keys():
            #todo: this is odd that I could not use self.__instance.items() mapping here
            out[key] = self.__instance[key].value
        return out

#settings instance to be used elsewhere in the project
settings = ConfigSettings()