diff --git a/tests/test_conf.py b/tests/test_conf.py index 5612756..650ef29 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + import unittest import tempfile import shutil @@ -69,6 +71,14 @@ def test_json_read(self): a = conf.Conf(name) self.assertEqual(a.read(), {"1": "cc"}) + def test_json_read_utf8(self): + """Check we can read an UTF-8 encoded JSON file.""" + name = "%s/test.json" % self.tmpdir + with open(name, "w") as f: + f.write('["😍", 123]') + a = conf.Conf(name) + self.assertEqual(a.read(), ['😍'.decode('utf-8'), 123]) + def test_json_read_not_exist(self): """Check we get `None` when asking for an inexistant file.""" name = "%s/test.json" % self.tmpdir @@ -236,6 +246,15 @@ def test_php_write_boolean(self): with open(name) as f: self.assertIn('false', f.read()) + def test_php_utf8_encoded(self): + """Check we get UTF-8 encoded output.""" + name = "%s/test.php" % self.tmpdir + a = conf.Conf(name, "php") + a.write(["ascii", "😍".decode("utf-8")]) + with open(name) as f: + result = f.read() + self.assertIn('"\xf0\x9f\x98\x8d"', result) + class TestConfDir(TempDirectoryTestCase): def test_dir_from_existence(self): diff --git a/zkfarmer/conf.py b/zkfarmer/conf.py index cf427b8..5776882 100644 --- a/zkfarmer/conf.py +++ b/zkfarmer/conf.py @@ -117,33 +117,34 @@ def write(self, obj): class ConfPHP(ConfFile): - meta = {'"': '\\"', "\0": "\\\0", "\n": "\\n", "\\": "\\\\"} - indent = ' ' + meta = {u'"': u'\\"', u"\0": u"\\\0", u"\n": u"\\n", u"\\": u"\\\\"} + indent = u' ' def _quotemeta(self, value): - return ''.join(self.meta.get(c, c) for c in value) + return u''.join(self.meta.get(c, c) for c in value) def _dump(self, value, lvl=0): if type(value) == int: return value elif isinstance(value, (str, unicode)): - return '"%s"' % self._quotemeta(value) + return u'"%s"' % self._quotemeta(value) elif type(value) == bool: if value: - return 'true' - return 'false' + return u'true' + return u'false' elif type(value) == dict: indent = lvl * self.indent - body = ',\n'.join(['%s"%s" => %s' % (indent + self.indent, self._quotemeta(key), self._dump(val, lvl + 1)) for key, val in value.items()]) - return 'array\n%s(\n%s\n%s)' % (indent, body, indent) + body = u',\n'.join([u'%s"%s" => %s' % (indent + self.indent, self._quotemeta(key), self._dump(val, lvl + 1)) for key, val in value.items()]) + return u'array\n%s(\n%s\n%s)' % (indent, body, indent) elif type(value) == list: - return 'array(%s)' % ','.join([str(self._dump(val)) for val in value]) + return u'array(%s)' % ','.join([unicode(self._dump(val)) for val in value]) else: raise TypeError('php_dump: cannot serialize value: %s' % type(value)) def write(self, obj): with self.open(write=True) as fd: - fd.write('