Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
conf: ensure we can encode unicode to PHP
Also, add a test that we are able to decode an UTF-8 encoded JSON
file.
  • Loading branch information
Vincent Bernat committed Oct 4, 2013
1 parent 1f01b33 commit a406b9a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
19 changes: 19 additions & 0 deletions tests/test_conf.py
@@ -1,3 +1,5 @@
# -*- encoding: utf-8 -*-

import unittest
import tempfile
import shutil
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
21 changes: 11 additions & 10 deletions zkfarmer/conf.py
Expand Up @@ -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('<?php return %s;' % self._dump(obj))
php = u'<?php return %s;' % self._dump(obj)
fd.write(php.encode("utf-8", errors="ignore"))


class ConfDir(ConfFile):
Expand Down

0 comments on commit a406b9a

Please sign in to comment.