Skip to content

Commit 617d24b

Browse files
committed
[tools/xmlparser] Add show_warnings on load
1 parent 6c86f9a commit 617d24b

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

odml/tools/xmlparser.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,21 @@ class XMLReader(object):
148148
>>> doc = XMLReader().from_file("file.odml")
149149
"""
150150

151-
def __init__(self, ignore_errors=False, filename=None):
151+
def __init__(self, ignore_errors=False, show_warnings=True, filename=None):
152+
"""
153+
:param ignore_errors: To allow loading and fixing of invalid odml files
154+
encountered errors can be converted to warnings
155+
instead. Such a document can only be saved when
156+
all errors have been addressed though.
157+
:param show_warnings: Toggle whether to print warnings to the command line.
158+
Any warnings can be accessed via the Reader's class
159+
warnings attribute after parsing is done.
160+
:param filename: Path to an odml file.
161+
"""
152162
self.parser = ET.XMLParser(remove_comments=True)
153163
self.tags = dict([(obj.name, obj) for obj in format.__all__])
154164
self.ignore_errors = ignore_errors
165+
self.show_warnings = show_warnings
155166
self.filename = filename
156167
self.warnings = []
157168

@@ -229,8 +240,10 @@ def warn(self, msg, elem):
229240
self.filename, elem.sourceline, elem.tag, msg)
230241
else:
231242
msg = "warning: %s\n" % msg
243+
232244
self.warnings.append(msg)
233-
sys.stderr.write(msg)
245+
if self.show_warnings:
246+
sys.stderr.write(msg)
234247

235248
def parse_element(self, node):
236249
if node.tag not in self.tags:

0 commit comments

Comments
 (0)