Passed
Push — master ( 77064b...da9bd1 )
by Konstantin
01:57
created

ocrd_models.report   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 38
dl 0
loc 81
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A ValidationReport.__init__() 0 7 1
A ValidationReport.to_xml() 0 9 4
A ValidationReport.add_notice() 0 5 1
A ValidationReport.is_valid() 0 6 1
B ValidationReport.__str__() 0 15 7
A ValidationReport.add_error() 0 5 1
A ValidationReport.merge_report() 0 7 1
A ValidationReport.add_warning() 0 5 1
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