diff 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
line wrap: on
line diff
--- a/piecrust/configuration.py	Thu Mar 03 08:19:28 2016 -0800
+++ b/piecrust/configuration.py	Thu Mar 03 08:22:41 2016 -0800
@@ -28,28 +28,15 @@
 
     def __getitem__(self, key):
         self._ensureLoaded()
-        bits = key.split('/')
-        cur = self._values
-        for b in bits:
-            try:
-                cur = cur[b]
-            except KeyError:
-                raise KeyError("No such item: %s" % key)
-        return cur
+        try:
+            return get_dict_value(self._values, key)
+        except KeyError:
+            raise KeyError("No such item: %s" % key)
 
     def __setitem__(self, key, value):
         self._ensureLoaded()
         value = self._validateValue(key, value)
-        bits = key.split('/')
-        bitslen = len(bits)
-        cur = self._values
-        for i, b in enumerate(bits):
-            if i == bitslen - 1:
-                cur[b] = value
-            else:
-                if b not in cur:
-                    cur[b] = {}
-                cur = cur[b]
+        set_dict_value(self._values, key, value)
 
     def __delitem__(self, key):
         raise NotImplementedError()
@@ -129,6 +116,27 @@
         return value
 
 
+def get_dict_value(d, key):
+    bits = key.split('/')
+    cur = d
+    for b in bits:
+        cur = cur[b]
+    return cur
+
+
+def set_dict_value(d, key, value):
+    bits = key.split('/')
+    bitslen = len(bits)
+    cur = d
+    for i, b in enumerate(bits):
+        if i == bitslen - 1:
+            cur[b] = value
+        else:
+            if b not in cur:
+                cur[b] = {}
+            cur = cur[b]
+
+
 def merge_dicts(source, merging, validator=None, *args):
     _recurse_merge_dicts(source, merging, None, validator)
     for other in args: