-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgenerate_docs.py
More file actions
165 lines (135 loc) · 5.99 KB
/
generate_docs.py
File metadata and controls
165 lines (135 loc) · 5.99 KB
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
"""
Contains functionality for turning YAML reports into human-readable documentation.
"""
import collections
import datetime
import os
import jinja2
import yaml
from slugify import slugify
class ReportRenderer:
"""
Generates human readable documentation from YAML reports.
"""
def __init__(self, config, report_files):
"""
Initialize a ReportRenderer.
Args:
config: An AnnotationConfig object
report_files: A list of files to combine and report on
"""
self.config = config
self.echo = self.config.echo
self.report_files = report_files
self.create_time = datetime.datetime.now(tz=datetime.timezone.utc)
self.full_report = self._aggregate_reports()
self.jinja_environment = jinja2.Environment(
autoescape=False,
loader=jinja2.FileSystemLoader(self.config.report_template_dir),
lstrip_blocks=True,
trim_blocks=True
)
self.top_level_template = self.jinja_environment.get_template('annotation_list.tpl')
self.all_choices = []
self.group_mapping = {}
for token in self.config.choices:
self.all_choices.extend(self.config.choices[token])
for group_name in self.config.groups:
for token in self.config.groups[group_name]:
self.group_mapping[token] = group_name
def _add_report_file_to_full_report(self, report_file, report):
"""
Add a specified report file to a report.
Args:
report_file:
report:
Returns:
"""
loaded_report = yaml.safe_load(report_file)
for filename in loaded_report:
trimmed_filename = filename
for prefix in self.config.trim_filename_prefixes:
if filename.startswith(prefix):
trimmed_filename = filename[len(prefix):]
break
if filename in report:
for loaded_annotation in loaded_report[filename]:
found = False
for report_annotation in report[filename]:
index_keys = ('line_number', 'annotation_token', 'annotation_data')
if all(loaded_annotation[k] == report_annotation[k] for k in index_keys):
report_annotation.update(loaded_annotation)
found = True
break
if not found:
report[trimmed_filename].append(loaded_annotation)
else:
report[trimmed_filename] = loaded_report[filename]
def _aggregate_reports(self):
"""
Combine all of the given report files into a single report object.
"""
report = collections.defaultdict(list)
# Combine report files into a single dict. If there are duplicate annotations, make sure we have the superset
# of data.
for r in self.report_files:
self._add_report_file_to_full_report(r, report)
return report
def _write_doc_file(self, doc_title, doc_filename, doc_data):
"""
Write out a single report file with the given data. This is rendered using the configured top level template.
Args:
doc_title: Title to use for the document.
doc_filename: Filename to write to.
doc_data: Dict of reporting data to use, in the {'file name': [list, of, annotations,]} style.
"""
full_doc_filename = os.path.join(
self.config.rendered_report_dir,
slugify(doc_filename)
)
full_doc_filename += self.config.rendered_report_file_extension
self.echo.echo_v(f'Writing {full_doc_filename}')
with open(full_doc_filename, 'w') as output:
output.write(self.top_level_template.render(
doc_title=doc_title,
create_time=self.create_time,
report=doc_data,
all_choices=self.all_choices,
all_annotations=self.config.annotation_tokens,
group_mapping=self.group_mapping,
slugify=slugify,
source_link_prefix=self.config.rendered_report_source_link_prefix,
third_party_package_location=self.config.third_party_package_location,
))
def _generate_per_choice_docs(self):
"""
Generate a page of documentation for each configured annotation choice.
"""
for choice in self.all_choices:
choice_report = collections.defaultdict(list)
for filename in self.full_report:
for annotation in self.full_report[filename]:
if isinstance(annotation['annotation_data'], list) and choice in annotation['annotation_data']:
choice_report[filename].append(annotation)
self._write_doc_file(f"All References to Choice '{choice}'", f'choice_{choice}', choice_report)
def _generate_per_annotation_docs(self):
"""
Generate a page of documentation for each configured annotation.
"""
for annotation in self.config.annotation_tokens:
annotation_report = collections.defaultdict(list)
for filename in self.full_report:
for report_annotation in self.full_report[filename]:
if report_annotation['annotation_token'] == annotation:
annotation_report[filename].append(report_annotation)
self._write_doc_file(
f"All References to Annotation '{annotation}'", f'annotation_{annotation}', annotation_report
)
def render(self):
"""
Perform the rendering of all documentation using the configured Jinja2 templates.
"""
# Generate the top level list of all annotations
self._write_doc_file("Complete Annotation List", 'index', self.full_report)
self._generate_per_choice_docs()
self._generate_per_annotation_docs()