forked from kirilvasilev16/mude-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreporter.py
75 lines (57 loc) · 2.35 KB
/
reporter.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
import sys
class Reporter:
"""Collect and report errors."""
# Marker to show that an expected value hasn't been provided.
# (Can't use 'None' because that might be a legitimate value.)
_DEFAULT_REPORTER = []
def __init__(self):
"""Constructor."""
self.messages = []
def check_field(self, filename, name, values, key, expected=_DEFAULT_REPORTER):
"""Check that a dictionary has an expected value."""
if key not in values:
self.add(filename, '{0} does not contain {1}', name, key)
elif expected is self._DEFAULT_REPORTER:
pass
elif type(expected) in (tuple, set, list):
if values[key] not in expected:
self.add(
filename, '{0} {1} value {2} is not in {3}', name, key, values[key], expected)
elif values[key] != expected:
self.add(filename, '{0} {1} is {2} not {3}',
name, key, values[key], expected)
def check(self, condition, location, fmt, *args):
"""Append error if condition not met."""
if not condition:
self.add(location, fmt, *args)
def add(self, location, fmt, *args):
"""Append error unilaterally."""
self.messages.append((location, fmt.format(*args)))
@staticmethod
def pretty(item):
location, message = item
if isinstance(location, type(None)):
return message
elif isinstance(location, str):
return location + ': ' + message
elif isinstance(location, tuple):
return '{0}:{1}: '.format(*location) + message
print('Unknown item "{0}"'.format(item), file=sys.stderr)
return NotImplemented
@staticmethod
def key(item):
location, message = item
if isinstance(location, type(None)):
return ('', -1, message)
elif isinstance(location, str):
return (location, -1, message)
elif isinstance(location, tuple):
return (location[0], location[1], message)
print('Unknown item "{0}"'.format(item), file=sys.stderr)
return NotImplemented
def report(self, stream=sys.stdout):
"""Report all messages in order."""
if not self.messages:
return
for m in sorted(self.messages, key=self.key):
print(self.pretty(m), file=stream)