mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-10 14:54:17 +00:00
Support stdin
This commit is contained in:
parent
7dbf2f727b
commit
2249538ba8
2 changed files with 25 additions and 15 deletions
|
|
@ -663,7 +663,7 @@ def checkPath(filename, ignore=[]):
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
def check(codeString, ignore, filename='(code)'):
|
def check(codeString, ignore, filename='stdin'):
|
||||||
"""
|
"""
|
||||||
Check the Python source given by C{codeString} for flakes.
|
Check the Python source given by C{codeString} for flakes.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,9 @@ import os.path
|
||||||
from subprocess import PIPE, Popen
|
from subprocess import PIPE, Popen
|
||||||
import select
|
import select
|
||||||
try:
|
try:
|
||||||
from StringIO import StringIO # NOQA
|
from StringIO import StringIO
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from io import StringIO # NOQA
|
from io import StringIO # NOQA
|
||||||
|
|
||||||
from flake8.util import skip_file
|
from flake8.util import skip_file
|
||||||
from flake8 import pep8
|
from flake8 import pep8
|
||||||
|
|
@ -20,7 +20,7 @@ from flake8 import mccabe
|
||||||
pep8style = None
|
pep8style = None
|
||||||
|
|
||||||
|
|
||||||
def check_file(path, ignore=[], complexity=-1):
|
def check_file(path, ignore=(), complexity=-1):
|
||||||
if pep8style.excluded(path):
|
if pep8style.excluded(path):
|
||||||
return 0
|
return 0
|
||||||
warnings = pyflakes.checkPath(path, ignore)
|
warnings = pyflakes.checkPath(path, ignore)
|
||||||
|
|
@ -30,8 +30,8 @@ def check_file(path, ignore=[], complexity=-1):
|
||||||
return warnings
|
return warnings
|
||||||
|
|
||||||
|
|
||||||
def check_code(code, complexity=-1):
|
def check_code(code, ignore=(), complexity=-1):
|
||||||
warnings = pyflakes.check(code, 'stdin')
|
warnings = pyflakes.check(code, ignore, 'stdin')
|
||||||
warnings += pep8style.input_file(StringIO(code))
|
warnings += pep8style.input_file(StringIO(code))
|
||||||
if complexity > -1:
|
if complexity > -1:
|
||||||
warnings += mccabe.get_code_complexity(code, complexity)
|
warnings += mccabe.get_code_complexity(code, complexity)
|
||||||
|
|
@ -56,6 +56,16 @@ def _get_python_files(paths):
|
||||||
yield path
|
yield path
|
||||||
|
|
||||||
|
|
||||||
|
def read_stdin():
|
||||||
|
# wait for 1 second on the stdin fd
|
||||||
|
reads, __, __ = select.select([sys.stdin], [], [], 1.)
|
||||||
|
if reads == []:
|
||||||
|
print('input not specified')
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
return sys.stdin.read()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
global pep8style
|
global pep8style
|
||||||
pep8style = pep8.StyleGuide(parse_argv=True, config_file=True)
|
pep8style = pep8.StyleGuide(parse_argv=True, config_file=True)
|
||||||
|
|
@ -63,6 +73,7 @@ def main():
|
||||||
complexity = options.max_complexity
|
complexity = options.max_complexity
|
||||||
builtins = set(options.builtins)
|
builtins = set(options.builtins)
|
||||||
warnings = 0
|
warnings = 0
|
||||||
|
stdin = None
|
||||||
|
|
||||||
if builtins:
|
if builtins:
|
||||||
orig_builtins = set(pyflakes._MAGIC_GLOBALS)
|
orig_builtins = set(pyflakes._MAGIC_GLOBALS)
|
||||||
|
|
@ -70,16 +81,15 @@ def main():
|
||||||
|
|
||||||
if pep8style.paths and options.filename is not None:
|
if pep8style.paths and options.filename is not None:
|
||||||
for path in _get_python_files(pep8style.paths):
|
for path in _get_python_files(pep8style.paths):
|
||||||
warnings += check_file(path, options.ignore, complexity)
|
if path == '-':
|
||||||
|
if stdin is None:
|
||||||
|
stdin = read_stdin()
|
||||||
|
warnings += check_code(stdin, options.ignore, complexity)
|
||||||
|
else:
|
||||||
|
warnings += check_file(path, options.ignore, complexity)
|
||||||
else:
|
else:
|
||||||
# wait for 1 second on the stdin fd
|
stdin = read_stdin()
|
||||||
reads, __, __ = select.select([sys.stdin], [], [], 1.)
|
warnings += check_code(stdin, options.ignore, complexity)
|
||||||
if reads == []:
|
|
||||||
print('input not specified')
|
|
||||||
raise SystemExit(1)
|
|
||||||
|
|
||||||
stdin = sys.stdin.read()
|
|
||||||
warnings += check_code(stdin, complexity)
|
|
||||||
|
|
||||||
if options.exit_zero:
|
if options.exit_zero:
|
||||||
raise SystemExit(0)
|
raise SystemExit(0)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue