2021-04-01 14:01:14 +00:00
|
|
|
from future.utils import PY2
|
|
|
|
|
2016-07-20 00:22:12 +00:00
|
|
|
import yaml
|
|
|
|
|
2021-04-01 14:01:14 +00:00
|
|
|
if PY2:
|
|
|
|
# On Python 2 the way pyyaml deals with Unicode strings is inconsistent.
|
|
|
|
#
|
|
|
|
# >>> yaml.safe_load(yaml.safe_dump(u"hello"))
|
|
|
|
# 'hello'
|
|
|
|
# >>> yaml.safe_load(yaml.safe_dump(u"hello\u1234"))
|
|
|
|
# u'hello\u1234'
|
|
|
|
#
|
|
|
|
# In other words, Unicode strings get roundtripped to byte strings, but
|
|
|
|
# only sometimes.
|
|
|
|
#
|
|
|
|
# In order to ensure unicode stays unicode, we add a configuration saying
|
|
|
|
# that the YAML String Language-Independent Type ("a sequence of zero or
|
|
|
|
# more Unicode characters") should be the underlying Unicode string object,
|
|
|
|
# rather than converting to bytes when possible.
|
|
|
|
#
|
|
|
|
# Reference: https://yaml.org/type/str.html
|
|
|
|
def construct_unicode(loader, node):
|
|
|
|
return node.value
|
|
|
|
yaml.SafeLoader.add_constructor("tag:yaml.org,2002:str",
|
|
|
|
construct_unicode)
|
2016-07-20 00:22:12 +00:00
|
|
|
|
|
|
|
def safe_load(f):
|
|
|
|
return yaml.safe_load(f)
|
|
|
|
|
|
|
|
def safe_dump(obj):
|
|
|
|
return yaml.safe_dump(obj)
|