Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_pyflakes_conformance():
"""Test source code for PyFlakes conformance."""
reporter = Reporter(sys.stdout, sys.stderr)
base_path = os.path.join(os.path.dirname(__file__), '..')
paths = [
os.path.join(base_path, 'ros_buildfarm'),
os.path.join(base_path, 'scripts'),
]
warning_count = checkRecursive(paths, reporter)
assert warning_count == 0, \
'Found %d code style warnings' % warning_count
def _flake_line(line_n):
undefined = 'undefined'
unused = 'unused'
redefinition = 'redefinition'
warnings = StringIO()
errors = StringIO()
rep = reporter.Reporter(warnings, errors)
code = '\n'.join(vim.current.buffer[:])
if api.check(code, 'f', rep):
warnings.seek(0)
errors = {
undefined: [],
unused: []
}
for line, error in [(int(x.split(':')[1]), x) for x in warnings.readlines()]:
if undefined in error and line == line_n:
def pyflakes_report(code):
"""Inspect code using PyFlakes to detect any 'missing name' report.
Args:
code: A multiline string representing Python code
Returns: a list of names that have been reported missing by Flakes
"""
flakes_stdout = StreamList()
flakes_stderr = StreamList()
rep = pyflakes_reporter.Reporter(
flakes_stdout.reset(),
flakes_stderr.reset())
pyflakes_api.check(code, filename="kale", reporter=rep)
# the stderr stream should be used just for compilation error, so if any
# message is found in the stderr stream, raise an exception
if rep._stderr():
raise RuntimeError("Flakes reported the following error:"
"\n{}".format('\t' + '\t'.join(rep._stderr())))
# Match names
p = r"'(.+?)'"
out = rep._stdout()
# Using a `set` to avoid repeating the same var names in case they are
# reported missing multiple times by flakes
def inspect_code(self, code):
code = self._append_globals(code)
rep = reporter.Reporter(self.flakes_stdout.reset(), self.flakes_stderr.reset())
api.check(code, filename="block", reporter=rep)
# Match names
p = r"'(.+?)'"
out = rep._stdout()
# Using set to avoid repeating same entry if same missing name is called multiple times
undef_vars = set()
for l in list(filter(lambda a: a != '\n' and 'undefined name' in a, out)):
var_search = re.search(p, l)
undef_vars.add(var_search.group(1))
return set(undef_vars)
def _makeDefaultReporter():
"""
Make a reporter that can be used when no reporter is specified.
"""
return Reporter(sys.stdout, sys.stderr)
def check(source):
"""Check code."""
compile(source, '', 'exec', dont_inherit=True)
reporter = pyflakes.reporter.Reporter(sys.stderr, sys.stderr)
pyflakes.api.check(source, filename='', reporter=reporter)
def run_linter(self, *files):
"""
for each file, run pyflakes and capture the output
flag if files have too many errors relative to configured
threshold
"""
capture_stdout = StringIO()
reporter.Reporter(capture_stdout, sys.stderr)
for file in files:
LOGGER.info("Pyflakes: {}".format(file))
result = checkPath(file)
if result:
LOGGER.warning("Found {} flakes".format(result))
if result > self.errors_per_file:
self.report_error(file, capture_stdout.getvalue())
def main(files):
"""Call main in all given files."""
t1 = time.time()
for fn in files:
# Report the file name.
assert g.os_path_exists(fn), fn
sfn = g.shortFileName(fn)
s = g.readFileIntoEncodedString(fn)
if s and s.strip():
r = reporter.Reporter(errorStream=sys.stderr, warningStream=sys.stderr)
api.check(s, sfn, r)
t2 = time.time()
n = len(files)
print('%s file%s, time: %5.2f sec.' % (n, g.plural(n), t2 - t1))
#@+node:ekr.20160518000549.14: ** report_version