4 from subprocess import Popen, PIPE
6 # Import this way to produce a more useful error message.
10 print('Unable to import YAML module: please install PyYAML', file=sys.stderr)
14 # Things an image file's name can end with.
22 # Files that shouldn't be present.
27 # Marker to show that an expected value hasn't been provided.
28 # (Can't use 'None' because that might be a legitimate value.)
33 """Collect and report errors."""
39 def check_field(self, filename, name, values, key, expected=REPORTER_NOT_SET):
40 """Check that a dictionary has an expected value."""
43 self.add(filename, '{0} does not contain {1}', name, key)
44 elif expected is REPORTER_NOT_SET:
46 elif type(expected) in (tuple, set, list):
47 if values[key] not in expected:
49 filename, '{0} {1} value {2} is not in {3}', name, key, values[key], expected)
50 elif values[key] != expected:
51 self.add(filename, '{0} {1} is {2} not {3}',
52 name, key, values[key], expected)
54 def check(self, condition, location, fmt, *args):
55 """Append error if condition not met."""
58 self.add(location, fmt, *args)
60 def add(self, location, fmt, *args):
61 """Append error unilaterally."""
63 self.messages.append((location, fmt.format(*args)))
67 location, message = item
68 if isinstance(location, type(None)):
70 elif isinstance(location, str):
71 return location + ': ' + message
72 elif isinstance(location, tuple):
73 return '{0}:{1}: '.format(*location) + message
75 print('Unknown item "{0}"'.format(item), file=sys.stderr)
80 location, message = item
81 if isinstance(location, type(None)):
82 return ('', -1, message)
83 elif isinstance(location, str):
84 return (location, -1, message)
85 elif isinstance(location, tuple):
86 return (location[0], location[1], message)
88 print('Unknown item "{0}"'.format(item), file=sys.stderr)
91 def report(self, stream=sys.stdout):
92 """Report all messages in order."""
97 for m in sorted(self.messages, key=self.key):
98 print(self.pretty(m), file=stream)
101 def read_markdown(parser, path):
103 Get YAML and AST for Markdown file, returning
104 {'metadata':yaml, 'metadata_len':N, 'text':text, 'lines':[(i, line, len)], 'doc':doc}.
107 # Split and extract YAML (if present).
108 with open(path, 'r') as reader:
110 metadata_raw, metadata_yaml, body = split_metadata(path, body)
113 metadata_len = 0 if metadata_raw is None else metadata_raw.count('\n')
114 lines = [(metadata_len+i+1, line, len(line))
115 for (i, line) in enumerate(body.split('\n'))]
118 cmd = 'ruby {0}'.format(parser)
119 p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE,
120 close_fds=True, universal_newlines=True)
121 stdout_data, stderr_data = p.communicate(body)
122 doc = json.loads(stdout_data)
125 'metadata': metadata_yaml,
126 'metadata_len': metadata_len,
133 def split_metadata(path, text):
135 Get raw (text) metadata, metadata as YAML, and rest of body.
136 If no metadata, return (None, None, body).
142 pieces = text.split('---', 2)
144 metadata_raw = pieces[1]
147 metadata_yaml = yaml.load(metadata_raw)
148 except yaml.YAMLError as e:
149 print('Unable to parse YAML header in {0}:\n{1}'.format(
150 path, e), file=sys.stderr)
153 return metadata_raw, metadata_yaml, text
156 def load_yaml(filename):
158 Wrapper around YAML loading so that 'import yaml' is only needed
163 with open(filename, 'r') as reader:
164 return yaml.load(reader)
165 except (yaml.YAMLError, IOError) as e:
166 print('Unable to load YAML file {0}:\n{1}'.format(
167 filename, e), file=sys.stderr)
171 def check_unwanted_files(dir_path, reporter):
173 Check that unwanted files are not present.
176 for filename in UNWANTED_FILES:
177 path = os.path.join(dir_path, filename)
178 reporter.check(not os.path.exists(path),
180 "Unwanted file found")
183 def require(condition, message):
184 """Fail if condition not met."""
187 print(message, file=sys.stderr)