|
| 1 | +""" |
| 2 | +Contains functionality for turning YAML reports into human-readable documentation. |
| 3 | +""" |
| 4 | + |
| 5 | +import collections |
| 6 | +import datetime |
| 7 | +import os |
| 8 | + |
| 9 | +import jinja2 |
| 10 | +import yaml |
| 11 | +from slugify import slugify |
| 12 | + |
| 13 | + |
| 14 | +class ReportRenderer(object): |
| 15 | + """ |
| 16 | + Generates human readable documentation from YAML reports. |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, config, report_files): |
| 20 | + """ |
| 21 | + Initialize a ReportRenderer. |
| 22 | +
|
| 23 | + Args: |
| 24 | + config: An AnnotationConfig object |
| 25 | + report_files: A list of files to combine and report on |
| 26 | + """ |
| 27 | + self.config = config |
| 28 | + self.echo = self.config.echo |
| 29 | + self.report_files = report_files |
| 30 | + self.create_time = datetime.datetime.now().isoformat() |
| 31 | + |
| 32 | + self.full_report = self._aggregate_reports() |
| 33 | + |
| 34 | + self.jinja_environment = jinja2.Environment( |
| 35 | + autoescape=False, |
| 36 | + loader=jinja2.FileSystemLoader(self.config.report_template_dir), |
| 37 | + lstrip_blocks=True, |
| 38 | + trim_blocks=True |
| 39 | + ) |
| 40 | + self.top_level_template = self.jinja_environment.get_template('annotation_list.tpl') |
| 41 | + self.all_choices = [] |
| 42 | + self.group_mapping = {} |
| 43 | + |
| 44 | + for token in self.config.choices: |
| 45 | + self.all_choices.extend(self.config.choices[token]) |
| 46 | + |
| 47 | + for group_name in self.config.groups: |
| 48 | + for token in self.config.groups[group_name]: |
| 49 | + self.group_mapping[token] = group_name |
| 50 | + |
| 51 | + def _add_report_file_to_full_report(self, report_file, report): |
| 52 | + loaded_report = yaml.safe_load(report_file) |
| 53 | + |
| 54 | + for filename in loaded_report: |
| 55 | + if filename in report: |
| 56 | + for loaded_annotation in loaded_report[filename]: |
| 57 | + found = False |
| 58 | + for report_annotation in report[filename]: |
| 59 | + index_keys = ('line_number', 'annotation_token', 'annotation_data') |
| 60 | + |
| 61 | + if all([loaded_annotation[k] == report_annotation[k] for k in index_keys]): |
| 62 | + report_annotation.update(loaded_annotation) |
| 63 | + found = True |
| 64 | + break |
| 65 | + |
| 66 | + if not found: |
| 67 | + report[filename].append(loaded_annotation) |
| 68 | + else: |
| 69 | + report[filename] = loaded_report[filename] |
| 70 | + |
| 71 | + def _aggregate_reports(self): |
| 72 | + """ |
| 73 | + Combine all of the given report files into a single report object. |
| 74 | + """ |
| 75 | + report = collections.defaultdict(list) |
| 76 | + |
| 77 | + # Combine report files into a single dict. If there are duplicate annotations, make sure we have the superset |
| 78 | + # of data. |
| 79 | + for r in self.report_files: |
| 80 | + self._add_report_file_to_full_report(r, report) |
| 81 | + |
| 82 | + return report |
| 83 | + |
| 84 | + def _write_doc_file(self, doc_filename, doc_data): |
| 85 | + """ |
| 86 | + Write out a single report file with the given data. This is rendered using the configured top level template. |
| 87 | +
|
| 88 | + Args: |
| 89 | + doc_filename: Filename to write to. |
| 90 | + doc_data: Dict of reporting data to use, in the {'file name': [list, of, annotations,]} style. |
| 91 | + """ |
| 92 | + full_doc_filename = os.path.join( |
| 93 | + self.config.rendered_report_dir, |
| 94 | + slugify(doc_filename) |
| 95 | + ) |
| 96 | + |
| 97 | + full_doc_filename += self.config.rendered_report_file_extension |
| 98 | + |
| 99 | + self.echo.echo_v('Writing {}'.format(full_doc_filename)) |
| 100 | + |
| 101 | + with open(full_doc_filename, 'w') as output: |
| 102 | + output.write(self.top_level_template.render( |
| 103 | + create_time=self.create_time, |
| 104 | + report=doc_data, |
| 105 | + all_choices=self.all_choices, |
| 106 | + all_annotations=self.config.annotation_tokens, |
| 107 | + group_mapping=self.group_mapping, |
| 108 | + slugify=slugify, |
| 109 | + source_link_prefix=self.config.rendered_report_source_link_prefix) |
| 110 | + ) |
| 111 | + |
| 112 | + def _generate_per_choice_docs(self): |
| 113 | + """ |
| 114 | + Generate a page of documentation for each configured annotation choice. |
| 115 | + """ |
| 116 | + for choice in self.all_choices: |
| 117 | + choice_report = collections.defaultdict(list) |
| 118 | + for filename in self.full_report: |
| 119 | + for annotation in self.full_report[filename]: |
| 120 | + if isinstance(annotation['annotation_data'], list) and choice in annotation['annotation_data']: |
| 121 | + choice_report[filename].append(annotation) |
| 122 | + |
| 123 | + self._write_doc_file('choice_{}'.format(choice), choice_report) |
| 124 | + |
| 125 | + def _generate_per_annotation_docs(self): |
| 126 | + """ |
| 127 | + Generate a page of documentation for each configured annotation. |
| 128 | + """ |
| 129 | + for annotation in self.config.annotation_tokens: |
| 130 | + annotation_report = collections.defaultdict(list) |
| 131 | + for filename in self.full_report: |
| 132 | + for report_annotation in self.full_report[filename]: |
| 133 | + if report_annotation['annotation_token'] == annotation: |
| 134 | + annotation_report[filename].append(report_annotation) |
| 135 | + |
| 136 | + self._write_doc_file('annotation_{}'.format(annotation), annotation_report) |
| 137 | + |
| 138 | + def render(self): |
| 139 | + """ |
| 140 | + Perform the rendering of all documentation using the configured Jinja2 templates. |
| 141 | + """ |
| 142 | + # Generate the top level list of all annotations |
| 143 | + self._write_doc_file('index', self.full_report) |
| 144 | + self._generate_per_choice_docs() |
| 145 | + self._generate_per_annotation_docs() |
0 commit comments