configglue team mailing list archive
-
configglue team
-
Mailing list archive
-
Message #00068
[Merge] lp:~ricardokirkner/configglue/unicode-save into lp:configglue
Ricardo Kirkner has proposed merging lp:~ricardokirkner/configglue/unicode-save into lp:configglue.
Requested reviews:
Configglue developers (configglue)
For more details, see:
https://code.launchpad.net/~ricardokirkner/configglue/unicode-save/+merge/59672
Override ConfigParser.write in SchemaConfigParser, to support writing non-ascii data.
--
https://code.launchpad.net/~ricardokirkner/configglue/unicode-save/+merge/59672
Your team Configglue developers is requested to review the proposed merge of lp:~ricardokirkner/configglue/unicode-save into lp:configglue.
=== modified file 'configglue/pyschema/parser.py'
--- configglue/pyschema/parser.py 2011-03-26 22:24:51 +0000
+++ configglue/pyschema/parser.py 2011-05-02 16:25:57 +0000
@@ -521,6 +521,23 @@
filename = self.locate(option)
self._dirty[filename][section][option] = value
+ def write(self, fp):
+ """Write an .ini-format representation of the configuration state."""
+ if self._defaults:
+ fp.write("[%s]\n" % DEFAULTSECT)
+ for (key, value) in self._defaults.items():
+ fp.write("%s = %s\n" % (key, value.replace('\n', '\n\t')))
+ fp.write("\n")
+ for section in self._sections:
+ fp.write("[%s]\n" % section)
+ for (key, value) in self._sections[section].items():
+ if key == "__name__":
+ continue
+ if (value is not None) or (self._optcre == self.OPTCRE):
+ key = " = ".join((key, value.replace('\n', '\n\t')))
+ fp.write("%s\n" % (key))
+ fp.write("\n")
+
def save(self, fp=None):
"""Save the parser contents to a file.
=== modified file 'tests/pyschema/test_parser.py'
--- tests/pyschema/test_parser.py 2011-03-31 23:15:40 +0000
+++ tests/pyschema/test_parser.py 2011-05-02 16:25:57 +0000
@@ -21,6 +21,7 @@
import tempfile
import unittest
from ConfigParser import (
+ DEFAULTSECT,
InterpolationDepthError,
InterpolationMissingOptionError,
InterpolationSyntaxError,
@@ -718,21 +719,50 @@
self.assertEqual(self.parser._dirty,
{f.name: {'__main__': {'foo': '2'}}})
+ def test_write(self):
+ class MySchema(Schema):
+ foo = StringConfigOption()
+ DEFAULTSECT = ConfigSection()
+ parser = SchemaConfigParser(MySchema())
+ expected = u"[{0}]\nbaz = 2\n\n[__main__]\nfoo = bar".format(DEFAULTSECT)
+ config = StringIO(expected)
+ parser.readfp(config)
+
+ # create config file
+ fp, filename = tempfile.mkstemp()
+ try:
+ parser.write(open(filename, 'w'))
+ result = open(filename, 'r').read().strip()
+ self.assertEqual(result, expected)
+ finally:
+ # remove the file
+ os.unlink(filename)
+
def test_save_config(self):
- expected_output = u'[__main__]\nfoo = 42\n\n'
- config = StringIO(u'[__main__]\nfoo=42')
+ expected = u'[__main__]\nfoo = 42'
+ self._check_save_file(expected)
+
+ def test_save_config_non_ascii(self):
+ expected = u'[__main__]\nfoo = fóobâr'
+ self._check_save_file(expected)
+
+ def _check_save_file(self, expected):
+ config = StringIO(expected.encode(CONFIG_FILE_ENCODING))
self.parser.readfp(config)
# create config file
fp, filename = tempfile.mkstemp()
- self.parser.save(open(filename, 'w'))
- self.assertEqual(open(filename, 'r').read(), expected_output)
-
- self.parser.save(filename)
- self.assertEqual(open(filename, 'r').read(), expected_output)
-
- # remove the file
- os.unlink(filename)
+ try:
+ self.parser.save(open(filename, 'w'))
+ result = open(filename, 'r').read().strip()
+ self.assertEqual(result.decode(CONFIG_FILE_ENCODING), expected)
+
+ self.parser.save(filename)
+ result = open(filename, 'r').read().strip()
+ self.assertEqual(result.decode(CONFIG_FILE_ENCODING), expected)
+ finally:
+ # remove the file
+ os.unlink(filename)
def test_save_config_same_files(self):
def setup_config():
Follow ups