Skip to content

Commit 4df4314

Browse files
authored
Merge pull request #311 from mpsonntag/showWarnings
Toggle warning messages on load
2 parents 6c86f9a + 3654d37 commit 4df4314

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

odml/fileio.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22
from .tools.odmlparser import ODMLReader, ODMLWriter
33

44

5-
def load(filename, backend="xml"):
5+
def load(filename, backend="xml", show_warnings=True):
66
"""
77
Load an odML document from file.
88
:param filename: Path and filename from where the odML document
99
is to be loaded and parsed.
1010
:param backend: File format of the file containing the odML document.
1111
The default format is XML.
12+
:param show_warnings: Toggle whether to print warnings to the command line.
1213
:return: The parsed odML document.
1314
"""
1415
if not os.path.exists(filename):
1516
msg = "File \'%s\' was not found!" % \
1617
(filename if len(filename) < 20 else "...%s" % filename[19:])
1718
raise FileNotFoundError(msg)
1819

19-
reader = ODMLReader(backend)
20+
reader = ODMLReader(backend, show_warnings)
2021
return reader.from_file(filename)
2122

2223

odml/tools/dict_parser.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,14 @@ class DictReader:
104104
A reader to parse dictionaries with odML content into a proper odML document.
105105
"""
106106

107-
def __init__(self):
107+
def __init__(self, show_warnings=True):
108+
"""
109+
:param show_warnings: Toggle whether to print warnings to the command line.
110+
Any warnings can be accessed via the Reader's class
111+
warnings attribute after parsing is done.
112+
"""
108113
self.parsed_doc = None # Python dictionary object equivalent
114+
self.show_warnings = show_warnings
109115
self.warnings = []
110116

111117
def is_valid_attribute(self, attr, fmt):
@@ -116,8 +122,9 @@ def is_valid_attribute(self, attr, fmt):
116122
return attr
117123

118124
msg = "Invalid element <%s> inside <%s> tag" % (attr, fmt.__class__.__name__)
119-
print(msg)
120125
self.warnings.append(msg)
126+
if self.show_warnings:
127+
print(msg)
121128
return None
122129

123130
def to_odml(self, parsed_doc):

odml/tools/odmlparser.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,25 @@ class ODMLReader:
107107
json_odml_doc = ODMLReader(parser='JSON').from_file("odml_doc.json")
108108
"""
109109

110-
def __init__(self, parser='XML'):
110+
def __init__(self, parser='XML', show_warnings=True):
111+
"""
112+
:param parser: odml parser; supported are 'XML', 'JSON', 'YAML' and 'RDF'.
113+
:param show_warnings: Toggle whether to print warnings to the command line.
114+
"""
111115
self.doc = None # odML document
112116
self.parsed_doc = None # Python dictionary object equivalent
113117
parser = parser.upper()
114118
if parser not in SUPPORTED_PARSERS:
115119
raise NotImplementedError("'%s' odML parser does not exist!" % parser)
116120
self.parser = parser
121+
self.show_warnings = show_warnings
117122
self.warnings = []
118123

119124
def from_file(self, file, doc_format=None):
120125

121126
if self.parser == 'XML':
122-
par = xmlparser.XMLReader(ignore_errors=True)
127+
par = xmlparser.XMLReader(ignore_errors=True,
128+
show_warnings=self.show_warnings)
123129
self.warnings = par.warnings
124130
self.doc = par.from_file(file)
125131
return self.doc
@@ -132,7 +138,8 @@ def from_file(self, file, doc_format=None):
132138
print(err)
133139
return
134140

135-
self.doc = DictReader().to_odml(self.parsed_doc)
141+
par = DictReader(show_warnings=self.show_warnings)
142+
self.doc = par.to_odml(self.parsed_doc)
136143
# Provide original file name via the in memory document
137144
self.doc._origin_file_name = basename(file)
138145
return self.doc
@@ -145,7 +152,8 @@ def from_file(self, file, doc_format=None):
145152
print("JSON Decoder Error: %s" % err)
146153
return
147154

148-
self.doc = DictReader().to_odml(self.parsed_doc)
155+
par = DictReader(show_warnings=self.show_warnings)
156+
self.doc = par.to_odml(self.parsed_doc)
149157
# Provide original file name via the in memory document
150158
self.doc._origin_file_name = basename(file)
151159
return self.doc

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)