Skip to content

Commit 28debe3

Browse files
authored
Merge pull request #140 from openedx/bmtcril/finish_rendering
Simple local report rendering
2 parents 5c4397a + 5058d4b commit 28debe3

22 files changed

+351
-72
lines changed

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ include CONTRIBUTING.rst
44
include LICENSE.txt
55
include README.rst
66
include requirements/base.in
7-
recursive-include code_annotations *.html *.png *.gif *js *.css *jpg *jpeg *svg *py *.yaml *.yml
7+
recursive-include code_annotations *.tpl *.html *.png *.gif *js *.css *jpg *jpeg *svg *py *.yaml *.yml

README.rst

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
code-annotations
22
=============================
33

4-
|pypi-badge| |CI| |codecov-badge| |doc-badge| |pyversions-badge|
5-
|license-badge|
6-
74
Extensible tools for parsing annotations in codebases
85

96
Overview
@@ -55,28 +52,3 @@ Have a question about this repository, or about Open edX in general? Please
5552
refer to this `list of resources`_ if you need any assistance.
5653

5754
.. _list of resources: https://open.edx.org/getting-help
58-
59-
60-
.. |pypi-badge| image:: https://img.shields.io/pypi/v/code-annotations.svg
61-
:target: https://pypi.python.org/pypi/code-annotations/
62-
:alt: PyPI
63-
64-
.. |CI| image:: https://github.com/openedx/code-annotations/workflows/Python%20CI/badge.svg?branch=master
65-
:target: https://github.com/openedx/code-annotations/actions?query=workflow%3A%22Python+CI%22
66-
:alt: CI
67-
68-
.. |codecov-badge| image:: http://codecov.io/github/edx/code-annotations/coverage.svg?branch=master
69-
:target: http://codecov.io/github/edx/code-annotations?branch=master
70-
:alt: Codecov
71-
72-
.. |doc-badge| image:: https://readthedocs.org/projects/code-annotations/badge/?version=latest
73-
:target: http://code-annotations.readthedocs.io/en/latest/
74-
:alt: Documentation
75-
76-
.. |pyversions-badge| image:: https://img.shields.io/pypi/pyversions/code-annotations.svg
77-
:target: https://pypi.python.org/pypi/code-annotations/
78-
:alt: Supported Python versions
79-
80-
.. |license-badge| image:: https://img.shields.io/github/license/edx/code-annotations.svg
81-
:target: https://github.com/openedx/code-annotations/blob/master/LICENSE.txt
82-
:alt: License

code_annotations/base.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
from code_annotations.exceptions import ConfigurationException
1515
from code_annotations.helpers import VerboseEcho
1616

17+
PACKAGE_DIR = os.path.dirname(os.path.realpath(__file__))
18+
DEFAULT_TEMPLATE_DIR = os.path.join(PACKAGE_DIR, "report_templates")
19+
1720

1821
class AnnotationConfig:
1922
"""
@@ -58,10 +61,17 @@ def __init__(self, config_file_path, report_path_override=None, verbosity=1, sou
5861
self.echo(f"Configured for source path: {self.source_path}")
5962

6063
self._configure_coverage(raw_config.get('coverage_target', None))
61-
self.report_template_dir = raw_config.get('report_template_dir')
62-
self.rendered_report_dir = raw_config.get('rendered_report_dir')
63-
self.rendered_report_file_extension = raw_config.get('rendered_report_file_extension')
64-
self.rendered_report_source_link_prefix = raw_config.get('rendered_report_source_link_prefix')
64+
65+
self.rendered_report_format = raw_config.get('rendered_report_format', 'rst')
66+
self.report_template_dir = raw_config.get(
67+
'report_template_dir',
68+
os.path.join(DEFAULT_TEMPLATE_DIR, self.rendered_report_format)
69+
)
70+
self.rendered_report_dir = raw_config.get('rendered_report_dir', 'annotation_reports')
71+
self.rendered_report_source_link_prefix = raw_config.get('rendered_report_source_link_prefix', None)
72+
self.trim_filename_prefixes = raw_config.get('trim_filename_prefixes', [])
73+
self.third_party_package_location = raw_config.get('third_party_package_location', "site-packages")
74+
self.rendered_report_file_extension = f".{self.rendered_report_format}"
6575

6676
self._configure_annotations(raw_config)
6777
self._configure_extensions()

code_annotations/cli.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,15 @@ def generate_docs(config_file, verbosity, report_files):
241241
try:
242242
config = AnnotationConfig(config_file, verbosity)
243243

244+
if not report_files:
245+
raise ConfigurationException(
246+
"No report files provided. Please provide one or more report files to generate docs from."
247+
)
248+
244249
for key in (
245250
"report_template_dir",
246251
"rendered_report_dir",
247-
"rendered_report_file_extension",
252+
"rendered_report_format",
248253
"rendered_report_source_link_prefix",
249254
):
250255
if not getattr(config, key):

code_annotations/generate_docs.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ def __init__(self, config, report_files):
2727
self.config = config
2828
self.echo = self.config.echo
2929
self.report_files = report_files
30-
self.create_time = datetime.datetime.utcnow().isoformat()
31-
30+
self.create_time = datetime.datetime.now(tz=datetime.timezone.utc)
3231
self.full_report = self._aggregate_reports()
3332

3433
self.jinja_environment = jinja2.Environment(
@@ -62,6 +61,12 @@ def _add_report_file_to_full_report(self, report_file, report):
6261
loaded_report = yaml.safe_load(report_file)
6362

6463
for filename in loaded_report:
64+
trimmed_filename = filename
65+
for prefix in self.config.trim_filename_prefixes:
66+
if filename.startswith(prefix):
67+
trimmed_filename = filename[len(prefix):]
68+
break
69+
6570
if filename in report:
6671
for loaded_annotation in loaded_report[filename]:
6772
found = False
@@ -74,9 +79,9 @@ def _add_report_file_to_full_report(self, report_file, report):
7479
break
7580

7681
if not found:
77-
report[filename].append(loaded_annotation)
82+
report[trimmed_filename].append(loaded_annotation)
7883
else:
79-
report[filename] = loaded_report[filename]
84+
report[trimmed_filename] = loaded_report[filename]
8085

8186
def _aggregate_reports(self):
8287
"""
@@ -91,11 +96,12 @@ def _aggregate_reports(self):
9196

9297
return report
9398

94-
def _write_doc_file(self, doc_filename, doc_data):
99+
def _write_doc_file(self, doc_title, doc_filename, doc_data):
95100
"""
96101
Write out a single report file with the given data. This is rendered using the configured top level template.
97102
98103
Args:
104+
doc_title: Title to use for the document.
99105
doc_filename: Filename to write to.
100106
doc_data: Dict of reporting data to use, in the {'file name': [list, of, annotations,]} style.
101107
"""
@@ -110,14 +116,16 @@ def _write_doc_file(self, doc_filename, doc_data):
110116

111117
with open(full_doc_filename, 'w') as output:
112118
output.write(self.top_level_template.render(
119+
doc_title=doc_title,
113120
create_time=self.create_time,
114121
report=doc_data,
115122
all_choices=self.all_choices,
116123
all_annotations=self.config.annotation_tokens,
117124
group_mapping=self.group_mapping,
118125
slugify=slugify,
119-
source_link_prefix=self.config.rendered_report_source_link_prefix)
120-
)
126+
source_link_prefix=self.config.rendered_report_source_link_prefix,
127+
third_party_package_location=self.config.third_party_package_location,
128+
))
121129

122130
def _generate_per_choice_docs(self):
123131
"""
@@ -130,7 +138,7 @@ def _generate_per_choice_docs(self):
130138
if isinstance(annotation['annotation_data'], list) and choice in annotation['annotation_data']:
131139
choice_report[filename].append(annotation)
132140

133-
self._write_doc_file(f'choice_{choice}', choice_report)
141+
self._write_doc_file(f"All References to Choice '{choice}'", f'choice_{choice}', choice_report)
134142

135143
def _generate_per_annotation_docs(self):
136144
"""
@@ -143,13 +151,15 @@ def _generate_per_annotation_docs(self):
143151
if report_annotation['annotation_token'] == annotation:
144152
annotation_report[filename].append(report_annotation)
145153

146-
self._write_doc_file(f'annotation_{annotation}', annotation_report)
154+
self._write_doc_file(
155+
f"All References to Annotation '{annotation}'", f'annotation_{annotation}', annotation_report
156+
)
147157

148158
def render(self):
149159
"""
150160
Perform the rendering of all documentation using the configured Jinja2 templates.
151161
"""
152162
# Generate the top level list of all annotations
153-
self._write_doc_file('index', self.full_report)
163+
self._write_doc_file("Complete Annotation List", 'index', self.full_report)
154164
self._generate_per_choice_docs()
155165
self._generate_per_annotation_docs()

code_annotations/report_templates/annotation.tpl

Lines changed: 0 additions & 5 deletions
This file was deleted.

code_annotations/report_templates/annotation_group.tpl

Lines changed: 0 additions & 3 deletions
This file was deleted.

code_annotations/report_templates/base.tpl

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{% if is_third_party %}
2+
{# no links for third party code since we don't know where to link to #}
3+
{% if annotation.extra and annotation.extra.object_id %}
4+
{{ annotation.extra.object_id }} {% if annotation.line_number > 0 %}line {{ annotation.line_number }}{% endif %}: {{ annotation.annotation_token }} {% include "annotation_data.tpl" %}
5+
{% else %}
6+
{% if loop.changed(annotation.line_number)%}{{ filename }}:{{ annotation.line_number }}<br />{% endif %}:
7+
{{ annotation.annotation_token }} {% include "annotation_data.tpl" %}
8+
{% endif %}
9+
10+
{% elif annotation.extra and annotation.extra.object_id %}
11+
<a href="{{ source_link_prefix }}{{ filename }}#L{{ annotation.line_number }}" target="_blank">{{ annotation.extra.object_id }} {% if annotation.line_number > 0 %}line {{ annotation.line_number }}{% endif %}</a>: {{ annotation.annotation_token }} {% include "annotation_data.tpl" %}
12+
{% else %}
13+
<a href="{{ source_link_prefix }}{{ filename }}#L{{ annotation.line_number }}" target="_blank">`{{ filename }}:{{ annotation.line_number }}: {{ annotation.annotation_token }} {% include "annotation_data.tpl" %}
14+
{% endif %}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% if annotation.annotation_data is sequence and annotation.annotation_data is not string %}
2+
{% for a in annotation.annotation_data %}
3+
<a href="choice-{{ slugify(a) }}.html">{{ a }}</a>{% if not loop.last %}, {% endif %}
4+
{% endfor %}
5+
6+
{% else %}
7+
{{ annotation.annotation_data }}
8+
{% endif %}

0 commit comments

Comments
 (0)