aboutsummaryrefslogtreecommitdiffstats
path: root/yamlish.py
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2015-01-12 19:17:50 +0100
committerMatěj Cepl <mcepl@cepl.eu>2015-01-12 19:19:05 +0100
commitdba0b0a5b65c57c71c50ce1681f3286869e231bc (patch)
treee1cf5638e259e75ab031999fa13841b5d702420a /yamlish.py
parent7297401f8acb8a232ad629bc03be635c2c530926 (diff)
downloadyamlish-dba0b0a5b65c57c71c50ce1681f3286869e231bc.tar.gz
Add optional parameter to yamlish.load ignore_wrong_characters0.15
When set to True, then yamlish doesn't crash on bad UTF8 characters but rather skips them. Fixes #2
Diffstat (limited to 'yamlish.py')
-rw-r--r--yamlish.py21
1 files changed, 16 insertions, 5 deletions
diff --git a/yamlish.py b/yamlish.py
index 28e8aea..a387d35 100644
--- a/yamlish.py
+++ b/yamlish.py
@@ -117,8 +117,9 @@ class NullHandler(logging.Handler):
def emit(self, record):
pass
-log = logging.getLogger("bayeux")
+log = logging.getLogger("yamlish")
log.addHandler(NullHandler())
+#log.setLevel(logging.DEBUG)
__docformat__ = 'reStructuredText'
__version__ = "0.10"
@@ -167,7 +168,7 @@ yaml.add_representer(str, str_representer_compact_multiline,
yaml.add_representer(unicode, str_representer_compact_multiline,
Dumper=_YamlishDumper)
-def load(source):
+def load(source, ignore_wrong_characters=False):
"""
Return object loaded from a YAML document in source.
@@ -176,16 +177,26 @@ def load(source):
many others).
"""
out = None
- log.debug("inobj:\n%s", source)
+ log.debug("inobj: (%s)\n%s", type(source), source)
+ log.debug('before ignore_wrong_characters = %s', ignore_wrong_characters)
if isinstance(source, (str, unicode)):
out = yaml.load(source, Loader=_YamlishLoader)
log.debug("out (string) = %s", out)
elif hasattr(source, "__iter__"):
inobj = ""
for line in source:
- inobj += line + '\n'
- out = load(inobj)
+ try:
+ inobj += line + '\n'
+ except UnicodeDecodeError:
+ log.debug('in ignore_wrong_characters = %s', ignore_wrong_characters)
+ if ignore_wrong_characters:
+ inobj += line.decode('utf8', errors='ignore') + '\n'
+ else:
+ raise
+ log.debug('restarting load with inobj as string')
+ out = load(inobj, ignore_wrong_characters)
log.debug("out (iter) = %s", out)
+ log.debug("out (iter) = type %s", type(out))
return out
def dump(source, destination):