11"""
22Command line interface for code annotation tools.
33"""
4+
45import datetime
56import sys
67import 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 )
4969def 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