|
10 | 10 | print('Unable to import YAML module: please install PyYAML', file=sys.stderr) |
11 | 11 | sys.exit(1) |
12 | 12 |
|
| 13 | +__all__ = ['check_unwanted_files', 'load_yaml', 'read_markdown', 'require'] |
13 | 14 |
|
14 | 15 | # Files that shouldn't be present. |
15 | 16 | UNWANTED_FILES = [ |
16 | 17 | '.nojekyll' |
17 | 18 | ] |
18 | 19 |
|
19 | | -# Marker to show that an expected value hasn't been provided. |
20 | | -# (Can't use 'None' because that might be a legitimate value.) |
21 | | -REPORTER_NOT_SET = [] |
22 | | - |
23 | | - |
24 | | -class Reporter: |
25 | | - """Collect and report errors.""" |
26 | | - |
27 | | - def __init__(self): |
28 | | - """Constructor.""" |
29 | | - self.messages = [] |
30 | | - |
31 | | - def check_field(self, filename, name, values, key, expected=REPORTER_NOT_SET): |
32 | | - """Check that a dictionary has an expected value.""" |
33 | | - |
34 | | - if key not in values: |
35 | | - self.add(filename, '{0} does not contain {1}', name, key) |
36 | | - elif expected is REPORTER_NOT_SET: |
37 | | - pass |
38 | | - elif type(expected) in (tuple, set, list): |
39 | | - if values[key] not in expected: |
40 | | - self.add( |
41 | | - filename, '{0} {1} value {2} is not in {3}', name, key, values[key], expected) |
42 | | - elif values[key] != expected: |
43 | | - self.add(filename, '{0} {1} is {2} not {3}', |
44 | | - name, key, values[key], expected) |
45 | | - |
46 | | - def check(self, condition, location, fmt, *args): |
47 | | - """Append error if condition not met.""" |
48 | | - |
49 | | - if not condition: |
50 | | - self.add(location, fmt, *args) |
51 | | - |
52 | | - def add(self, location, fmt, *args): |
53 | | - """Append error unilaterally.""" |
54 | | - |
55 | | - self.messages.append((location, fmt.format(*args))) |
56 | | - |
57 | | - @staticmethod |
58 | | - def pretty(item): |
59 | | - location, message = item |
60 | | - if isinstance(location, type(None)): |
61 | | - return message |
62 | | - elif isinstance(location, str): |
63 | | - return location + ': ' + message |
64 | | - elif isinstance(location, tuple): |
65 | | - return '{0}:{1}: '.format(*location) + message |
66 | | - |
67 | | - print('Unknown item "{0}"'.format(item), file=sys.stderr) |
68 | | - return NotImplemented |
69 | | - |
70 | | - @staticmethod |
71 | | - def key(item): |
72 | | - location, message = item |
73 | | - if isinstance(location, type(None)): |
74 | | - return ('', -1, message) |
75 | | - elif isinstance(location, str): |
76 | | - return (location, -1, message) |
77 | | - elif isinstance(location, tuple): |
78 | | - return (location[0], location[1], message) |
79 | | - |
80 | | - print('Unknown item "{0}"'.format(item), file=sys.stderr) |
81 | | - return NotImplemented |
82 | | - |
83 | | - def report(self, stream=sys.stdout): |
84 | | - """Report all messages in order.""" |
85 | | - |
86 | | - if not self.messages: |
87 | | - return |
88 | | - |
89 | | - for m in sorted(self.messages, key=self.key): |
90 | | - print(self.pretty(m), file=stream) |
91 | | - |
92 | | - |
93 | 20 | def read_markdown(parser, path): |
94 | 21 | """ |
95 | 22 | Get YAML and AST for Markdown file, returning |
|
0 commit comments