Browse Source

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
Joshua Harlow 4 years ago
parent
commit
355029419e
1 changed files with 81 additions and 71 deletions
  1. 81
    71
      doc8/main.py

+ 81
- 71
doc8/main.py View File

@@ -148,86 +148,34 @@ def setup_logging(verbose):
148 148
                         format='%(levelname)s: %(message)s', stream=sys.stdout)
149 149
 
150 150
 
151
-def main():
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
-
151
+def scan(cfg):
206 152
     print("Scanning...")
207 153
     files = collections.deque()
208
-    ignored_paths = args.pop('ignore_path')
154
+    ignored_paths = cfg.pop('ignore_path')
209 155
     files_ignored = 0
210
-    files_selected = 0
211
-    file_iter = utils.find_files(args.pop('paths', []),
212
-                                 args.pop('extension', []), ignored_paths)
156
+    file_iter = utils.find_files(cfg.pop('paths', []),
157
+                                 cfg.pop('extension', []), ignored_paths)
213 158
     for filename, ignoreable in file_iter:
214 159
         if ignoreable:
215 160
             files_ignored += 1
216
-            if args.get('verbose'):
161
+            if cfg.get('verbose'):
217 162
                 print("  Ignoring '%s'" % (filename))
218 163
         else:
219
-            files_selected += 1
220 164
             files.append(file_parser.parse(filename))
221
-            if args.get('verbose'):
165
+            if cfg.get('verbose'):
222 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 172
     error_counts = {}
173
+    ignoreables = frozenset(cfg.pop('ignore', []))
226 174
     while files:
227 175
         f = files.popleft()
228
-        if args.get('verbose'):
176
+        if cfg.get('verbose'):
229 177
             print("Validating %s" % f)
230
-        for c in fetch_checks(args):
178
+        for c in fetch_checks(cfg):
231 179
             try:
232 180
                 # http://legacy.python.org/dev/peps/pep-3155/
233 181
                 check_name = c.__class__.__qualname__
@@ -241,7 +189,7 @@ def main():
241 189
                 pass
242 190
             else:
243 191
                 if not extension_matcher.match(f.extension):
244
-                    if args.get('verbose'):
192
+                    if cfg.get('verbose'):
245 193
                         print("  Skipping check '%s' since it does not"
246 194
                               " understand parsing a file with extension '%s'"
247 195
                               % (check_name, f.extension))
@@ -253,17 +201,17 @@ def main():
253 201
             else:
254 202
                 reports = reports - ignoreables
255 203
                 if not reports:
256
-                    if args.get('verbose'):
204
+                    if cfg.get('verbose'):
257 205
                         print("  Skipping check '%s', determined to only"
258 206
                               " check ignoreable codes" % check_name)
259 207
                     continue
260
-            if args.get('verbose'):
208
+            if cfg.get('verbose'):
261 209
                 print("  Running check '%s'" % check_name)
262 210
             if isinstance(c, checks.ContentCheck):
263 211
                 for line_num, code, message in c.report_iter(f):
264 212
                     if code in ignoreables:
265 213
                         continue
266
-                    if args.get('verbose'):
214
+                    if cfg.get('verbose'):
267 215
                         print('    - %s:%s: %s %s'
268 216
                               % (f.filename, line_num, code, message))
269 217
                     else:
@@ -275,7 +223,7 @@ def main():
275 223
                     for code, message in c.report_iter(line):
276 224
                         if code in ignoreables:
277 225
                             continue
278
-                        if args.get('verbose'):
226
+                        if cfg.get('verbose'):
279 227
                             print('    - %s:%s: %s %s'
280 228
                                   % (f.filename, line_num, code, message))
281 229
                         else:
@@ -285,11 +233,73 @@ def main():
285 233
             else:
286 234
                 raise TypeError("Unknown check type: %s, %s"
287 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 297
     total_errors = sum(six.itervalues(error_counts))
298
+
289 299
     print("=" * 8)
290 300
     print("Total files scanned = %s" % (files_selected))
291 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 303
     if error_counts:
294 304
         print("Detailed error counts:")
295 305
         for check_name in sorted(six.iterkeys(error_counts)):

Loading…
Cancel
Save