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)
19 class Reporter(object):
20 """Collect and report errors."""
25 super(Reporter, self).__init__()
29 def check_field(self, filename, name, values, key, expected):
30 """Check that a dictionary has an expected value."""
33 self.add(filename, '{0} does not contain {1}', name, key)
34 elif values[key] != expected:
35 self.add(filename, '{0} {1} is {2} not {3}', name, key, values[key], expected)
38 def check(self, condition, location, fmt, *args):
39 """Append error if condition not met."""
42 self.add(location, fmt, *args)
45 def add(self, location, fmt, *args):
46 """Append error unilaterally."""
48 if isinstance(location, type(None)):
50 elif isinstance(location, str):
51 coords = '{0}: '.format(location)
52 elif isinstance(location, tuple):
53 filename, line_number = location
54 coords = '{0}:{1}: '.format(*location)
56 assert False, 'Unknown location "{0}"/{1}'.format(location, type(location))
58 self.messages.append(coords + fmt.format(*args))
61 def report(self, stream=sys.stdout):
62 """Report all messages."""
66 for m in sorted(self.messages):
70 def read_markdown(parser, path):
72 Get YAML and AST for Markdown file, returning
73 {'metadata':yaml, 'metadata_len':N, 'text':text, 'lines':[(i, line, len)], 'doc':doc}.
76 # Split and extract YAML (if present).
77 with open(path, 'r') as reader:
79 metadata_raw, metadata_yaml, body = split_metadata(path, body)
82 metadata_len = 0 if metadata_raw is None else metadata_raw.count('\n')
83 lines = [(metadata_len+i+1, line, len(line)) for (i, line) in enumerate(body.split('\n'))]
86 cmd = 'ruby {0}'.format(parser)
87 p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, close_fds=True, universal_newlines=True)
88 stdout_data, stderr_data = p.communicate(body)
89 doc = json.loads(stdout_data)
92 'metadata': metadata_yaml,
93 'metadata_len': metadata_len,
100 def split_metadata(path, text):
102 Get raw (text) metadata, metadata as YAML, and rest of body.
103 If no metadata, return (None, None, body).
110 pieces = text.split('---', 2)
112 metadata_raw = pieces[1]
115 metadata_yaml = yaml.load(metadata_raw)
116 except yaml.YAMLError as e:
117 print('Unable to parse YAML header in {0}:\n{1}'.format(path, e), file=sys.stderr)
120 return metadata_raw, metadata_yaml, text
123 def load_yaml(filename):
125 Wrapper around YAML loading so that 'import yaml' is only needed
130 with open(filename, 'r') as reader:
131 return yaml.load(reader)
132 except (yaml.YAMLError, FileNotFoundError) as e:
133 print('Unable to load YAML file {0}:\n{1}'.format(filename, e), file=sys.stderr)
137 def check_unwanted_files(dir_path, reporter):
139 Check that unwanted files are not present.
142 for filename in UNWANTED_FILES:
143 path = os.path.join(dir_path, filename)
144 reporter.check(not os.path.exists(path),
146 "Unwanted file found")