Merge lp:~qcxhome/pyflakes/pyflakes-options--exclude into lp:pyflakes

Proposed by Chenxiong Qi on 2013-08-16
Status: Needs review
Proposed branch: lp:~qcxhome/pyflakes/pyflakes-options--exclude
Merge into: lp:pyflakes
Diff against target: 72 lines (+40/-5)
1 file modified
pyflakes/api.py (+40/-5)
To merge this branch: bzr merge lp:~qcxhome/pyflakes/pyflakes-options--exclude
Reviewer Review Type Date Requested Status
Pyflakes Dev 2013-08-16 Pending
Review via email: mp+180500@code.launchpad.net

Description of the change

Hi Team,

To exclude some files or directories is more useful. In some cases, a django-based project might have directory named migrations to track models' changes, and test code within tests/ or tests.py. All these files and directories are not necessary to be checked with pyflakes. Without option --exclude, user has to specify all other files and directories that must be checked. It's not very convenient. Hope this commit be useful to pyflakes' users.

Regards,
Chenxiong Qi

To post a comment you must log in.
109. By Chenxiong Qi on 2013-08-17

change to help text of exclude option

Unmerged revisions

109. By Chenxiong Qi on 2013-08-17

change to help text of exclude option

108. By Chenxiong Qi on 2013-08-16

add --exclude option

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'pyflakes/api.py'
2--- pyflakes/api.py 2013-04-21 09:32:10 +0000
3+++ pyflakes/api.py 2013-08-17 03:26:51 +0000
4@@ -104,7 +104,14 @@
5 yield path
6
7
8-def checkRecursive(paths, reporter):
9+def is_path_excluded(path, exclude_patterns):
10+ if exclude_patterns is None:
11+ return False
12+ result = set(exclude_patterns) & set(path.split(os.sep))
13+ return len(result) > 0
14+
15+
16+def checkRecursive(paths, reporter, options):
17 """
18 Recursively check all source files in C{paths}.
19
20@@ -112,20 +119,48 @@
21 containing Python source files.
22 @param reporter: A L{Reporter} where all of the warnings and errors
23 will be reported to.
24+ @param options: options passed in the command line.
25 @return: The number of warnings found.
26 """
27 warnings = 0
28+ patterns = options.exclude_patterns
29 for sourcePath in iterSourceCode(paths):
30- warnings += checkPath(sourcePath, reporter)
31+ if not is_path_excluded(sourcePath, patterns):
32+ warnings += checkPath(sourcePath, reporter)
33 return warnings
34
35
36+def build_options_parser(prog, version):
37+ parser = OptionParser(prog=prog, version=version)
38+ parser.add_option('--exclude', action='store',
39+ metavar='patterns', dest='exclude_patterns',
40+ help='exclude files or directories which match patterns '
41+ 'separated by comma. Specify the basename instead '
42+ 'of relative or absolute path. (default: '
43+ '.svn,CVS,.bzr,.hg,.git)')
44+ return parser
45+
46+
47+def clean_options(options):
48+ patterns = options.exclude_patterns
49+ default_patterns = ['.svn', 'CVS', '.bzr', '.hg', '.git']
50+ if patterns is not None:
51+ options.exclude_patterns = default_patterns + patterns.split(',')
52+
53+
54+def parse_args(prog, version):
55+ parser = build_options_parser(prog=prog, version=version)
56+ opts, args = parser.parse_args()
57+ clean_options(opts)
58+ return opts, args
59+
60+
61 def main(prog=None):
62- parser = OptionParser(prog=prog, version=__version__)
63- __, args = parser.parse_args()
64+ opts, args = parse_args(prog=prog, version=__version__)
65+
66 reporter = modReporter._makeDefaultReporter()
67 if args:
68- warnings = checkRecursive(args, reporter)
69+ warnings = checkRecursive(args, reporter, opts)
70 else:
71 warnings = check(sys.stdin.read(), '<stdin>', reporter)
72 raise SystemExit(warnings > 0)

Subscribers

People subscribed via source and target branches

to all changes: