1
1
"""
2
2
Command line interface for code annotation tools.
3
3
"""
4
+
4
5
import datetime
5
6
import sys
6
7
import traceback
@@ -21,47 +22,72 @@ def entry_point():
21
22
"""
22
23
23
24
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
+ )
25
38
@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." ,
48
+ )
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)" )
51
+ @click .option (
52
+ "--lint/--no_lint" ,
53
+ help = "Enable or disable linting checks" ,
54
+ default = False ,
55
+ show_default = True ,
30
56
)
31
57
@click .option (
32
- '--seed_safelist/--no_safelist' ,
58
+ "--report/--no_report" ,
59
+ help = "Enable or disable writing the report" ,
33
60
default = False ,
34
61
show_default = True ,
35
- help = 'Generate an initial safelist file based on the current Django environment.' ,
36
62
)
37
63
@click .option (
38
- '--list_local_models/--no_list_models' ,
64
+ "--coverage/--no_coverage" ,
65
+ help = "Enable or disable coverage checks" ,
39
66
default = False ,
40
67
show_default = True ,
41
- help = 'List all locally defined models (in the current repo) that require annotations.' ,
42
68
)
43
- @click .option ('--app_name' , default = None , 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 )
49
69
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 ,
59
79
):
60
80
"""
61
81
Subcommand for dealing with annotations in Django models.
62
82
"""
63
83
try :
64
- if not coverage and not seed_safelist and not list_local_models and not lint and not report and not coverage :
84
+ if (
85
+ not coverage
86
+ and not seed_safelist
87
+ and not list_local_models
88
+ and not lint
89
+ and not report
90
+ ):
65
91
click .echo (
66
92
"No actions specified. Please specify one or more of --seed_safelist, --list_local_models, "
67
93
"--lint, --report, or --coverage"
@@ -74,7 +100,9 @@ def django_find_annotations(
74
100
75
101
# Early out if we're trying to do coverage, but a coverage target is not configured
76
102
if coverage and not config .coverage_target :
77
- raise ConfigurationException ("Please add 'coverage_target' to your configuration before running --coverage" )
103
+ raise ConfigurationException (
104
+ "Please add 'coverage_target' to your configuration before running --coverage"
105
+ )
78
106
79
107
if seed_safelist :
80
108
searcher .seed_safelist ()
@@ -114,32 +142,46 @@ def django_find_annotations(
114
142
annotation_count += len (annotated_models [filename ])
115
143
116
144
elapsed = datetime .datetime .now () - start_time
117
- click .echo ("Search found {} annotations in {} seconds." .format (
118
- annotation_count , elapsed .total_seconds ()
119
- ))
145
+ click .echo (
146
+ "Search found {} annotations in {} seconds." .format (
147
+ annotation_count , elapsed .total_seconds ()
148
+ )
149
+ )
120
150
121
151
except Exception as exc :
122
152
click .echo (traceback .print_exc ())
123
153
fail (str (exc ))
124
154
125
155
126
- @entry_point .command ('static_find_annotations' )
156
+ @entry_point .command ("static_find_annotations" )
157
+ @click .option (
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 ),
162
+ )
127
163
@click .option (
128
- '--config_file' ,
129
- default = '.annotations' ,
130
- help = 'Path to the configuration file' ,
131
- type = click .Path (exists = True , dir_okay = False , 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 ),
132
167
)
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)" )
133
170
@click .option (
134
- '--source_path' ,
135
- help = 'Location of the source code to search' ,
136
- type = click .Path (exists = True , dir_okay = True , resolve_path = True )
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 ,
137
181
)
138
- @click .option ('--report_path' , default = None , help = 'Location to write the report' )
139
- @click .option ('-v' , '--verbosity' , count = True , help = 'Verbosity level (-v through -vvv)' )
140
- @click .option ('--lint/--no_lint' , help = 'Enable or disable linting checks' , default = True , show_default = True )
141
- @click .option ('--report/--no_report' , help = 'Enable or disable writing the report file' , default = True , show_default = True )
142
- def static_find_annotations (config_file , source_path , report_path , verbosity , lint , report ):
182
+ def static_find_annotations (
183
+ config_file , source_path , report_path , verbosity , lint , report
184
+ ):
143
185
"""
144
186
Subcommand to find annotations via static file analysis.
145
187
"""
@@ -183,18 +225,14 @@ def static_find_annotations(config_file, source_path, report_path, verbosity, li
183
225
184
226
@entry_point .command ("generate_docs" )
185
227
@click .option (
186
- ' --config_file' ,
187
- default = ' .annotations' ,
188
- help = ' Path to the configuration file' ,
189
- 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 ),
190
232
)
191
- @click .option ('-v' , '--verbosity' , count = True , help = 'Verbosity level (-v through -vvv)' )
192
- @click .argument ("report_files" , type = click .File ('r' ), nargs = - 1 )
193
- def generate_docs (
194
- config_file ,
195
- verbosity ,
196
- report_files
197
- ):
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 ):
198
236
"""
199
237
Generate documentation from a code annotations report.
200
238
"""
@@ -204,15 +242,19 @@ def generate_docs(
204
242
config = AnnotationConfig (config_file , verbosity )
205
243
206
244
for key in (
207
- ' report_template_dir' ,
208
- ' rendered_report_dir' ,
209
- ' rendered_report_file_extension' ,
210
- ' rendered_report_source_link_prefix'
245
+ " report_template_dir" ,
246
+ " rendered_report_dir" ,
247
+ " rendered_report_file_extension" ,
248
+ " rendered_report_source_link_prefix" ,
211
249
):
212
250
if not getattr (config , key ):
213
251
raise ConfigurationException (f"No { key } key in { config_file } " )
214
252
215
- 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
+ )
216
258
217
259
renderer = ReportRenderer (config , report_files )
218
260
renderer .render ()
0 commit comments