|
6 | 6 | pass |
7 | 7 | else: |
8 | 8 | demandimport.disable() |
| 9 | +import os |
| 10 | + |
| 11 | +import pep8 |
9 | 12 | import pyflakes |
10 | 13 | import pyflakes.checker |
11 | 14 |
|
@@ -39,23 +42,78 @@ class FlakesChecker(pyflakes.checker.Checker): |
39 | 42 | version = pyflakes.__version__ |
40 | 43 |
|
41 | 44 | def __init__(self, tree, filename): |
| 45 | + filename = pep8.normalize_paths(filename)[0] |
| 46 | + withDoctest = self.withDoctest |
| 47 | + included_by = [include for include in self.include_in_doctest |
| 48 | + if include != '' and filename.startswith(include)] |
| 49 | + if included_by: |
| 50 | + withDoctest = True |
| 51 | + |
| 52 | + for exclude in self.exclude_from_doctest: |
| 53 | + if exclude != '' and filename.startswith(exclude): |
| 54 | + withDoctest = False |
| 55 | + overlaped_by = [include for include in included_by |
| 56 | + if include.startswith(exclude)] |
| 57 | + |
| 58 | + if overlaped_by: |
| 59 | + withDoctest = True |
| 60 | + |
42 | 61 | super(FlakesChecker, self).__init__(tree, filename, |
43 | | - withDoctest=self.withDoctest) |
| 62 | + withDoctest=withDoctest) |
44 | 63 |
|
45 | 64 | @classmethod |
46 | 65 | def add_options(cls, parser): |
47 | 66 | parser.add_option('--builtins', |
48 | 67 | help="define more built-ins, comma separated") |
49 | 68 | parser.add_option('--doctests', default=False, action='store_true', |
50 | 69 | help="check syntax of the doctests") |
51 | | - parser.config_options.extend(['builtins', 'doctests']) |
| 70 | + parser.add_option('--include-in-doctest', default='', |
| 71 | + dest='include_in_doctest', |
| 72 | + help='Run doctests only on these files', |
| 73 | + type='string') |
| 74 | + parser.add_option('--exclude-from-doctest', default='', |
| 75 | + dest='exclude_from_doctest', |
| 76 | + help='Skip these files when running doctests', |
| 77 | + type='string') |
| 78 | + parser.config_options.extend(['builtins', 'doctests', |
| 79 | + 'include-in-doctest', |
| 80 | + 'exclude-from-doctest']) |
52 | 81 |
|
53 | 82 | @classmethod |
54 | 83 | def parse_options(cls, options): |
55 | 84 | if options.builtins: |
56 | 85 | cls.builtIns = cls.builtIns.union(options.builtins.split(',')) |
57 | 86 | cls.withDoctest = options.doctests |
58 | 87 |
|
| 88 | + included_files = [] |
| 89 | + for included_file in options.include_in_doctest.split(','): |
| 90 | + if included_file == '': |
| 91 | + continue |
| 92 | + if not included_file.startswith((os.sep, './', '~/')): |
| 93 | + included_files.append('./' + included_file) |
| 94 | + else: |
| 95 | + included_files.append(included_file) |
| 96 | + cls.include_in_doctest = pep8.normalize_paths(','.join(included_files)) |
| 97 | + |
| 98 | + excluded_files = [] |
| 99 | + for excluded_file in options.exclude_from_doctest.split(','): |
| 100 | + if excluded_file == '': |
| 101 | + continue |
| 102 | + if not excluded_file.startswith((os.sep, './', '~/')): |
| 103 | + excluded_files.append('./' + excluded_file) |
| 104 | + else: |
| 105 | + excluded_files.append(excluded_file) |
| 106 | + cls.exclude_from_doctest = pep8.normalize_paths( |
| 107 | + ','.join(excluded_files)) |
| 108 | + |
| 109 | + inc_exc = set(cls.include_in_doctest).intersection( |
| 110 | + set(cls.exclude_from_doctest)) |
| 111 | + if inc_exc: |
| 112 | + raise ValueError('"%s" was specified in both the ' |
| 113 | + 'include-in-doctest and exclude-from-doctest ' |
| 114 | + 'options. You are not allowed to specify it in ' |
| 115 | + 'both for doctesting.' % inc_exc) |
| 116 | + |
59 | 117 | def run(self): |
60 | 118 | for m in self.messages: |
61 | 119 | col = getattr(m, 'col', 0) |
|
0 commit comments