changeset 27:c6d966431498

add serialization details
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 26 Mar 2012 16:40:35 -0700
parents 4fd88b1b08d5
children c516ab813079
files configuration/config.py
diffstat 1 files changed, 25 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/configuration/config.py	Mon Mar 26 16:10:15 2012 -0700
+++ b/configuration/config.py	Mon Mar 26 16:40:35 2012 -0700
@@ -31,24 +31,34 @@
     abstract base class for configuration providers for
     serialization/deserialization
     """
+    def read(self, filename):
+        raise NotImplementedError("Abstract base class")
+
+    def write(self, config, filename):
+        if isinstance(filename, basestring):
+            f = file(filename, 'w')
+            newfile = True
+        else:
+            f = filename
+            newfile = False
+        try:
+            self._write(f, config)
+        finally:
+            # XXX try: finally: works in python >= 2.5
+            if newfile:
+                f.close()
+    def _write(self, fp, config):
+        raise NotImplementedError("Abstract base class")
 
 if json:
     class JSON(object):
+        indent = 2
         extensions = ['json']
         def read(self, filename):
             return json.loads(file(filename).read())
-        def write(self, config, filename):
-            if isinstance(filename, basestring):
-                f = file(filename, 'w')
-                newfile = True
-            else:
-                f = filename
-                newfile = False
-            try:
-                f.write(json.dumps(config))
-            finally:
-                if newfile:
-                    f.close()
+        def _write(self, fp, config):
+            fp.write(json.dumps(config), indent=self.indent, sort_keys=True)
+            # TODO: could use templates to get order down, etc
     configuration_providers.append(JSON)
 
 if yaml:
@@ -59,6 +69,9 @@
             config = yaml.load(f)
             f.close()
             return config
+        def _write(self, fp, config):
+            fp.write(yaml.dump(config))
+            # TODO: could use templates to get order down, etc
 
     configuration_providers.append(YAML)