diff options
author | Joshua Harlow <harlowja@gmail.com> | 2014-09-06 17:48:54 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2014-09-08 11:42:38 -0700 |
commit | 355029419eabef896adc809dedb58951f03f4c8d (patch) | |
tree | 5a2147ba13e0bfefe5d10e160b443525eb0e0f19 | |
parent | 89779ed115860d7f590448d80d61a7e45b74883e (diff) |
Split the main function into pieces
Have there be a scanning function that contained the
previous file scanning logic, then a validating function
that did the checking logic and then have the main function
call into those functions instead of containing all the
same logic in its own function.
Change-Id: Ie35665a016164122be294fee41e7ce54605d832e
Notes
Notes (review):
Verified+2: Jenkins
Code-Review+2: Joshua Harlow <harlowja@yahoo-inc.com>
Workflow+1: Joshua Harlow <harlowja@yahoo-inc.com>
Submitted-by: Jenkins
Submitted-at: Thu, 11 Sep 2014 20:08:15 +0000
Reviewed-on: https://review.openstack.org/119582
Project: stackforge/doc8
Branch: refs/heads/master
-rw-r--r-- | doc8/main.py | 152 |
1 files changed, 81 insertions, 71 deletions
diff --git a/doc8/main.py b/doc8/main.py index 95ebab0..eaabdd1 100644 --- a/doc8/main.py +++ b/doc8/main.py | |||
@@ -148,86 +148,34 @@ def setup_logging(verbose): | |||
148 | format='%(levelname)s: %(message)s', stream=sys.stdout) | 148 | format='%(levelname)s: %(message)s', stream=sys.stdout) |
149 | 149 | ||
150 | 150 | ||
151 | def main(): | 151 | def scan(cfg): |
152 | parser = argparse.ArgumentParser( | ||
153 | prog='doc8', | ||
154 | description=__doc__, | ||
155 | formatter_class=argparse.RawDescriptionHelpFormatter) | ||
156 | default_configs = ", ".join(CONFIG_FILENAMES) | ||
157 | parser.add_argument("paths", metavar='path', type=str, nargs='*', | ||
158 | help=("Path to scan for doc files" | ||
159 | " (default: current directory)."), | ||
160 | default=[os.getcwd()]) | ||
161 | parser.add_argument("--config", metavar='path', action="append", | ||
162 | help="User config file location" | ||
163 | " (default: %s)." % default_configs, | ||
164 | default=[]) | ||
165 | parser.add_argument("--allow-long-titles", action="store_true", | ||
166 | help="Allow long section titles (default: False).", | ||
167 | default=False) | ||
168 | parser.add_argument("--ignore", action="append", metavar="code", | ||
169 | help="Ignore the given error code(s).", | ||
170 | type=split_set_type, | ||
171 | default=[]) | ||
172 | parser.add_argument("--no-sphinx", action="store_false", | ||
173 | help="Do not ignore sphinx specific false positives.", | ||
174 | default=True, dest='sphinx') | ||
175 | parser.add_argument("--ignore-path", action="append", default=[], | ||
176 | help="Ignore the given directory or file (globs" | ||
177 | " are supported).", metavar='path') | ||
178 | parser.add_argument("--max-line-length", action="store", metavar="int", | ||
179 | type=int, | ||
180 | help="Maximum allowed line" | ||
181 | " length (default: %s)." % MAX_LINE_LENGTH, | ||
182 | default=MAX_LINE_LENGTH) | ||
183 | parser.add_argument("-e", "--extension", action="append", | ||
184 | metavar="extension", | ||
185 | help="Check file extensions of the given type" | ||
186 | " (default: %s)." % ", ".join(FILE_PATTERNS), | ||
187 | default=list(FILE_PATTERNS)) | ||
188 | parser.add_argument("-v", "--verbose", dest="verbose", action='store_true', | ||
189 | help="Run in verbose mode.", default=False) | ||
190 | parser.add_argument("--version", dest="version", action='store_true', | ||
191 | help="Show the version and exit.", default=False) | ||
192 | args = vars(parser.parse_args()) | ||
193 | if args.get('version'): | ||
194 | print(version.version_string()) | ||
195 | return 0 | ||
196 | args['ignore'] = merge_sets(args['ignore']) | ||
197 | cfg = extract_config(args) | ||
198 | args['ignore'].update(cfg.pop("ignore", set())) | ||
199 | if 'sphinx' in cfg: | ||
200 | args['sphinx'] = cfg.pop("sphinx") | ||
201 | args['extension'].extend(cfg.pop('extension', [])) | ||
202 | args['ignore_path'].extend(cfg.pop('ignore_path', [])) | ||
203 | args.update(cfg) | ||
204 | setup_logging(args.get('verbose')) | ||
205 | |||
206 | print("Scanning...") | 152 | print("Scanning...") |
207 | files = collections.deque() | 153 | files = collections.deque() |
208 | ignored_paths = args.pop('ignore_path') | 154 | ignored_paths = cfg.pop('ignore_path') |
209 | files_ignored = 0 | 155 | files_ignored = 0 |
210 | files_selected = 0 | 156 | file_iter = utils.find_files(cfg.pop('paths', []), |
211 | file_iter = utils.find_files(args.pop('paths', []), | 157 | cfg.pop('extension', []), ignored_paths) |
212 | args.pop('extension', []), ignored_paths) | ||
213 | for filename, ignoreable in file_iter: | 158 | for filename, ignoreable in file_iter: |
214 | if ignoreable: | 159 | if ignoreable: |
215 | files_ignored += 1 | 160 | files_ignored += 1 |
216 | if args.get('verbose'): | 161 | if cfg.get('verbose'): |
217 | print(" Ignoring '%s'" % (filename)) | 162 | print(" Ignoring '%s'" % (filename)) |
218 | else: | 163 | else: |
219 | files_selected += 1 | ||
220 | files.append(file_parser.parse(filename)) | 164 | files.append(file_parser.parse(filename)) |
221 | if args.get('verbose'): | 165 | if cfg.get('verbose'): |
222 | print(" Selecting '%s'" % (filename)) | 166 | print(" Selecting '%s'" % (filename)) |
167 | return (files, files_ignored) | ||
168 | |||
223 | 169 | ||
224 | ignoreables = frozenset(args.pop('ignore', [])) | 170 | def validate(cfg, files): |
171 | print("Validating...") | ||
225 | error_counts = {} | 172 | error_counts = {} |
173 | ignoreables = frozenset(cfg.pop('ignore', [])) | ||
226 | while files: | 174 | while files: |
227 | f = files.popleft() | 175 | f = files.popleft() |
228 | if args.get('verbose'): | 176 | if cfg.get('verbose'): |
229 | print("Validating %s" % f) | 177 | print("Validating %s" % f) |
230 | for c in fetch_checks(args): | 178 | for c in fetch_checks(cfg): |
231 | try: | 179 | try: |
232 | # http://legacy.python.org/dev/peps/pep-3155/ | 180 | # http://legacy.python.org/dev/peps/pep-3155/ |
233 | check_name = c.__class__.__qualname__ | 181 | check_name = c.__class__.__qualname__ |
@@ -241,7 +189,7 @@ def main(): | |||
241 | pass | 189 | pass |
242 | else: | 190 | else: |
243 | if not extension_matcher.match(f.extension): | 191 | if not extension_matcher.match(f.extension): |
244 | if args.get('verbose'): | 192 | if cfg.get('verbose'): |
245 | print(" Skipping check '%s' since it does not" | 193 | print(" Skipping check '%s' since it does not" |
246 | " understand parsing a file with extension '%s'" | 194 | " understand parsing a file with extension '%s'" |
247 | % (check_name, f.extension)) | 195 | % (check_name, f.extension)) |
@@ -253,17 +201,17 @@ def main(): | |||
253 | else: | 201 | else: |
254 | reports = reports - ignoreables | 202 | reports = reports - ignoreables |
255 | if not reports: | 203 | if not reports: |
256 | if args.get('verbose'): | 204 | if cfg.get('verbose'): |
257 | print(" Skipping check '%s', determined to only" | 205 | print(" Skipping check '%s', determined to only" |
258 | " check ignoreable codes" % check_name) | 206 | " check ignoreable codes" % check_name) |
259 | continue | 207 | continue |
260 | if args.get('verbose'): | 208 | if cfg.get('verbose'): |
261 | print(" Running check '%s'" % check_name) | 209 | print(" Running check '%s'" % check_name) |
262 | if isinstance(c, checks.ContentCheck): | 210 | if isinstance(c, checks.ContentCheck): |
263 | for line_num, code, message in c.report_iter(f): | 211 | for line_num, code, message in c.report_iter(f): |
264 | if code in ignoreables: | 212 | if code in ignoreables: |
265 | continue | 213 | continue |
266 | if args.get('verbose'): | 214 | if cfg.get('verbose'): |
267 | print(' - %s:%s: %s %s' | 215 | print(' - %s:%s: %s %s' |
268 | % (f.filename, line_num, code, message)) | 216 | % (f.filename, line_num, code, message)) |
269 | else: | 217 | else: |
@@ -275,7 +223,7 @@ def main(): | |||
275 | for code, message in c.report_iter(line): | 223 | for code, message in c.report_iter(line): |
276 | if code in ignoreables: | 224 | if code in ignoreables: |
277 | continue | 225 | continue |
278 | if args.get('verbose'): | 226 | if cfg.get('verbose'): |
279 | print(' - %s:%s: %s %s' | 227 | print(' - %s:%s: %s %s' |
280 | % (f.filename, line_num, code, message)) | 228 | % (f.filename, line_num, code, message)) |
281 | else: | 229 | else: |
@@ -285,11 +233,73 @@ def main(): | |||
285 | else: | 233 | else: |
286 | raise TypeError("Unknown check type: %s, %s" | 234 | raise TypeError("Unknown check type: %s, %s" |
287 | % (type(c), c)) | 235 | % (type(c), c)) |
236 | return error_counts | ||
237 | |||
238 | |||
239 | def main(): | ||
240 | parser = argparse.ArgumentParser( | ||
241 | prog='doc8', | ||
242 | description=__doc__, | ||
243 | formatter_class=argparse.RawDescriptionHelpFormatter) | ||
244 | default_configs = ", ".join(CONFIG_FILENAMES) | ||
245 | parser.add_argument("paths", metavar='path', type=str, nargs='*', | ||
246 | help=("Path to scan for doc files" | ||
247 | " (default: current directory)."), | ||
248 | default=[os.getcwd()]) | ||
249 | parser.add_argument("--config", metavar='path', action="append", | ||
250 | help="User config file location" | ||
251 | " (default: %s)." % default_configs, | ||
252 | default=[]) | ||
253 | parser.add_argument("--allow-long-titles", action="store_true", | ||
254 | help="Allow long section titles (default: False).", | ||
255 | default=False) | ||
256 | parser.add_argument("--ignore", action="append", metavar="code", | ||
257 | help="Ignore the given error code(s).", | ||
258 | type=split_set_type, | ||
259 | default=[]) | ||
260 | parser.add_argument("--no-sphinx", action="store_false", | ||
261 | help="Do not ignore sphinx specific false positives.", | ||
262 | default=True, dest='sphinx') | ||
263 | parser.add_argument("--ignore-path", action="append", default=[], | ||
264 | help="Ignore the given directory or file (globs" | ||
265 | " are supported).", metavar='path') | ||
266 | parser.add_argument("--max-line-length", action="store", metavar="int", | ||
267 | type=int, | ||
268 | help="Maximum allowed line" | ||
269 | " length (default: %s)." % MAX_LINE_LENGTH, | ||
270 | default=MAX_LINE_LENGTH) | ||
271 | parser.add_argument("-e", "--extension", action="append", | ||
272 | metavar="extension", | ||
273 | help="Check file extensions of the given type" | ||
274 | " (default: %s)." % ", ".join(FILE_PATTERNS), | ||
275 | default=list(FILE_PATTERNS)) | ||
276 | parser.add_argument("-v", "--verbose", dest="verbose", action='store_true', | ||
277 | help="Run in verbose mode.", default=False) | ||
278 | parser.add_argument("--version", dest="version", action='store_true', | ||
279 | help="Show the version and exit.", default=False) | ||
280 | args = vars(parser.parse_args()) | ||
281 | if args.get('version'): | ||
282 | print(version.version_string()) | ||
283 | return 0 | ||
284 | args['ignore'] = merge_sets(args['ignore']) | ||
285 | cfg = extract_config(args) | ||
286 | args['ignore'].update(cfg.pop("ignore", set())) | ||
287 | if 'sphinx' in cfg: | ||
288 | args['sphinx'] = cfg.pop("sphinx") | ||
289 | args['extension'].extend(cfg.pop('extension', [])) | ||
290 | args['ignore_path'].extend(cfg.pop('ignore_path', [])) | ||
291 | args.update(cfg) | ||
292 | setup_logging(args.get('verbose')) | ||
293 | |||
294 | files, files_ignored = scan(args) | ||
295 | files_selected = len(files) | ||
296 | error_counts = validate(args, files) | ||
288 | total_errors = sum(six.itervalues(error_counts)) | 297 | total_errors = sum(six.itervalues(error_counts)) |
298 | |||
289 | print("=" * 8) | 299 | print("=" * 8) |
290 | print("Total files scanned = %s" % (files_selected)) | 300 | print("Total files scanned = %s" % (files_selected)) |
291 | print("Total files ignored = %s" % (files_ignored)) | 301 | print("Total files ignored = %s" % (files_ignored)) |
292 | print("Total accumulated errors = %s" % total_errors) | 302 | print("Total accumulated errors = %s" % (total_errors)) |
293 | if error_counts: | 303 | if error_counts: |
294 | print("Detailed error counts:") | 304 | print("Detailed error counts:") |
295 | for check_name in sorted(six.iterkeys(error_counts)): | 305 | for check_name in sorted(six.iterkeys(error_counts)): |