def check_all(self, log_flag, pyflakes_errors_only, roots): """Run pyflakes on all files in paths.""" try: from pyflakes import api, reporter except Exception: # ModuleNotFoundError return True # Pretend all is fine. total_errors = 0 for i, root in enumerate(roots): fn = self.finalize(root) sfn = g.shortFileName(fn) # #1306: nopyflakes if any([ z.strip().startswith('@nopyflakes') for z in g.splitLines(root.b) ]): continue # Report the file name. s = g.readFileIntoEncodedString(fn) if s and s.strip(): if not pyflakes_errors_only: g.es(f"Pyflakes: {sfn}") # Send all output to the log pane. r = reporter.Reporter( errorStream=self.LogStream(i, roots), warningStream=self.LogStream(i, roots), ) errors = api.check(s, sfn, r) total_errors += errors return total_errors
def runPyflakes(script_code, script_path): from pyflakes.api import check from pyflakes import reporter from StringIO import StringIO stream = StringIO() check(script_code, script_path, reporter.Reporter(stream, stream)) return stream.getvalue()
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) # 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 undef_vars = set() # iterate over all the flakes report output, keeping only lines # with 'undefined name' reports for line in filter(lambda a: a != '\n' and 'undefined name' in a, out): var_search = re.search(p, line) undef_vars.add(var_search.group(1)) return undef_vars
def check_all(self, log_flag, paths): '''Run pyflakes on all files in paths.''' from pyflakes import api, reporter total_errors = 0 for fn in sorted(paths): # Report the file name. sfn = g.shortFileName(fn) s = g.readFileIntoEncodedString(fn, silent=False) if s.strip(): g.es('Pyflakes: %s' % sfn) # Send all output to the log pane. class LogStream: def write(self, s): if s.strip(): g.es_print(s) # It *is* useful to send pyflakes errors to the console. r = reporter.Reporter( errorStream=LogStream(), warningStream=LogStream(), ) errors = api.check(s, sfn, r) total_errors += errors return total_errors
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 undef_vars = set() # iterate over all the flakes report output, keeping only lines # with 'undefined name' reports for line in filter(lambda a: a != '\n' and 'undefined name' in a, out): var_search = re.search(p, line) undef_vars.add(var_search.group(1)) return undef_vars
def check_script(self, p, script): from pyflakes import api, reporter r = reporter.Reporter( errorStream=self.LogStream(), warningStream=self.LogStream(), ) errors = api.check(script, '', r) return errors == 0
def run_pyflakes(): """ check if the code wer'e about to import is fine""" returnSTRM = StringIO() reporter = modReporter.Reporter(returnSTRM, returnSTRM) with open("setup.py", "r") as file: pyflask_api.check(file.read(), 'line', reporter) output = returnSTRM.getvalue() returnSTRM.close() return output
def check_script(self, p, script): try: from pyflakes import api, reporter except Exception: # ModuleNotFoundError return True # Pretend all is fine. r = reporter.Reporter( errorStream=self.LogStream(), warningStream=self.LogStream(), ) errors = api.check(script, '', r) return errors == 0
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(f"{n} file{g.plural(n)}, time: {t2 - t1:5.2f} sec.")
def revisar_sintaxis_codigo(codigo: str) -> (int, str, str): # pip install --upgrade pyflakes import io import pyflakes.api as api from pyflakes import reporter as modReporter warning_stream = io.StringIO() error_stream = io.StringIO() reporter = modReporter.Reporter(warning_stream, error_stream) cantidad = api.check(codigo, 'Envio usuario, línea', reporter) warnings = warning_stream.getvalue() errores = error_stream.getvalue() return (cantidad, warnings, errores)
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 run on all tables in tables_table.''' 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))
def check_script(self, p, script): """Call pyflakes to check the given script.""" try: from pyflakes import api, reporter except Exception: # ModuleNotFoundError return True # Pretend all is fine. # #1306: nopyflakes lines = g.splitLines(p.b) for line in lines: if line.strip().startswith('@nopyflakes'): return True r = reporter.Reporter( errorStream=self.LogStream(), warningStream=self.LogStream(), ) errors = api.check(script, '', r) return errors == 0
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 check_all(self, log_flag, paths, pyflakes_errors_only, roots=None): '''Run pyflakes on all files in paths.''' from pyflakes import api, reporter total_errors = 0 # pylint: disable=cell-var-from-loop for fn_n, fn in enumerate(sorted(paths)): # Report the file name. sfn = g.shortFileName(fn) s = g.readFileIntoEncodedString(fn) if s.strip(): if not pyflakes_errors_only: g.es('Pyflakes: %s' % sfn) # Send all output to the log pane. r = reporter.Reporter( errorStream=self.LogStream(fn_n, roots), warningStream=self.LogStream(fn_n, roots), ) errors = api.check(s, sfn, r) total_errors += errors return total_errors
def check_all(self, roots): """Run pyflakes on all files in paths.""" total_errors = 0 for i, root in enumerate(roots): fn = self.finalize(root) sfn = g.shortFileName(fn) # #1306: nopyflakes if any(z.strip().startswith('@nopyflakes') for z in g.splitLines(root.b)): continue # Report the file name. s = g.readFileIntoEncodedString(fn) if s and s.strip(): # Send all output to the log pane. r = reporter.Reporter( errorStream=self.LogStream(i, roots), warningStream=self.LogStream(i, roots), ) errors = api.check(s, sfn, r) total_errors += errors return total_errors
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: module = error.split()[-1].strip("\n|'") errors[undefined].append(module) elif unused in error and redefinition not in error: module = error.split()[1].strip(" |'") errors[unused].append(module) return errors