Skip to content

Commit f60763c

Browse files
authored
fix: Propagate app_name to DjanoSearch, fix naming (#119)
* fix: Propagate app_name to DjanoSearch, fix naming The app_name option should always filter on the whole DjangoSearch when present, not just the report. This fixes that. Also updates some variable and output names to be more clear about the state of the models. * build: Stop testing against Python 3.8 * chore: Add catalog-info * chore: Bump version to 1.8.2
1 parent 0eb8375 commit f60763c

File tree

8 files changed

+332
-188
lines changed

8 files changed

+332
-188
lines changed

.github/workflows/ci.yml

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
branches: [master]
66
pull_request:
77
branches:
8-
- '**'
8+
- "**"
99

1010
jobs:
1111
run_tests:
@@ -14,31 +14,31 @@ jobs:
1414
strategy:
1515
matrix:
1616
os: [ubuntu-latest]
17-
python-version: ['3.8', '3.11', '3.12']
17+
python-version: ["3.11", "3.12"]
1818
toxenv: [quality, docs, django42]
1919

2020
steps:
21-
- uses: actions/checkout@v2
22-
- name: setup python
23-
uses: actions/setup-python@v2
24-
with:
25-
python-version: ${{ matrix.python-version }}
21+
- uses: actions/checkout@v2
22+
- name: setup python
23+
uses: actions/setup-python@v2
24+
with:
25+
python-version: ${{ matrix.python-version }}
2626

27-
- name: Install pip
28-
run: pip install -r requirements/pip.txt
27+
- name: Install pip
28+
run: pip install -r requirements/pip.txt
2929

30-
- name: Install Dependencies
31-
run: pip install -r requirements/ci.txt
30+
- name: Install Dependencies
31+
run: pip install -r requirements/ci.txt
3232

33-
- name: Run Tests
34-
env:
35-
TOXENV: ${{ matrix.toxenv }}
36-
run: tox
33+
- name: Run Tests
34+
env:
35+
TOXENV: ${{ matrix.toxenv }}
36+
run: tox
3737

38-
- name: Run Coverage
39-
if: matrix.python-version == '3.8' && matrix.toxenv=='django42'
40-
uses: codecov/codecov-action@v4
41-
with:
42-
token: ${{ secrets.CODECOV_TOKEN }}
43-
flags: unittests
44-
fail_ci_if_error: true
38+
- name: Run Coverage
39+
if: matrix.python-version == '3.11' && matrix.toxenv=='django42'
40+
uses: codecov/codecov-action@v4
41+
with:
42+
token: ${{ secrets.CODECOV_TOKEN }}
43+
flags: unittests
44+
fail_ci_if_error: true

catalog-info.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file records information about this repo. Its use is described in OEP-55:
2+
# https://open-edx-proposals.readthedocs.io/en/latest/processes/oep-0055-proc-project-maintainers.html
3+
4+
apiVersion: backstage.io/v1alpha1
5+
kind: Component
6+
metadata:
7+
name: "code-annotations"
8+
description: "Tools for annotating code comments, linting, and Django model coverage."
9+
spec:
10+
owner: "bmtcril"
11+
type: "library"
12+
lifecycle: "production"

code_annotations/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
Extensible tools for parsing annotations in codebases.
33
"""
44

5-
__version__ = '1.8.1'
5+
__version__ = '1.8.2'

code_annotations/cli.py

Lines changed: 108 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Command line interface for code annotation tools.
33
"""
4+
45
import datetime
56
import sys
67
import traceback
@@ -21,53 +22,88 @@ def entry_point():
2122
"""
2223

2324

24-
@entry_point.command('django_find_annotations')
25+
@entry_point.command("django_find_annotations")
26+
@click.option(
27+
"--config_file",
28+
default=".annotations",
29+
help="Path to the configuration file",
30+
type=click.Path(exists=True, dir_okay=False, resolve_path=True),
31+
)
32+
@click.option(
33+
"--seed_safelist/--no_safelist",
34+
default=False,
35+
show_default=True,
36+
help="Generate an initial safelist file based on the current Django environment.",
37+
)
2538
@click.option(
26-
'--config_file',
27-
default='.annotations',
28-
help='Path to the configuration file',
29-
type=click.Path(exists=True, dir_okay=False, resolve_path=True)
39+
"--list_local_models/--no_list_models",
40+
default=False,
41+
show_default=True,
42+
help="List all locally defined models (in the current repo) that require annotations.",
43+
)
44+
@click.option(
45+
"--app_name",
46+
default=None,
47+
help="(Optional) App name for which coverage is generated.",
3048
)
49+
@click.option("--report_path", default=None, help="Location to write the report")
50+
@click.option("-v", "--verbosity", count=True, help="Verbosity level (-v through -vvv)")
3151
@click.option(
32-
'--seed_safelist/--no_safelist',
52+
"--lint/--no_lint",
53+
help="Enable or disable linting checks",
3354
default=False,
3455
show_default=True,
35-
help='Generate an initial safelist file based on the current Django environment.',
3656
)
3757
@click.option(
38-
'--list_local_models/--no_list_models',
58+
"--report/--no_report",
59+
help="Enable or disable writing the report",
60+
default=False,
61+
show_default=True,
62+
)
63+
@click.option(
64+
"--coverage/--no_coverage",
65+
help="Enable or disable coverage checks",
3966
default=False,
4067
show_default=True,
41-
help='List all locally defined models (in the current repo) that require annotations.',
4268
)
43-
@click.option('--app_name', default='', help='(Optional) App name for which coverage is generated.')
44-
@click.option('--report_path', default=None, help='Location to write the report')
45-
@click.option('-v', '--verbosity', count=True, help='Verbosity level (-v through -vvv)')
46-
@click.option('--lint/--no_lint', help='Enable or disable linting checks', default=False, show_default=True)
47-
@click.option('--report/--no_report', help='Enable or disable writing the report', default=False, show_default=True)
48-
@click.option('--coverage/--no_coverage', help='Enable or disable coverage checks', default=False, show_default=True)
4969
def django_find_annotations(
50-
config_file,
51-
seed_safelist,
52-
list_local_models,
53-
app_name,
54-
report_path,
55-
verbosity,
56-
lint,
57-
report,
58-
coverage
70+
config_file,
71+
seed_safelist,
72+
list_local_models,
73+
app_name,
74+
report_path,
75+
verbosity,
76+
lint,
77+
report,
78+
coverage,
5979
):
6080
"""
6181
Subcommand for dealing with annotations in Django models.
6282
"""
6383
try:
6484
start_time = datetime.datetime.utcnow()
85+
86+
if (
87+
not coverage
88+
and not seed_safelist
89+
and not list_local_models
90+
and not lint
91+
and not report
92+
):
93+
click.echo(
94+
"No actions specified. Please specify one or more of --seed_safelist, --list_local_models, "
95+
"--lint, --report, or --coverage"
96+
)
97+
sys.exit(1)
98+
6599
config = AnnotationConfig(config_file, report_path, verbosity)
66-
searcher = DjangoSearch(config)
100+
searcher = DjangoSearch(config, app_name)
67101

68102
# Early out if we're trying to do coverage, but a coverage target is not configured
69103
if coverage and not config.coverage_target:
70-
raise ConfigurationException("Please add 'coverage_target' to your configuration before running --coverage")
104+
raise ConfigurationException(
105+
"Please add 'coverage_target' to your configuration before running --coverage"
106+
)
71107

72108
if seed_safelist:
73109
searcher.seed_safelist()
@@ -107,32 +143,45 @@ def django_find_annotations(
107143
annotation_count += len(annotated_models[filename])
108144

109145
elapsed = datetime.datetime.utcnow() - start_time
110-
click.echo("Search found {} annotations in {} seconds.".format(
111-
annotation_count, elapsed.total_seconds()
112-
))
113-
146+
click.echo(
147+
"Search found {} annotations in {} seconds.".format(
148+
annotation_count, elapsed.total_seconds()
149+
)
150+
)
114151
except Exception as exc:
115152
click.echo(traceback.print_exc())
116153
fail(str(exc))
117154

118155

119-
@entry_point.command('static_find_annotations')
156+
@entry_point.command("static_find_annotations")
120157
@click.option(
121-
'--config_file',
122-
default='.annotations',
123-
help='Path to the configuration file',
124-
type=click.Path(exists=True, dir_okay=False, resolve_path=True)
158+
"--config_file",
159+
default=".annotations",
160+
help="Path to the configuration file",
161+
type=click.Path(exists=True, dir_okay=False, resolve_path=True),
125162
)
126163
@click.option(
127-
'--source_path',
128-
help='Location of the source code to search',
129-
type=click.Path(exists=True, dir_okay=True, resolve_path=True)
164+
"--source_path",
165+
help="Location of the source code to search",
166+
type=click.Path(exists=True, dir_okay=True, resolve_path=True),
130167
)
131-
@click.option('--report_path', default=None, help='Location to write the report')
132-
@click.option('-v', '--verbosity', count=True, help='Verbosity level (-v through -vvv)')
133-
@click.option('--lint/--no_lint', help='Enable or disable linting checks', default=True, show_default=True)
134-
@click.option('--report/--no_report', help='Enable or disable writing the report file', default=True, show_default=True)
135-
def static_find_annotations(config_file, source_path, report_path, verbosity, lint, report):
168+
@click.option("--report_path", default=None, help="Location to write the report")
169+
@click.option("-v", "--verbosity", count=True, help="Verbosity level (-v through -vvv)")
170+
@click.option(
171+
"--lint/--no_lint",
172+
help="Enable or disable linting checks",
173+
default=True,
174+
show_default=True,
175+
)
176+
@click.option(
177+
"--report/--no_report",
178+
help="Enable or disable writing the report file",
179+
default=True,
180+
show_default=True,
181+
)
182+
def static_find_annotations(
183+
config_file, source_path, report_path, verbosity, lint, report
184+
):
136185
"""
137186
Subcommand to find annotations via static file analysis.
138187
"""
@@ -176,18 +225,14 @@ def static_find_annotations(config_file, source_path, report_path, verbosity, li
176225

177226
@entry_point.command("generate_docs")
178227
@click.option(
179-
'--config_file',
180-
default='.annotations',
181-
help='Path to the configuration file',
182-
type=click.Path(exists=True, dir_okay=False)
228+
"--config_file",
229+
default=".annotations",
230+
help="Path to the configuration file",
231+
type=click.Path(exists=True, dir_okay=False),
183232
)
184-
@click.option('-v', '--verbosity', count=True, help='Verbosity level (-v through -vvv)')
185-
@click.argument("report_files", type=click.File('r'), nargs=-1)
186-
def generate_docs(
187-
config_file,
188-
verbosity,
189-
report_files
190-
):
233+
@click.option("-v", "--verbosity", count=True, help="Verbosity level (-v through -vvv)")
234+
@click.argument("report_files", type=click.File("r"), nargs=-1)
235+
def generate_docs(config_file, verbosity, report_files):
191236
"""
192237
Generate documentation from a code annotations report.
193238
"""
@@ -197,15 +242,19 @@ def generate_docs(
197242
config = AnnotationConfig(config_file, verbosity)
198243

199244
for key in (
200-
'report_template_dir',
201-
'rendered_report_dir',
202-
'rendered_report_file_extension',
203-
'rendered_report_source_link_prefix'
245+
"report_template_dir",
246+
"rendered_report_dir",
247+
"rendered_report_file_extension",
248+
"rendered_report_source_link_prefix",
204249
):
205250
if not getattr(config, key):
206251
raise ConfigurationException(f"No {key} key in {config_file}")
207252

208-
config.echo("Rendering the following reports: \n{}".format("\n".join([r.name for r in report_files])))
253+
config.echo(
254+
"Rendering the following reports: \n{}".format(
255+
"\n".join([r.name for r in report_files])
256+
)
257+
)
209258

210259
renderer = ReportRenderer(config, report_files)
211260
renderer.render()

0 commit comments

Comments
 (0)