1
|
|
|
""" |
2
|
|
|
Validation report with messages of different levels of severity. |
3
|
|
|
""" |
4
|
|
|
__all__ = ['ValidationReport'] |
5
|
|
|
|
6
|
|
|
# |
7
|
|
|
# ------------------------------------------------- |
8
|
|
|
# |
9
|
|
|
|
10
|
|
|
class ValidationReport(object): |
11
|
|
|
""" |
12
|
|
|
Container of notices, warnings and errors about a workspace. |
13
|
|
|
""" |
14
|
|
|
|
15
|
|
|
def __init__(self): |
16
|
|
|
""" |
17
|
|
|
Create a new ValidationReport. |
18
|
|
|
""" |
19
|
|
|
self.notices = [] |
20
|
|
|
self.warnings = [] |
21
|
|
|
self.errors = [] |
22
|
|
|
|
23
|
|
|
def __str__(self): |
24
|
|
|
""" |
25
|
|
|
Serialize to string. |
26
|
|
|
""" |
27
|
|
|
ret = 'OK' if self.is_valid else 'INVALID' |
28
|
|
|
if not self.is_valid or self.notices: |
29
|
|
|
ret += '[' |
30
|
|
|
if self.warnings: |
31
|
|
|
ret += ' %s warnings' % len(self.warnings) |
32
|
|
|
if self.errors: |
33
|
|
|
ret += ' %s errors' % len(self.errors) |
34
|
|
|
if self.notices: |
35
|
|
|
ret += ' %s notices' % len(self.notices) |
36
|
|
|
ret += ' ]' |
37
|
|
|
return ret |
38
|
|
|
|
39
|
|
|
@property |
40
|
|
|
def is_valid(self): |
41
|
|
|
""" |
42
|
|
|
Whether the report contains neither errors nor warnings. |
43
|
|
|
""" |
44
|
|
|
return not self.warnings and not self.errors |
45
|
|
|
|
46
|
|
|
def to_xml(self): |
47
|
|
|
""" |
48
|
|
|
Serialize to XML. |
49
|
|
|
""" |
50
|
|
|
body = '' |
51
|
|
|
for k in ['warning', 'error', 'notice']: |
52
|
|
|
for msg in self.__dict__[k + 's']: |
53
|
|
|
body += '\n <%s>%s</%s>' % (k, msg, k) |
54
|
|
|
return '<report valid="%s">%s\n</report>' % ("true" if self.is_valid else "false", body) |
55
|
|
|
|
56
|
|
|
def add_warning(self, msg): |
57
|
|
|
""" |
58
|
|
|
Add a warning. |
59
|
|
|
""" |
60
|
|
|
self.warnings.append(msg) |
61
|
|
|
|
62
|
|
|
def add_error(self, msg): |
63
|
|
|
""" |
64
|
|
|
Add an error |
65
|
|
|
""" |
66
|
|
|
self.errors.append(msg) |
67
|
|
|
|
68
|
|
|
def add_notice(self, msg): |
69
|
|
|
""" |
70
|
|
|
Add a notice |
71
|
|
|
""" |
72
|
|
|
self.notices.append(msg) |
73
|
|
|
|
74
|
|
|
def merge_report(self, otherself): |
75
|
|
|
""" |
76
|
|
|
Merge another report into this one. |
77
|
|
|
""" |
78
|
|
|
self.notices += otherself.notices |
79
|
|
|
self.warnings += otherself.warnings |
80
|
|
|
self.errors += otherself.errors |
81
|
|
|
|