comparison piecrust/configuration.py @ 666:81d9c3a3a0b5

internal: Get rid of the whole "sub cache" business. * Compute cache keys up front, so the cache directory is only chosen once. * Buffer up config variants to apply before loading the config. Makes it possible to cache variant-resulting configs, too. * Make a factory class to reuse the logic that creates the `PieCrust` object correctly for multi-process workers and such. * Add a test.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 03 Mar 2016 08:22:41 -0800
parents 9ccc933ac2c7
children ec384174b8b2
comparison
equal deleted inserted replaced
665:5dc13c816045 666:81d9c3a3a0b5
26 else: 26 else:
27 self._values = None 27 self._values = None
28 28
29 def __getitem__(self, key): 29 def __getitem__(self, key):
30 self._ensureLoaded() 30 self._ensureLoaded()
31 bits = key.split('/') 31 try:
32 cur = self._values 32 return get_dict_value(self._values, key)
33 for b in bits: 33 except KeyError:
34 try: 34 raise KeyError("No such item: %s" % key)
35 cur = cur[b]
36 except KeyError:
37 raise KeyError("No such item: %s" % key)
38 return cur
39 35
40 def __setitem__(self, key, value): 36 def __setitem__(self, key, value):
41 self._ensureLoaded() 37 self._ensureLoaded()
42 value = self._validateValue(key, value) 38 value = self._validateValue(key, value)
43 bits = key.split('/') 39 set_dict_value(self._values, key, value)
44 bitslen = len(bits)
45 cur = self._values
46 for i, b in enumerate(bits):
47 if i == bitslen - 1:
48 cur[b] = value
49 else:
50 if b not in cur:
51 cur[b] = {}
52 cur = cur[b]
53 40
54 def __delitem__(self, key): 41 def __delitem__(self, key):
55 raise NotImplementedError() 42 raise NotImplementedError()
56 43
57 def __iter__(self): 44 def __iter__(self):
125 def _validateAll(self, values): 112 def _validateAll(self, values):
126 return values 113 return values
127 114
128 def _validateValue(self, key_path, value): 115 def _validateValue(self, key_path, value):
129 return value 116 return value
117
118
119 def get_dict_value(d, key):
120 bits = key.split('/')
121 cur = d
122 for b in bits:
123 cur = cur[b]
124 return cur
125
126
127 def set_dict_value(d, key, value):
128 bits = key.split('/')
129 bitslen = len(bits)
130 cur = d
131 for i, b in enumerate(bits):
132 if i == bitslen - 1:
133 cur[b] = value
134 else:
135 if b not in cur:
136 cur[b] = {}
137 cur = cur[b]
130 138
131 139
132 def merge_dicts(source, merging, validator=None, *args): 140 def merge_dicts(source, merging, validator=None, *args):
133 _recurse_merge_dicts(source, merging, None, validator) 141 _recurse_merge_dicts(source, merging, None, validator)
134 for other in args: 142 for other in args: