]>
crepu.dev Git - config.git/blob - djavu-asus/emacs/elpy/rpc-venv/lib/python3.11/site-packages/pyflakes/api.py
2 API for the command-line I{pyflakes} tool.
10 from pyflakes
import checker
, __version__
11 from pyflakes
import reporter
as modReporter
13 __all__
= ['check', 'checkPath', 'checkRecursive', 'iterSourceCode', 'main']
15 PYTHON_SHEBANG_REGEX
= re
.compile(br
'^#!.*\bpython(3(\.\d+)?|w)?[dmu]?\s')
18 def check(codeString
, filename
, reporter
=None):
20 Check the Python source given by C{codeString} for flakes.
22 @param codeString: The Python source to check.
23 @type codeString: C{str}
25 @param filename: The name of the file the source came from, used to report
27 @type filename: C{str}
29 @param reporter: A L{Reporter} instance, where errors and warnings will be
32 @return: The number of warnings emitted.
36 reporter
= modReporter
._makeDefaultReporter
()
37 # First, compile into an AST and handle syntax errors.
39 tree
= ast
.parse(codeString
, filename
=filename
)
40 except SyntaxError as e
:
41 reporter
.syntaxError(filename
, e
.args
[0], e
.lineno
, e
.offset
, e
.text
)
44 reporter
.unexpectedError(filename
, 'problem decoding source')
46 # Okay, it's syntactically valid. Now check it.
47 w
= checker
.Checker(tree
, filename
=filename
)
48 w
.messages
.sort(key
=lambda m
: m
.lineno
)
49 for warning
in w
.messages
:
50 reporter
.flake(warning
)
51 return len(w
.messages
)
54 def checkPath(filename
, reporter
=None):
56 Check the given path, printing out any warnings detected.
58 @param reporter: A L{Reporter} instance, where errors and warnings will be
61 @return: the number of warnings printed
64 reporter
= modReporter
._makeDefaultReporter
()
66 with
open(filename
, 'rb') as f
:
69 reporter
.unexpectedError(filename
, e
.args
[1])
71 return check(codestr
, filename
, reporter
)
74 def isPythonFile(filename
):
75 """Return True if filename points to a Python file."""
76 if filename
.endswith('.py'):
79 # Avoid obvious Emacs backup files
80 if filename
.endswith("~"):
86 with
open(filename
, 'rb') as f
:
87 text
= f
.read(max_bytes
)
93 return PYTHON_SHEBANG_REGEX
.match(text
)
96 def iterSourceCode(paths
):
98 Iterate over all Python source files in C{paths}.
100 @param paths: A list of paths. Directories will be recursed into and
101 any .py files found will be yielded. Any non-directories will be
105 if os
.path
.isdir(path
):
106 for dirpath
, dirnames
, filenames
in os
.walk(path
):
107 for filename
in filenames
:
108 full_path
= os
.path
.join(dirpath
, filename
)
109 if isPythonFile(full_path
):
115 def checkRecursive(paths
, reporter
):
117 Recursively check all source files in C{paths}.
119 @param paths: A list of paths to Python source files and directories
120 containing Python source files.
121 @param reporter: A L{Reporter} where all of the warnings and errors
123 @return: The number of warnings found.
126 for sourcePath
in iterSourceCode(paths
):
127 warnings
+= checkPath(sourcePath
, reporter
)
131 def _exitOnSignal(sigName
, message
):
132 """Handles a signal with sys.exit.
134 Some of these signals (SIGPIPE, for example) don't exist or are invalid on
135 Windows. So, ignore errors that might arise.
140 sigNumber
= getattr(signal
, sigName
)
141 except AttributeError:
142 # the signal constants defined in the signal module are defined by
143 # whether the C library supports them or not. So, SIGPIPE might not
151 signal
.signal(sigNumber
, handler
)
153 # It's also possible the signal is defined, but then it's invalid. In
154 # this case, signal.signal raises ValueError.
160 Retrieve and format package version along with python version & OS used
162 return ('%s Python %s on %s' %
163 (__version__
, platform
.python_version(), platform
.system()))
166 def main(prog
=None, args
=None):
167 """Entry point for the script "pyflakes"."""
170 # Handle "Keyboard Interrupt" and "Broken pipe" gracefully
171 _exitOnSignal('SIGINT', '... stopped')
172 _exitOnSignal('SIGPIPE', 1)
174 parser
= argparse
.ArgumentParser(prog
=prog
,
175 description
='Check Python source files for errors')
176 parser
.add_argument('-V', '--version', action
='version', version
=_get_version())
177 parser
.add_argument('path', nargs
='*',
178 help='Path(s) of Python file(s) to check. STDIN if not given.')
179 args
= parser
.parse_args(args
=args
).path
180 reporter
= modReporter
._makeDefaultReporter
()
182 warnings
= checkRecursive(args
, reporter
)
184 warnings
= check(sys
.stdin
.read(), '<stdin>', reporter
)
185 raise SystemExit(warnings
> 0)