2
API for the command-line I{pyflakes} tool.
9
from pyflakes import checker
10
from pyflakes import reporter as modReporter
12
__all__ = ['check', 'checkPath', 'checkRecursive', 'iterSourceCode', 'main']
15
def check(codeString, filename, reporter=None):
17
Check the Python source given by C{codeString} for flakes.
19
@param codeString: The Python source to check.
20
@type codeString: C{str}
22
@param filename: The name of the file the source came from, used to report
24
@type filename: C{str}
26
@param reporter: A L{Reporter} instance, where errors and warnings will be
29
@return: The number of warnings emitted.
33
reporter = modReporter._makeDefaultReporter()
34
# First, compile into an AST and handle syntax errors.
36
tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
38
value = sys.exc_info()[1]
41
(lineno, offset, text) = value.lineno, value.offset, value.text
43
# If there's an encoding problem with the file, the text is None.
45
# Avoid using msg, since for the only known case, it contains a
46
# bogus message that claims the encoding the file declared was
48
reporter.unexpectedError(filename, 'problem decoding source')
50
reporter.syntaxError(filename, msg, lineno, offset, text)
53
reporter.unexpectedError(filename, 'problem decoding source')
56
# Okay, it's syntactically valid. Now check it.
57
w = checker.Checker(tree, filename)
58
w.messages.sort(key=lambda m: m.lineno)
59
for warning in w.messages:
60
reporter.flake(warning)
61
return len(w.messages)
64
def checkPath(filename, reporter=None):
66
Check the given path, printing out any warnings detected.
68
@param reporter: A L{Reporter} instance, where errors and warnings will be
71
@return: the number of warnings printed
74
reporter = modReporter._makeDefaultReporter()
76
f = open(filename, 'U')
78
return check(f.read() + '\n', filename, reporter)
82
reporter.unexpectedError(filename, 'problem decoding source')
84
msg = sys.exc_info()[1]
85
reporter.unexpectedError(filename, msg.args[1])
89
def iterSourceCode(paths):
91
Iterate over all Python source files in C{paths}.
93
@param paths: A list of paths. Directories will be recursed into and
94
any .py files found will be yielded. Any non-directories will be
98
if os.path.isdir(path):
99
for dirpath, dirnames, filenames in os.walk(path):
100
for filename in filenames:
101
if filename.endswith('.py'):
102
yield os.path.join(dirpath, filename)
107
def checkRecursive(paths, reporter):
109
Recursively check all source files in C{paths}.
111
@param paths: A list of paths to Python source files and directories
112
containing Python source files.
113
@param reporter: A L{Reporter} where all of the warnings and errors
115
@return: The number of warnings found.
118
for sourcePath in iterSourceCode(paths):
119
warnings += checkPath(sourcePath, reporter)
124
reporter = modReporter._makeDefaultReporter()
126
warnings = checkRecursive(args, reporter)
128
warnings = check(sys.stdin.read(), '<stdin>', reporter)
129
raise SystemExit(warnings > 0)