Explorar o código

added yaml_construct_str_as_unicode() and register_yaml_str_as_unicode_constructor()

Fabian Peter Hammerle %!s(int64=9) %!d(string=hai) anos
pai
achega
383a35d687
Modificáronse 2 ficheiros con 55 adicións e 0 borrados
  1. 7 0
      ioex/__init__.py
  2. 48 0
      tests/test_yaml_construct_str_as_unicode.py

+ 7 - 0
ioex/__init__.py

@@ -49,3 +49,10 @@ def yaml_represent_unicode_as_str(dumper, unicode_string):
 
 def register_yaml_unicode_as_str_representer(dumper):
     dumper.add_representer(unicode, yaml_represent_unicode_as_str)
+
+def yaml_construct_str_as_unicode(loader, node):
+    string = loader.construct_scalar(node)
+    return string if type(string) is unicode else string.decode('utf-8')
+
+def register_yaml_str_as_unicode_constructor(loader):
+    loader.add_constructor(u'tag:yaml.org,2002:str', yaml_construct_str_as_unicode)

+ 48 - 0
tests/test_yaml_construct_str_as_unicode.py

@@ -0,0 +1,48 @@
+# -*- coding: utf-8 -*-
+import pytest
+
+import copy
+import ioex.datetimeex
+yaml = pytest.importorskip('yaml')
+
+@pytest.mark.parametrize(('loader'), [yaml.Loader, yaml.SafeLoader])
+@pytest.mark.parametrize(('yaml_string', 'expected_object'), [
+    # ascii strings
+    #
+    # nota bene:
+    # >>> yaml.dump('it\xc3\xa4m', allow_unicode = False) == '!!python/str "it\\xE4m"\n'
+    # True
+    # >>> yaml.dump('it\xc3\xa4m', allow_unicode = True) == "!!python/str 'it\xc3\xa4m'\n"
+    # True
+    ['item', u'item'],
+    ['itäm', u'itäm'],
+    ['it\xc3\xa4m', u'itäm'],
+    ['"it\xc3\xa4m"', u'itäm'],
+    [r'it\xE4m', ur'it\xE4m'],
+    ['"itäm"', u'itäm'],
+    [r'"it\xc3\xa4m"', u'it\xc3\xa4m'], # see comment above
+    # unicode strings
+    [r'"it\xE4m"', u'it\xE4m'],
+    [r'"it\xE4m"', u'itäm'],
+    [u'item', u'item'],
+    [u'itäm', u'itäm'],
+    ['{kĕyĭ: 可以}\n', {u'kĕyĭ': u'可以'}],
+    ['{⚕: ☤}\n', {u'⚕': u'☤'}],
+    # lists
+    ['[item]', [u'item']],
+    ['[itäm]', [u'itäm']],
+    # dicts
+    ['{key: value}', {u'key': u'value'}],
+    ['{kï: valü}', {u'kï': u'valü'}],
+    ])
+def test_to_yaml(yaml_string, expected_object, loader):
+    loader_copy = copy.deepcopy(loader)
+    ioex.register_yaml_str_as_unicode_constructor(loader_copy)
+    generated_object = yaml.load(yaml_string, Loader = loader_copy)
+    assert type(expected_object) == type(generated_object)
+    assert expected_object == generated_object
+    if type(expected_object) is list:
+        assert [type(i) for i in expected_object] == [type(i) for i in generated_object]
+    elif type(expected_object) is dict:
+        assert [type(k) for k in expected_object.keys()] == [type(k) for k in generated_object.keys()]
+        assert [type(v) for v in expected_object.values()] == [type(v) for v in generated_object.values()]