Skip to content

Commit 9f41471

Browse files
committed
[odmlparser] Validation results on save and load
A validation report is now printed as a warnings module UserWarnings if a saved or loaded Document contains Validation warnings or errors. On load the warnings will only be printed when the Parsers 'show_warnings' attribute is True.
1 parent 93af689 commit 9f41471

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

odml/tools/odmlparser.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import datetime
88
import json
99
import sys
10+
import warnings
1011

1112
from os.path import basename
1213

@@ -61,11 +62,18 @@ def write_file(self, odml_document, filename):
6162
msg = ""
6263
for err in validation.errors:
6364
if err.is_error:
64-
msg += "\n\t- %s %s: %s" % (err.obj, err.rank, err.msg)
65+
# msg += "\n\t- %s %s: %s" % (err.obj, err.rank, err.msg)
66+
msg += "\n- %s" % err
6567
if msg != "":
6668
msg = "Resolve document validation errors before saving %s" % msg
6769
raise ParserException(msg)
6870

71+
report = validation.report()
72+
if report:
73+
msg += "The saved Document contains formal issues."
74+
msg += " Run 'odml.Validation' to resolve them.\n%s" % report
75+
warnings.warn(msg)
76+
6977
with open(filename, 'w') as file:
7078
# Add XML header to support odML stylesheets.
7179
if self.parser == 'XML':
@@ -153,6 +161,13 @@ def __init__(self, parser='XML', show_warnings=True):
153161
self.show_warnings = show_warnings
154162
self.warnings = []
155163

164+
def _validation_warning(self):
165+
report = Validation(self.doc).report()
166+
if report:
167+
msg = "The loaded Document contains formal issues."
168+
msg += "Run 'odml.Validation' to resolve them.\n%s" % report
169+
warnings.warn(msg)
170+
156171
def from_file(self, file, doc_format=None):
157172
"""
158173
Loads an odML document from a file. The ODMLReader.parser specifies the
@@ -171,6 +186,11 @@ def from_file(self, file, doc_format=None):
171186
show_warnings=self.show_warnings)
172187
self.warnings = par.warnings
173188
self.doc = par.from_file(file)
189+
190+
# Print validation warnings after parsing
191+
if self.show_warnings:
192+
self._validation_warning()
193+
174194
return self.doc
175195

176196
if self.parser == 'YAML':
@@ -188,6 +208,11 @@ def from_file(self, file, doc_format=None):
188208
self.doc = par.to_odml(self.parsed_doc)
189209
# Provide original file name via the in memory document
190210
self.doc.origin_file_name = basename(file)
211+
212+
# Print validation warnings after parsing
213+
if self.show_warnings:
214+
self._validation_warning()
215+
191216
return self.doc
192217

193218
if self.parser == 'JSON':
@@ -202,13 +227,27 @@ def from_file(self, file, doc_format=None):
202227
self.doc = par.to_odml(self.parsed_doc)
203228
# Provide original file name via the in memory document
204229
self.doc.origin_file_name = basename(file)
230+
231+
# Print validation warnings after parsing
232+
if self.show_warnings:
233+
self._validation_warning()
234+
205235
return self.doc
206236

207237
if self.parser == 'RDF':
208238
if not doc_format:
209239
raise ValueError("Format of the rdf file was not specified")
210240

241+
# Importing from an RDF graph can return multiple documents
211242
self.doc = RDFReader().from_file(file, doc_format)
243+
244+
for doc in self.doc:
245+
report = Validation(doc).report()
246+
if report:
247+
msg = "The loaded Document contains formal issues."
248+
msg += "Run 'odml.Validation' to resolve them.\n%s" % report
249+
warnings.warn(msg)
250+
212251
return self.doc
213252

214253
def from_string(self, string, doc_format=None):
@@ -227,6 +266,11 @@ def from_string(self, string, doc_format=None):
227266

228267
if self.parser == 'XML':
229268
self.doc = xmlparser.XMLReader().from_string(string)
269+
270+
# Print validation warnings after parsing
271+
if self.show_warnings:
272+
self._validation_warning()
273+
230274
return self.doc
231275

232276
if self.parser == 'YAML':
@@ -237,6 +281,11 @@ def from_string(self, string, doc_format=None):
237281
return
238282

239283
self.doc = DictReader().to_odml(self.parsed_doc)
284+
285+
# Print validation warnings after parsing
286+
if self.show_warnings:
287+
self._validation_warning()
288+
240289
return self.doc
241290

242291
if self.parser == 'JSON':
@@ -247,13 +296,27 @@ def from_string(self, string, doc_format=None):
247296
return
248297

249298
self.doc = DictReader().to_odml(self.parsed_doc)
299+
300+
# Print validation warnings after parsing
301+
if self.show_warnings:
302+
self._validation_warning()
303+
250304
return self.doc
251305

252306
if self.parser == 'RDF':
253307
if not doc_format:
254308
raise ValueError("Format of the rdf file was not specified")
255309

310+
# Importing from an RDF graph can return multiple documents
256311
self.doc = RDFReader().from_string(string, doc_format)
312+
313+
for doc in self.doc:
314+
report = Validation(doc).report()
315+
if report:
316+
msg = "The loaded Document contains formal issues."
317+
msg += "Run 'odml.Validation' to resolve them.\n%s" % report
318+
warnings.warn(msg)
319+
257320
return self.doc
258321

259322

0 commit comments

Comments
 (0)