# HG changeset patch # User Jeff Hammel # Date 1332805235 25200 # Node ID c6d966431498491ea757597c2724d41f30462d75 # Parent 4fd88b1b08d5a020bf60b3922283e5de61da9ff1 add serialization details diff -r 4fd88b1b08d5 -r c6d966431498 configuration/config.py --- 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)