@@ -11,15 +11,76 @@ class RegisterError(Exception):
1111 pass
1212
1313
14+ class ConfigStorage (object ):
15+ """This is the backend which :class:`Environment` uses to store
16+ it's configuration values.
17+
18+ Environment-subclasses like the one used by ``django-assets`` will
19+ often want to use a custom ``ConfigStorage`` as well, building upon
20+ whatever configuration the framework is using.
21+
22+ The goal in designing this class therefore is to make it easy for
23+ subclasses to change the place the data is stored: Only
24+ _meth:`__getitem__`, _meth:`__setitem__` and _meth:`__delitem__`
25+ need to be implemented.
26+
27+ One rule: The default storage is case-insensitive, and custom
28+ environments should maintain those semantics.
29+
30+ A related reason is why we don't inherit from ``dict``. It would
31+ require us to re-implement a whole bunch of methods, like pop() etc.
32+ """
33+ # Don't inherit from ``dict``, as that would require us to implement
34+ # a whole bunch of methods, like pop() etc.
35+ def __init__ (self , d = {}):
36+ self ._dict = {}
37+ self .update (d )
38+
39+ def __contains__ (self , key ):
40+ return self ._dict .__contains__ (key .lower ())
41+
42+ def get (self , key , default = None ):
43+ try :
44+ return self .__getitem__ (key )
45+ except KeyError :
46+ return default
47+
48+ def update (self , d ):
49+ for key in d :
50+ self ._dict [key ] = d [key ]
51+
52+ def __getitem__ (self , key ):
53+ raise NotImplementedError ()
54+
55+ def __setitem__ (self , key , value ):
56+ raise NotImplementedError ()
57+
58+ def __delitem__ (self , key ):
59+ raise NotImplementedError ()
60+
61+
62+ class DictConfigStorage (ConfigStorage ):
63+ """Using a lower-case dict for configuration values.
64+ """
65+ def __getitem__ (self , key ):
66+ return self ._dict .__getitem__ (key .lower ())
67+ def __setitem__ (self , key , value ):
68+ self ._dict .__setitem__ (key .lower (), value )
69+ def __delitem__ (self , key ):
70+ self ._dict .__delitem__ (key .lower ())
71+
72+
1473class Environment (object ):
1574 """Owns a collection of bundles, and a set of configuration values
1675 which will be used when processing these bundles.
1776 """
1877
78+ config_storage_class = DictConfigStorage
79+
1980 def __init__ (self , directory , url , ** config ):
2081 self ._named_bundles = {}
2182 self ._anon_bundles = []
22- self ._config = config
83+ self ._config = self . config_storage_class ( config )
2384
2485 self .directory = directory
2586 self .url = url
@@ -84,19 +145,18 @@ def add(self, *bundles):
84145 self ._anon_bundles .append (bundle )
85146 bundle .env = self # take ownership
86147
87- def get_config ( self , key , default = None ):
88- """This is a simple configuration area provided by the asset
89- env which holds additional options used by the filters .
148+ @ property
149+ def config ( self ):
150+ """Key-value configuration. Keys are case-insensitive .
90151 """
91- return self ._config .get (key .lower (), default )
92-
93- def set_config (self , key , value ):
94- self ._config [key .lower ()] = value
152+ # This is a property so that user are not tempted to assign
153+ # a custom dictionary which won't uphold our caseless semantics.
154+ return self ._config
95155
96156 def set_debug (self , debug ):
97- self ._debug = debug
157+ self .config [ 'debug' ] = debug
98158 def get_debug (self ):
99- return self ._debug
159+ return self .config [ 'debug' ]
100160 debug = property (get_debug , set_debug , doc =
101161 """Enable/disable debug mode. Possible values are:
102162
@@ -110,9 +170,9 @@ def get_debug(self):
110170 """ )
111171
112172 def set_cache (self , enable ):
113- self ._cache = enable
173+ self .config [ 'cache' ] = enable
114174 def get_cache (self ):
115- return self ._cache
175+ return self .config [ 'cache' ]
116176 cache = property (get_cache , set_cache , doc =
117177 """Controls the behavior of the cache. The cache will speed up rebuilding
118178 of your bundles, by caching individual filter results. This can be
@@ -135,9 +195,9 @@ def get_cache(self):
135195 """ )
136196
137197 def set_updater (self , updater ):
138- self ._updater = updater
198+ self .config [ 'updater' ] = updater
139199 def get_updater (self ):
140- return self ._updater
200+ return self .config [ 'updater' ]
141201 updater = property (get_updater , set_updater , doc =
142202 """Controls when and if bundles should be automatically rebuilt.
143203 Possible values are:
@@ -167,9 +227,9 @@ def get_updater(self):
167227 """ )
168228
169229 def set_expire (self , expire ):
170- self ._expire = expire
230+ self .config [ 'expire' ] = expire
171231 def get_expire (self ):
172- return self ._expire
232+ return self .config [ 'expire' ]
173233 expire = property (get_expire , set_expire , doc =
174234 """If you send your assets to the client using a *far future expires*
175235 header (to minimize the 304 responses your server has to send), you
@@ -193,17 +253,17 @@ def get_expire(self):
193253 """ )
194254
195255 def set_directory (self , directory ):
196- self ._directory = directory
256+ self .config [ 'directory' ] = directory
197257 def get_directory (self ):
198- return self ._directory
258+ return self .config [ 'directory' ]
199259 directory = property (get_directory , set_directory , doc =
200260 """The base directory to which all paths will be relative to.
201261 """ )
202262
203263 def set_url (self , url ):
204- self ._url = url
264+ self .config [ 'url' ] = url
205265 def get_url (self ):
206- return self ._url
266+ return self .config [ 'url' ]
207267 url = property (get_url , set_url , doc =
208268 """The base used to construct urls under which :attr:`directory`
209269 should be exposed.
0 commit comments