瀏覽代碼

added yaml_represent_unicode_as_str() and register_yaml_unicode_as_str_representer()

Fabian Peter Hammerle 9 年之前
父節點
當前提交
33d82af19a
共有 3 個文件被更改,包括 37 次插入4 次删除
  1. 6 0
      ioex/__init__.py
  2. 0 4
      ioex/datetimeex.py
  3. 31 0
      tests/test_yaml_represent_unicode_as_str.py

+ 6 - 0
ioex/__init__.py

@@ -43,3 +43,9 @@ def int_input_with_default(prompt, default):
         return int(s)
     else:
         return None
+
+def yaml_represent_unicode_as_str(dumper, unicode_string):
+    return dumper.represent_scalar(u'tag:yaml.org,2002:str', unicode_string)
+
+def register_yaml_unicode_as_str_representer(dumper):
+    dumper.add_representer(unicode, yaml_represent_unicode_as_str)

+ 0 - 4
ioex/datetimeex.py

@@ -2,10 +2,6 @@ import datetime
 import dateutil.parser
 import dateutil.tz.tz
 import re
-try:
-    import yaml
-except ImportError:
-    yaml = None
 
 def construct_yaml_timestamp(loader, node):
     loaded_dt = loader.construct_yaml_timestamp(node)

+ 31 - 0
tests/test_yaml_represent_unicode_as_str.py

@@ -0,0 +1,31 @@
+# -*- coding: utf-8 -*-
+import pytest
+
+import copy
+import ioex.datetimeex
+yaml = pytest.importorskip('yaml')
+
+@pytest.mark.parametrize(('dumper'), [yaml.Dumper, yaml.SafeDumper])
+@pytest.mark.parametrize(('unicode_string', 'expected_yaml_string', 'dump_params'), [
+    [[u'item'], '[item]\n', {}],
+    [[u'itäm'], '["it\\xE4m"]\n', {'allow_unicode': False}],
+    [[u'itäm'], '[itäm]\n', {'allow_unicode': True}],
+    [[u'itäm'], u'[itäm]\n'.encode('utf-8'), {'allow_unicode': True}],
+    [{u'key': u'value'}, '{key: value}\n', {}],
+    [{u'kï': u'valü'}, '{"k\\xEF": "val\\xFC"}\n', {'allow_unicode': False}],
+    [{u'kï': u'valü'}, '{kï: valü}\n', {'allow_unicode': True}],
+    [{u'kï': u'valü'}, u'{kï: valü}\n'.encode('utf-8'), {'allow_unicode': True}],
+    [{u'kĕyĭ': u'可以'}, '{kĕyĭ: 可以}\n', {'allow_unicode': True}],
+    [{u'⚕': u'☤'}, '{⚕: ☤}\n', {'allow_unicode': True}],
+    ])
+def test_to_yaml(unicode_string, expected_yaml_string, dump_params, dumper):
+    dumper_copy = copy.deepcopy(dumper)
+    ioex.register_yaml_unicode_as_str_representer(dumper_copy)
+    generated_yaml_string = yaml.dump(
+            unicode_string,
+            Dumper = dumper_copy,
+            default_flow_style = True,
+            **dump_params
+            )
+    assert type(expected_yaml_string) == type(generated_yaml_string)
+    assert expected_yaml_string == generated_yaml_string