-
Notifications
You must be signed in to change notification settings - Fork 22
/
loggers.py
206 lines (159 loc) · 6.76 KB
/
loggers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""Copyright 2020 Google Inc.
All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import copy
class LogEntry(object):
"""This class contains needed information for user output."""
def __init__(self, message, elements=None, lines=None):
self.message = message
self.elements = elements
if lines is not None:
self.lines = lines
elif elements is not None:
self.lines = []
for element in elements:
if(element is not None and hasattr(element, "sourceline")
and element.sourceline is not None):
self.lines.append(element.sourceline)
else:
self.lines = None
# pylint: disable=g-bad-exception-name
class ElectionException(Exception):
"""Base class for all the exceptions in this script."""
description = None
def __init__(self, log_entry):
super(ElectionException, self).__init__()
if isinstance(log_entry, list):
self.log_entry = log_entry
else:
self.log_entry = [log_entry]
@classmethod
def from_message(cls, message, elements=None, lines=None):
return cls(LogEntry(message, elements, lines))
class ElectionFatal(ElectionException):
"""An Fatal that prevents the feed from being processed successfully."""
description = "Fatal"
class ElectionError(ElectionException):
"""An error that prevents the feed from being processed successfully."""
description = "Error"
# pylint: disable=g-bad-exception-name
class ElectionWarning(ElectionException):
"""An issue that should be fixed.
It will not stop the feed from being successfully processed but may lead to
undefined errors.
"""
description = "Warning"
# pylint: disable=g-bad-exception-name
class ElectionInfo(ElectionException):
"""Information that user needs to know about following XML best practices."""
description = "Info"
def get_parent_hierarchy_object_id_str(elt):
"""Get the elt path from the 1st parent with an objectId / ElectionReport."""
elt_hierarchy = []
current_elt = elt
while current_elt is not None:
if current_elt.get("objectId"):
elt_hierarchy.append(current_elt.tag + ":" + current_elt.get("objectId"))
break
else:
elt_hierarchy.append(current_elt.tag)
current_elt = current_elt.getparent()
return " > ".join(elt_hierarchy[::-1])
class ExceptionListWrapper:
"""This provide helpers to manage a list of ElectionException.
This class contains a list of ElectionExceptions. It provides a handler to
enrich the list and manage the validator output depending on the user choice
for severity. So, as an example if the user selects a severity warning,
ElectionWarning, ElectionError log_entry will be displayed and ElectionInfo
will be skipped. We print the selected severity + other Exception types with
higher severity.
"""
SUPPORTED_SEVERITIES = (ElectionInfo,
ElectionWarning,
ElectionError,
ElectionFatal)
def __init__(self):
self._rules_exc_logs = dict()
for e_type in self.SUPPORTED_SEVERITIES:
self._rules_exc_logs[e_type] = dict()
def exception_handler(self, exception, rule_name="No rule"):
"""Gather log entry counts by type, class, and total."""
for e_type in self._rules_exc_logs:
if issubclass(exception.__class__, e_type):
if rule_name not in self._rules_exc_logs[e_type]:
self._rules_exc_logs[e_type][rule_name] = []
self._rules_exc_logs[e_type][rule_name].extend(exception.log_entry)
def _select_exception_types(self, severity):
if not severity:
severity = 0
elif severity > len(self.SUPPORTED_SEVERITIES):
severity = len(self.SUPPORTED_SEVERITIES) - 1
return self.SUPPORTED_SEVERITIES[severity:]
def count_logs_with_exception_type(self, e_type):
exception_count = 0
for log_list in list(self._rules_exc_logs[e_type].values()):
exception_count += len(log_list)
return exception_count
def _print_exception_type_summary(self, e_type, count_exception):
suffix = ""
if count_exception > 1:
suffix = "s"
print("{0:6d} {1} message{2} found".format(count_exception,
e_type.description, suffix))
def _print_rule_exception_summary(self, e_type, rule_name):
rule_count = len(self._rules_exc_logs[e_type][rule_name])
rule_suffix = ""
if rule_count > 1:
rule_suffix = "s"
print("{0:10d} {1} {2} message{3}".format(rule_count, rule_name,
e_type.description, rule_suffix))
def _print_exception_log(self, log_entry):
if log_entry.lines is not None:
print(" " * 14 + "Lines %s :" % log_entry.lines)
if log_entry.elements is not None:
print(" " * 15 + "Affected elements :")
for element in log_entry.elements:
print(" " * 16 + get_parent_hierarchy_object_id_str(element))
print(" " * 18 + "* {0}".format(log_entry.message))
def get_all_exceptions(self):
return copy.deepcopy(self._rules_exc_logs)
def print_exceptions(self, severity, verbose):
"""Print exceptions in decreasing order of severity."""
exception_types = self._select_exception_types(severity)
for e_type in reversed(exception_types):
exception_count = self.count_logs_with_exception_type(e_type)
self._print_exception_type_summary(e_type, exception_count)
if exception_count == 0:
continue
# pylint: disable=cell-var-from-loop
# Within the error severity, sort from most common to least common.
for rule_name in sorted(
self._rules_exc_logs[e_type].keys(),
key=lambda rclass: len(self._rules_exc_logs[e_type][rclass]),
reverse=True):
self._print_rule_exception_summary(e_type, rule_name)
if verbose:
for log_entry in self._rules_exc_logs[e_type][rule_name]:
self._print_exception_log(log_entry)
def handled_severities():
return ExceptionListWrapper.SUPPORTED_SEVERITIES
def supported_severities_mapping():
exp_severity_map = {}
for n in range(len(ExceptionListWrapper.SUPPORTED_SEVERITIES)):
exp_severity_map[
ExceptionListWrapper.SUPPORTED_SEVERITIES[n].description.lower()] = n
return exp_severity_map
def severities_names():
names = []
for exp in ExceptionListWrapper.SUPPORTED_SEVERITIES:
names.append(exp.description.lower())
return names