Skip to content

Commit 8e292c8

Browse files
authored
Merge pull request #389 from mpsonntag/refactorValid
Validation changes LGTM
2 parents 266f1c1 + 271d78d commit 8e292c8

File tree

6 files changed

+270
-63
lines changed

6 files changed

+270
-63
lines changed

odml/doc.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from . import dtypes
99
from . import format as fmt
1010
from . import terminology
11+
from . import validation
1112
from .tools.doc_inherit import inherit_docstring, allow_inherit_docstring
1213

1314

@@ -152,6 +153,14 @@ def finalize(self):
152153
if sec._include is not None:
153154
sec.include = sec._include
154155

156+
def validate(self):
157+
"""
158+
Runs a validation on itself and returns the Validation object.
159+
160+
:return: odml.Validation
161+
"""
162+
return validation.Validation(self)
163+
155164
@inherit_docstring
156165
def get_terminology_equivalent(self):
157166
if self.repository is None:

odml/property.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ def name(self, new_name):
213213
if self.name == new_name:
214214
return
215215

216+
# Make sure name cannot be set to None or empty
217+
if not new_name:
218+
self._name = self._id
219+
return
220+
216221
curr_parent = self.parent
217222
if hasattr(curr_parent, "properties") and new_name in curr_parent.properties:
218223

@@ -569,11 +574,12 @@ def _values_cardinality_validation(self):
569574
is respected and prints a warning message otherwise.
570575
"""
571576
valid = validation.Validation(self)
577+
val_id = validation.IssueID.property_values_cardinality
572578

573579
# Make sure to display only warnings of the current property
574-
res = [curr for curr in valid.errors if self.id == curr.obj.id]
575-
for err in res:
576-
print("%s: %s" % (err.rank.capitalize(), err.msg))
580+
for curr in valid.errors:
581+
if curr.validation_id == val_id and self.id == curr.obj.id:
582+
print("%s: %s" % (curr.rank.capitalize(), curr.msg))
577583

578584
def set_values_cardinality(self, min_val=None, max_val=None):
579585
"""

odml/section.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ def name(self, new_value):
171171
if self.name == new_value:
172172
return
173173

174+
# Make sure name cannot be set to None or empty
175+
if not new_value:
176+
self._name = self._id
177+
return
178+
174179
curr_parent = self.parent
175180
if hasattr(curr_parent, "sections") and new_value in curr_parent.sections:
176181
raise KeyError("Object with the same name already exists!")
@@ -416,10 +421,12 @@ def _sections_cardinality_validation(self):
416421
is respected and prints a warning message otherwise.
417422
"""
418423
valid = validation.Validation(self)
424+
val_id = validation.IssueID.section_sections_cardinality
425+
419426
# Make sure to display only warnings of the current section
420-
res = [curr for curr in valid.errors if self.id == curr.obj.id]
421-
for err in res:
422-
print("%s: %s" % (err.rank.capitalize(), err.msg))
427+
for curr in valid.errors:
428+
if curr.validation_id == val_id and self.id == curr.obj.id:
429+
print("%s: %s" % (curr.rank.capitalize(), curr.msg))
423430

424431
@property
425432
def prop_cardinality(self):
@@ -469,10 +476,12 @@ def _properties_cardinality_validation(self):
469476
is respected and prints a warning message otherwise.
470477
"""
471478
valid = validation.Validation(self)
479+
val_id = validation.IssueID.section_properties_cardinality
480+
472481
# Make sure to display only warnings of the current section
473-
res = [curr for curr in valid.errors if self.id == curr.obj.id]
474-
for err in res:
475-
print("%s: %s" % (err.rank.capitalize(), err.msg))
482+
for curr in valid.errors:
483+
if curr.validation_id == val_id and self.id == curr.obj.id:
484+
print("%s: %s" % (curr.rank.capitalize(), curr.msg))
476485

477486
@inherit_docstring
478487
def get_terminology_equivalent(self):

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 unresolved issues."
74+
msg += " Run the Documents 'validate' method to access 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 unresolved issues."
168+
msg += " Run the Documents 'validate' method to access 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 unresolved issues."
248+
msg += " Run the Documents 'validate' method to access 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 unresolved issues."
317+
msg += " Run the Documents 'validate' method to access them.\n%s" % report
318+
warnings.warn(msg)
319+
257320
return self.doc
258321

259322

0 commit comments

Comments
 (0)