changeset 138:b540d431f2da

Make configuration class more like `dict`, add support for merging `dicts`.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 29 Nov 2014 15:32:19 -0800
parents 67a1c082d89d
children 2889d85b1d32
files piecrust/configuration.py
diffstat 1 files changed, 23 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/configuration.py	Sat Nov 29 15:31:26 2014 -0800
+++ b/piecrust/configuration.py	Sat Nov 29 15:32:19 2014 -0800
@@ -19,6 +19,18 @@
         else:
             self._values = None
 
+    def __contains__(self, key):
+        return self.has(key)
+
+    def __getitem__(self, key):
+        value = self.get(key)
+        if value is None:
+            raise KeyError()
+        return value
+
+    def __setitem__(self, key, value):
+        return self.set(key, value)
+
     def setAll(self, values, validate=True):
         if validate:
             self._validateAll(values)
@@ -65,8 +77,17 @@
 
     def merge(self, other):
         self._ensureLoaded()
-        merge_dicts(self._values, other._values,
-                validator=self._validateValue)
+
+        if isinstance(other, dict):
+            other_values = other
+        elif isinstance(other, Configuration):
+            other_values = other._values
+        else:
+            raise Exception(
+                    "Unsupported value type to merge: %s" % type(other))
+
+        merge_dicts(self._values, other_values,
+                    validator=self._validateValue)
 
     def _ensureLoaded(self):
         if self._values is None: