class CrunchyChecker: """Class to configure and start a pychecker analysis """ def __init__(self): self._report = None self._code = None self._output_buffer = None def run(self, code): """Make the analysis""" self._code = code # Save the code in a temporary file temp = tempfile.NamedTemporaryFile(suffix = '.py') temp.write(self._code) temp.flush() fname = os.path.basename(temp.name) self._output_buffer = StringIO() checker._printWarnings = self._printWarnings checker.main(['dummy_arg', '--only', temp.name]) # remove all traces of the Temporary file name self._report = self._output_buffer.getvalue().replace(temp.name, 'line') self._report = self._report.replace(fname[:-3] + ":", "") self._output_buffer.close() temp.close() def get_report(self): """Return the full report""" return "Report from pychecker:\n" + self._report def get_global_score(self): """Return the global score or None if not available. This score can be formatted with "%.2f/10" % score It is not computed by pychecker, but here, by the formule: score = 10 - ((number_of_errors / number_of_lines) * 10) """ if not "UNABLE TO IMPORT" in self.get_report(): # Just count non-empty and non-comment lines code_lines = [line for line in self._code.split('\n') \ if line.strip() and not line.strip().startswith('#')] report_lines = [line for line in self.get_report().split('\n') \ if line] if len(report_lines) == 1: return 10 number_of_errors = float(len(report_lines)-1) number_of_lines = float(len(code_lines)) return 10 - ((number_of_errors / number_of_lines) * 10) else: return 0 def _printWarnings(self, warnings, stream=None): """This function call the original checker._printWarnings, but set the stream to self._output_buffer """ original_printWarnings(warnings, self._output_buffer)
class CrunchyChecker: """Class to configure and start a pychecker analysis """ def __init__(self): self._report = None self._code = None self._output_buffer = None def run(self, code): """Make the analysis""" self._code = code # Save the code in a temporary file temp = tempfile.NamedTemporaryFile(suffix='.py') temp.write(self._code) temp.flush() fname = os.path.basename(temp.name) self._output_buffer = StringIO() checker._printWarnings = self._printWarnings checker.main(['dummy_arg', '--only', temp.name]) # remove all traces of the Temporary file name self._report = self._output_buffer.getvalue().replace( temp.name, 'line') self._report = self._report.replace(fname[:-3] + ":", "") self._output_buffer.close() temp.close() def get_report(self): """Return the full report""" return "Report from pychecker:\n" + self._report def get_global_score(self): """Return the global score or None if not available. This score can be formatted with "%.2f/10" % score It is not computed by pychecker, but here, by the formule: score = 10 - ((number_of_errors / number_of_lines) * 10) """ if not "UNABLE TO IMPORT" in self.get_report(): # Just count non-empty and non-comment lines code_lines = [line for line in self._code.split('\n') \ if line.strip() and not line.strip().startswith('#')] report_lines = [line for line in self.get_report().split('\n') \ if line] if len(report_lines) == 1: return 10 number_of_errors = float(len(report_lines) - 1) number_of_lines = float(len(code_lines)) return 10 - ((number_of_errors / number_of_lines) * 10) else: return 0 def _printWarnings(self, warnings, stream=None): """This function call the original checker._printWarnings, but set the stream to self._output_buffer """ original_printWarnings(warnings, self._output_buffer)
def run(self, code): """Make the analysis""" temp = tempfile.NamedTemporaryFile(suffix = '.py') temp.write(code) temp.flush() output_buffer = StringIO() self.linter.reporter.set_output(output_buffer) self.linter.check(temp.name) self._report = output_buffer.getvalue().replace(temp.name, 'line ') output_buffer.close() temp.close()
def run(self, code): """Make the analysis""" temp = tempfile.NamedTemporaryFile(suffix='.py') temp.write(code) temp.flush() output_buffer = StringIO() self.linter.reporter.set_output(output_buffer) self.linter.check(temp.name) self._report = output_buffer.getvalue().replace(temp.name, 'line ') output_buffer.close() temp.close()
def run(self, code): """Make the analysis This function is inspired from the check function of the pyflakes start script. """ print("run called in pychecker") self._code = code # Open a buffer for the output output = StringIO() # Start the check try: tree = compiler.parse(self._code) except (SyntaxError, IndentationError): value = sys.exc_info()[1] try: (lineno, offset, line) = value[1][1:] except IndexError: print >> output, _('Could not compile the code.') else : if line.endswith("\n"): line = line[:-1] print >> output, _('line %d: could not compile') % lineno print >> output, line print >> output, " " * (offset-2), "^" self._nb_errors = None else: w = checker.Checker(tree, 'line') w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno)) for warning in w.messages: print >> output, warning self._nb_errors = len(w.messages) # Get the output and remove the irrelevant file path self._report = output.getvalue() # Close the buffer output.close()
def run(self, code): """Make the analysis This function is inspired from the check function of the pyflakes start script. """ print("run called in pychecker") self._code = code # Open a buffer for the output output = StringIO() # Start the check try: tree = compiler.parse(self._code) except (SyntaxError, IndentationError): value = sys.exc_info()[1] try: (lineno, offset, line) = value[1][1:] except IndexError: print >> output, _('Could not compile the code.') else: if line.endswith("\n"): line = line[:-1] print >> output, _('line %d: could not compile') % lineno print >> output, line print >> output, " " * (offset - 2), "^" self._nb_errors = None else: w = checker.Checker(tree, 'line') w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno)) for warning in w.messages: print >> output, warning self._nb_errors = len(w.messages) # Get the output and remove the irrelevant file path self._report = output.getvalue() # Close the buffer output.close()