Commits

David Cramer  committed f3804c5

Support stdin

  • Participants
  • Parent commits 106b15a

Comments (0)

Files changed (2)

File flake8/pyflakes.py

         return 1
 
 
-def check(codeString, ignore, filename='(code)'):
+def check(codeString, ignore, filename='stdin'):
     """
     Check the Python source given by C{codeString} for flakes.
 

File flake8/run.py

 from subprocess import PIPE, Popen
 import select
 try:
-    from StringIO import StringIO   # NOQA
+    from StringIO import StringIO
 except ImportError:
-    from io import StringIO         # NOQA
+    from io import StringIO  # NOQA
 
 from flake8.util import skip_file
 from flake8 import pep8
 pep8style = None
 
 
-def check_file(path, ignore=[], complexity=-1):
+def check_file(path, ignore=(), complexity=-1):
     if pep8style.excluded(path):
         return 0
     warnings = pyflakes.checkPath(path, ignore)
     return warnings
 
 
-def check_code(code, complexity=-1):
-    warnings = pyflakes.check(code, 'stdin')
+def check_code(code, ignore=(), complexity=-1):
+    warnings = pyflakes.check(code, ignore, 'stdin')
     warnings += pep8style.input_file(StringIO(code))
     if complexity > -1:
         warnings += mccabe.get_code_complexity(code, complexity)
                 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():
     global pep8style
     pep8style = pep8.StyleGuide(parse_argv=True, config_file=True)
     complexity = options.max_complexity
     builtins = set(options.builtins)
     warnings = 0
+    stdin = None
 
     if builtins:
         orig_builtins = set(pyflakes._MAGIC_GLOBALS)
 
     if pep8style.paths and options.filename is not None:
         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:
-        # wait for 1 second on the stdin fd
-        reads, __, __ = select.select([sys.stdin], [], [], 1.)
-        if reads == []:
-            print('input not specified')
-            raise SystemExit(1)
-
-        stdin = sys.stdin.read()
-        warnings += check_code(stdin, complexity)
+        stdin = read_stdin()
+        warnings += check_code(stdin, options.ignore, complexity)
 
     if options.exit_zero:
         raise SystemExit(0)