def test_pep8(self): """ Verify that source code complies with PEP 8 formatting. """ import pep8 ignored_codes = [ "E221", "E241", ] # Setup pep8 processing options. pep8_args = [ "--show-source", "--repeat", "--ignore=" + ",".join(ignored_codes), "ignored", # Ignored argument, but needed # by pep8.process_options() ] pep8.process_options(pep8_args) # Call pep8 to process local files. directory = os.path.split(os.path.abspath(__file__))[0] directory = os.path.split(directory)[0] pep8.input_dir(directory) statistics = pep8.get_statistics() filtered_statistics = [] for line in statistics: code = line[8:12] if code not in ignored_codes: filtered_statistics.append(line) self.assertFalse(filtered_statistics)
def pep8_error_count(path): # process_options initializes some data structures and MUST be called before each Checker().check_all() pep8.process_options( ["pep8", "--ignore=E202,E221,E222,E241,E301,E302,E401,E501,E701,W391,W601,W602", "--show-source", "dummy_path"] ) error_count = pep8.Checker(path).check_all() return error_count
def run_pep8_for_package(package_name): """ If pep8 is installed, run it across the given package name returning any warnings or errors found. """ import pep8 package_path = path_for_import(package_name) pep8.process_options(["-r", package_path]) class Checker(pep8.Checker): """ Subclass pep8's Checker to hook into error reporting. """ def report_error(self, line_number, offset, text, check): """ Store pairs of line numbers and errors. """ self.errors.append((line_number, text.split(" ", 1)[1])) def check_all(self, *args, **kwargs): """ Assign the errors attribute and return it after running. """ self.errors = [] super(Checker, self).check_all(*args, **kwargs) return self.errors def pep8_checker(path): for line_number, text in Checker(path).check_all(): yield "%s:%s: %s" % (path, line_number, text) return _run_checker_for_package(pep8_checker, package_name)
def _execute_pep8(pep8_options, source): """Execute pep8 via python method calls.""" pep8.process_options(['pep8'] + pep8_options) class QuietChecker(pep8.Checker): """Version of checker that does not print.""" def __init__(self, filename, lines): pep8.Checker.__init__(self, filename, lines=lines) self.__results = None def report_error(self, line_number, offset, text, check): """Collect errors.""" code = text[:4] if not pep8.ignore_code(code): self.__results.append( dict(id=text.split()[0], line=line_number, column=offset + 1, info=text)) def check_all(self, expected=None, line_offset=0): """Check code and return results.""" self.__results = [] pep8.Checker.check_all(self, expected, line_offset) return self.__results checker = QuietChecker('', lines=source) return checker.check_all()
def test_pep8(self): pep8.process_options() pep8.options.repeat = True pep8_errors = [] pep8_warnings = [] for fname, text in get_source_file_contents(): def report_error(line_number, offset, text, check): code = text[:4] if code in self.pep8_ignore: code = 'W' + code[1:] text = code + text[4:] print "%s:%s: %s" % (fname, line_number, text) summary = (fname, line_number, offset, text, check) if code[0] == 'W': pep8_warnings.append(summary) else: pep8_errors.append(summary) lines = text.splitlines(True) checker = pep8.Checker(fname, lines) checker.report_error = report_error checker.check_all() if len(pep8_errors) > 0: d = {} for (fname, line_no, offset, text, check) in pep8_errors: d.setdefault(fname, []).append(line_no - 1) self.fail(self._format_message(d, 'There were %d PEP8 errors:' % len(pep8_errors)))
def test_pep8(self): pep8.process_options() pep8.options.repeat = True pep8_errors = [] pep8_warnings = [] for fname, text in get_source_file_contents(): def report_error(line_number, offset, text, check): code = text[:4] if code in self.pep8_ignore: code = 'W' + code[1:] text = code + text[4:] print "%s:%s: %s" % (fname, line_number, text) summary = (fname, line_number, offset, text, check) if code[0] == 'W': pep8_warnings.append(summary) else: pep8_errors.append(summary) lines = text.splitlines(True) checker = pep8.Checker(fname, lines) checker.report_error = report_error checker.check_all() if len(pep8_errors) > 0: d = {} for (fname, line_no, offset, text, check) in pep8_errors: d.setdefault(fname, []).append(line_no - 1) self.fail( self._format_message( d, 'There were %d PEP8 errors:' % len(pep8_errors)))
def test_pep8(self): filepath = settings.INSTALL_DIR arglist = ['--exclude=', filepath] pep8.process_options(arglist) pep8.input_dir(filepath) output = pep8.get_statistics() self.assertEqual(len(output), 0)
def handle(self, *test_labels, **opts): reportMessages = {} appMessages = [] def writer(name): reportMessages[name] = [] rex = re.compile(r"^/(?P<filename>.*):(?P<row>\d{1,}):(?P<col>\d{1,}): (?P<code>[A-Z]\d\d\d) (?P<msg>.*)$") def storeMessage( msg ): appMessages.append(msg) m = rex.match( msg ) if m: reportMessages[name].append( m.groups() ) return storeMessage pep8_options = ['','--filename','*.py'] #[ pep8_options.extend(['--ignore', x]) for x in opts.get('ignore','').split(',') ] if opts.get('ignore', False): pep8_options.extend(['--ignore', opts['ignore']]) pep8_options.extend( [ k for k in ['--show-source', '--show-pep8','--verbose' ] if k in sys.argv[1:] ]) #pep8_options.extend( [ k for k in ['--show-source', '--show-pep8','--verbose' ] if k in opts ]) pep8.process_options(pep8_options) for name, dir, app in self.get_apps( *test_labels ): if 'django' == name: continue pep8.message = writer(name) pep8.input_dir( dir ) if opts.get("output")=='json': self.generateJsonOutput(reportMessages) elif opts.get("output")=='xml': self.generateXmlOutput(reportMessages) else: self.generateConsoleOutput(appMessages)
def do_test(self): arglist = ['--exclude=', filepath] pep8.process_options(arglist) pep8.input_dir(filepath) output = pep8.get_statistics() print "PEP8 OUTPUT: " + str(output) self.assertEqual(len(output), 0)
def test_fullhouse_test(self): filepath = os.path.join(settings.PROJECT_ROOT, 'fullhouse/test') arglist = [filepath] pep8.process_options(arglist) pep8.input_dir(filepath)
def check_pep8(srcdir): print(">>> Running pep8...") clean = True pep8.process_options(['']) for pyfile in findpy(srcdir): if pep8.Checker(pyfile).check_all() != 0: clean = False return clean
def closure(self, fullpath=fullpath): pep8.process_options([ '--first', fullpath, '--ignore', ','.join(PEP8_IGNORE)], ) pep8.input_file(fullpath) if len(pep8.get_statistics()): self.fail('PEP8 issue in "%s"' % fullpath)
def do_test(self): #print "PATH:", filepath arglist = ['--exclude=lib', filepath] pep8.process_options(arglist) pep8.input_dir(filepath) output = pep8.get_statistics() #print "PEP8 OUTPUT: " + str(output) self.assertEqual(len(output), 0)
def check_filename(self, root, filename): pep8.process_options( ["--repeat", "--select=%s" % (",".join(ERRORS),), filename] ) pep8.input_file(filename) result = pep8.get_count() if result: raise AssertionError("ERROR: %d PEP8 errors in %s" % (result, filename,))
def run_pep8(options): import pep8 from settings import testable_modules as modules # alas, pep8 can out go to stdout arglist = ["-r"] + modules pep8.process_options(arglist) for module in modules: pep8.input_dir(module, runner=pep8.input_file)
def pep8_error_count(path): # process_options initializes some data structures and MUST be called before each Checker().check_all() pep8.process_options([ 'pep8', '--ignore=E202,E221,E222,E241,E301,E302,E401,E501,E701,W391,W601,W602', '--show-source', 'dummy_path' ]) error_count = pep8.Checker(path).check_all() return error_count
def check_filename(self, root, filename): pep8.process_options([ '--repeat', '--select=%s' % (','.join(ERRORS), ), filename ]) pep8.input_file(filename) result = pep8.get_count() if result: raise AssertionError("ERROR: %d PEP8 errors in %s" % (result, filename))
def test_fullhouse_dashboard_py(self): ''' Check style guide in dashboard root level. This excludes the south migration folder. ''' filepath = os.path.join(settings.PROJECT_ROOT, 'fullhouse/dashboard/*.py') arglist = [filepath] pep8.process_options(arglist) pep8.input_dir(filepath)
def __init__(self, pep8_args, propose_arc_fixes=False): pep8.process_options(pep8_args + ['dummy']) self._propose_arc_fixes = propose_arc_fixes # Our version of pep8 thinks that python3-style type annotaions are # multiple statements on one line. We therefore ignore this rule for # python3. (E701 is "multiple statements on one line".) # TODO(colin): it would be nice to use this lint rule. Change pep8 to # pycodestyle (its successor; pep8 has had its final release), which # correctly recognizes these annotations, and then remove this ignore. if six.PY3: Pep8.GLOBAL_IGNORES.append('E701')
def run_pep8(): """ Use the pep8 package to analyze the Dragonfly source code. """ import pep8 argv = [ sys.argv[0], "--filename=*.py", ] from pkg_resources import resource_filename setup_path = os.path.abspath(resource_filename(__name__, "setup.py")) directory = os.path.dirname(setup_path) pep8.process_options(argv) pep8.input_dir(directory)
def check_file(path): pep8.process_options(['pep8', #taken from moin '--ignore=E202,E221,E222,E241,E301,E302,E401,E501,E701,W391,W601,W602', '--show-source', 'dummy file', ]) checker = PyTestChecker(str(path)) error_count = checker.check_all() ignored = checker.ignored_errors return max(error_count - ignored, 0)
def check_file(self, path): """ Check one regular file with pylint for py syntax errors. :param path: Path to a regular file. :return: False, if pylint found syntax problems, True, if pylint didn't find problems, or path is not a python module or script. """ inspector = PathInspector(path=path, args=self.args) if inspector.is_toignore(): return True if not inspector.is_python(): return True try: opt_obj = pep8.StyleGuide().options ignore_list = self.args.disable.split(',') + list(opt_obj.ignore) opt_obj.ignore = tuple(set(ignore_list)) # pylint: disable=E1123 runner = pep8.Checker(filename=path, options=opt_obj) except Exception: opts = ['--ignore'] + self.args.disable.split(',') pep8.process_options(opts) runner = pep8.Checker(filename=path) try: status = runner.check_all() except: log.error('Unexpected exception while checking %s', path) exc_info = sys.exc_info() stacktrace.log_exc_info(exc_info, 'inspektor.style') status = 1 if status != 0: log.error('PEP8 check fail: %s', path) self.failed_paths.append(path) if AUTOPEP8_CAPABLE: if self.args.fix: log.info('Trying to fix errors with autopep8') try: auto_args = [path, '--in-place', ('--max-line-length=%s' % self.args.max_line_length)] if self.args.disable: auto_args.append("--ignore='%s'" % self.args.disable) opt_obj = autopep8.parse_args(auto_args) autopep8.fix_file(path, opt_obj) except Exception: log.error('Unable to fix errors') exc_info = sys.exc_info() stacktrace.log_exc_info(exc_info, 'inspektor.style') return status == 0
def checkPep8(currentDocument=None, refresh=True): """Check the pep8 errors of the document""" if not canCheckDocument(currentDocument): return if refresh: checkAll.f(currentDocument, ['checkPep8'], exclude_all=not currentDocument) move_cursor = not currentDocument currentDocument = currentDocument or kate.activeDocument() if currentDocument.isModified(): saveFirst() return path = currentDocument.url().path() if not path: saveFirst() return mark_key = '%s-pep8' % currentDocument.url().path() # Check the file for errors with PEP8 sys.argv = [path] pep8.process_options([path]) python_utils_conf = kate.configuration.root.get('python_utils', {}) ignore_pep8_errors = python_utils_conf.get(_IGNORE_PEP8_ERRORS, DEFAULT_IGNORE_PEP8_ERRORS) if ignore_pep8_errors: ignore_pep8_errors = ignore_pep8_errors.split(",") else: ignore_pep8_errors = [] if pep8.__version__ in OLD_PEP8_VERSIONS: checker = StoreErrorsChecker(path) pep8.options.ignore = ignore_pep8_errors checker.check_all() errors = checker.get_errors() else: checker = pep8.Checker(path, reporter=KateReport, ignore=ignore_pep8_errors) checker.check_all() errors = checker.report.get_errors() if len(errors) == 0: showOk(i18n('Pep8 Ok')) return errors_to_show = [] # Paint errors found for error in errors: errors_to_show.append({ "line": error[0], "column": error[1] + 1, "message": error[3], }) showErrors(i18n('Pep8 Errors:'), errors_to_show, mark_key, currentDocument, move_cursor=move_cursor)
def pluginsLoaded(self, event): self.pep8 = pep8 and self.config.as_bool('pep8', default=False) self.pyflakes = pyflakes_check and self.config.as_bool('pyflakes', default=False) if not pep8 and not pyflakes_check: raise AssertionError('checker plugin requires pep8 or pyflakes') if self.pep8: pep8.process_options(['pep8', PEP8_IGNORE_LIST, '--show-source', '--repeat', 'dummy file', ])
def checkPep8(currentDocument=None, refresh=True): """Check the pep8 errors of the document""" if not canCheckDocument(currentDocument): return if refresh: checkAll.f(currentDocument, ['checkPep8'], exclude_all=not currentDocument) move_cursor = not currentDocument currentDocument = currentDocument or kate.activeDocument() if currentDocument.isModified(): saveFirst() return path = unicode(currentDocument.url().path()) if not path: saveFirst() return mark_key = '%s-pep8' % unicode(currentDocument.url().path()) # Check the file for errors with PEP8 sys.argv = [path] pep8.process_options([path]) if pep8.__version__ in OLD_PEP8_VERSIONS: checker = StoreErrorsChecker(path) pep8.options.ignore = IGNORE_PEP8_ERRORS checker.check_all() errors = checker.get_errors() else: checker = pep8.Checker(path, reporter=KateReport, ignore=IGNORE_PEP8_ERRORS) checker.check_all() errors = checker.report.get_errors() if len(errors) == 0: showOk('Pep8 Ok') return errors_to_show = [] # Paint errors found for error in errors: errors_to_show.append({ "line": error[0], "column": error[1] + 1, "message": error[3], }) showErrors('Pep8 Errors:', errors_to_show, mark_key, currentDocument, move_cursor=move_cursor)
def test_pep8(): arglist = [ "--statistics", "--filename=*.py", "--show-source", "--repeat", "--exclude=SVGdraw.py", "--ignore=E302,E701", #"--show-pep8", #"-qq", #"-v", BASE_DIR, ] options, args = pep8.process_options(arglist) runner = pep8.input_file for path in args: if isdir(path): pep8.input_dir(path, runner=runner) elif not pep8.excluded(path): options.counters["files"] += 1 runner(path) pep8.print_statistics() errors = pep8.get_count("E") warnings = pep8.get_count("W") message = "pep8: %d errors / %d warnings" % (errors, warnings) print(message) assert errors + warnings == 0, message
def test_pep8(): arglist = [ "--statistics", "--filename=*.py", "--show-source", "--repeat", "--exclude=SVGdraw.py", "--ignore=E302,E701", #"--show-pep8", #"-qq", #"-v", BASE_DIR, ] options, args = pep8.process_options(arglist) runner = pep8.input_file for path in args: if isdir(path): pep8.input_dir(path, runner=runner) elif not pep8.excluded(path): options.counters["files"] += 1 runner(path) pep8.print_statistics() errors = pep8.get_count("E") warnings = pep8.get_count("W") message = "pep8: %d errors / %d warnings" % (errors, warnings) print message assert errors + warnings == 0, message
def test_pep8(): arglist = [ "--statistics", "--filename=*.py", "--show-source", "--benchmark", "--repeat", "--show-pep8", #'--qq', #'-v', BASE_DIR, ] options, args = pep8.process_options(arglist) runner = pep8.input_file for path in args: if os.path.isdir(path): pep8.input_dir(path, runner=runner) elif not pep8.excluded(path): options.counters["files"] += 1 runner(path) pep8.print_statistics() errors = pep8.get_count("E") warnings = pep8.get_count("W") message = "pep8: %d errors / %d warnings" % (errors, warnings) print message assert errors + warnings == 0, message
def test_pep8(): arglist = [ '--statistics', '--filename=*.py', '--show-source', '--repeat', #'--show-pep8', #'-qq', #'-v', BASE_DIR, ] options, args = pep8.process_options(arglist) runner = pep8.input_file for path in args: if os.path.isdir(path): pep8.input_dir(path, runner=runner) elif not pep8.excluded(path): options.counters['files'] += 1 runner(path) pep8.print_statistics() errors = pep8.get_count('E') warnings = pep8.get_count('W') message = 'pep8: %d errors / %d warnings' % (errors, warnings) print message assert errors + warnings == 0, message
def configure(self, options, config): plugins.Plugin.configure(self, options, config) self.conf = config self.tissue_packages = [] self.tissue_statistics = options.tissue_statistics self.tissue_fail_on_error = options.tissue_fail_on_error if options.tissue_packages: for pkgs in [util.tolist(x) for x in options.tissue_packages]: self.tissue_packages.extend(pkgs) self.tissue_inclusive = options.tissue_inclusive if self.tissue_packages: log.info('PEP8 report will include only packages: %s', self.tissue_packages) arglist = [] if options.tissue_repeat: arglist.append('--repeat') if options.tissue_select: arglist.append('--select') arglist.append(options.tissue_select) if options.tissue_ignore: arglist.append('--ignore') arglist.append(options.tissue_ignore) if options.tissue_show_source: arglist.append('--show-source') if options.tissue_show_pep8: arglist.append('--show-pep8') options, paths = pep8.process_options(arglist) self.pep8 = pep8.StyleGuide(**options.__dict__) self.pep8.init_report(TissueReport)
def pep8_report(self): """ Outputs PEP8 report to screen and pep8.txt. """ verbose = '--quiet' not in sys.argv if verbose: # Hook into stdout. old_stdout = sys.stdout sys.stdout = mystdout = StringIO() # Run Pep8 checks. pep8.options, pep8.args = pep8.process_options() pep8.args = self.packages pep8.options.repeat = True for package in self.packages: pep8.input_dir(package) # Restore stdout. sys.stdout = old_stdout # Save result to pep8.txt. result = mystdout.getvalue() output = open('pep8.txt', 'w') output.write(result) output.close() # Return Pep8 result if result: log.info("\nPEP8 Report:") log.info(result)
def test_pep8(): arglist = [ '--statistics', '--filename=*.py', '--show-source', '--benchmark', '--repeat', '--show-pep8', #'--qq', #'-v', BASE_DIR, ] options, args = pep8.process_options(arglist) runner = pep8.input_file for path in args: if os.path.isdir(path): pep8.input_dir(path, runner=runner) elif not pep8.excluded(path): options.counters['files'] += 1 runner(path) pep8.print_statistics() errors = pep8.get_count('E') warnings = pep8.get_count('W') message = 'pep8: %d errors / %d warnings' % (errors, warnings) print(message) assert errors + warnings == 0, message
def teardown_test_environment(self, **kwargs): locations = get_apps_locations(self.test_labels, self.test_all) pep8.process_options(self.pep8_options + locations) # run pep8 tool with captured output def report_error(instance, line_number, offset, text, check): code = text[:4] if pep8.ignore_code(code): return sourceline = instance.line_offset + line_number self.output.write('%s:%s:%s: %s\n' % (instance.filename, sourceline, offset+1, text)) pep8.Checker.report_error = report_error for location in locations: pep8.input_dir(relpath(location), runner=pep8.input_file) self.output.close()
def output(self): options = [self.path, '--repeat'] options.extend(('--ignore', self.get_ignored_ids())) stdout, sys.stdout = sys.stdout, StringIO.StringIO() try: import pep8 pep8.MAX_LINE_LENGTH = self.max_line_length or 79 pep8.process_options(options) pep8.input_file(self.path) return sys.stdout.getvalue().strip() finally: sys.stdout = stdout
def _process_file(contents): with tempfile.NamedTemporaryFile(delete=False) as f: f.write(contents) options, args = pep8.process_options(config_file=f.name) os.remove(f.name) return options, args
def checkPep8(currentDocument=None, refresh=True): """Check the pep8 errors of the document""" if not commons.canCheckDocument(currentDocument): return if refresh: checkAll.f(currentDocument, ['checkPep8'], exclude_all=not currentDocument) move_cursor = not currentDocument currentDocument = currentDocument or kate.activeDocument() if currentDocument.isModified(): saveFirst() return path = unicode(currentDocument.url().path()) if not path: saveFirst() return mark_key = '%s-pep8' % unicode(currentDocument.url().path()) # Check the file for errors with PEP8 sys.argv = [path] pep8.process_options([path]) if pep8.__version__ in OLD_PEP8_VERSIONS: checker = StoreErrorsChecker(path) pep8.options.ignore = IGNORE_PEP8_ERRORS checker.check_all() errors = checker.get_errors() else: checker = pep8.Checker(path, reporter=KateReport, ignore=IGNORE_PEP8_ERRORS) checker.check_all() errors = checker.report.get_errors() if len(errors) == 0: commons.showOk('Pep8 Ok') return errors_to_show = [] # Paint errors found for error in errors: errors_to_show.append({ "line": error[0], "column": error[1] + 1, "message": error[3], }) commons.showErrors('Pep8 Errors:', errors_to_show, mark_key, currentDocument, move_cursor=move_cursor)
def main(): global pep8style # parse out our flags so pep8 doesn't get confused parser = get_parser() # This is required so we can parse out our added options opts, args = pep8.process_options(parse_argv=True, parser=parser) if opts.install_hook: from flake8.hooks import install_hook install_hook() if opts.builtins: s = '--builtins={0}'.format(opts.builtins) sys.argv.remove(s) if opts.exit_zero: sys.argv.remove('--exit-zero') if opts.install_hook: sys.argv.remove('--install-hook') complexity = opts.max_complexity if complexity > 0: sys.argv.remove('--max-complexity={0}'.format(complexity)) # make sure pep8 gets the information it expects sys.argv.pop(0) sys.argv.insert(0, 'pep8') read_config(opts, parser) # We then have to re-parse argv to make sure pep8 is properly initialized pep8style = pep8.StyleGuide(parse_argv=True, config_file=True) merge_opts(pep8style.options, opts) warnings = 0 stdin = None builtins = set(opts.builtins.split(',')) if builtins: orig_builtins = set(flakey.checker._MAGIC_GLOBALS) flakey.checker._MAGIC_GLOBALS = orig_builtins | builtins if pep8style.paths and pep8style.options.filename is not None: for path in _get_python_files(pep8style.paths): if path == '-': if stdin is None: stdin = read_stdin() warnings += check_code(stdin, opts.ignore, complexity) else: warnings += check_file(path, opts.ignore, complexity) else: stdin = read_stdin() warnings += check_code(stdin, opts.ignore, complexity) if opts.exit_zero: raise SystemExit(0) raise SystemExit(warnings)
def checkPep8(currentDocument=None, refresh=True): if not commons.canCheckDocument(currentDocument): return if refresh: checkAll(currentDocument, ['checkPep8'], exclude_all=not currentDocument) move_cursor = not currentDocument currentDocument = currentDocument or kate.activeDocument() if currentDocument.isModified(): kate.gui.popup('You must save the file first', 3, icon='dialog-warning', minTextWidth=200) return path = unicode(currentDocument.url().path()) mark_key = '%s-pep8' % unicode(currentDocument.url().path()) # Check the file for errors with PEP8 sys.argv = [path] pep8.process_options([path]) checker = StoreErrorsChecker(path) checker.check_all() errors = checker.get_errors() if len(errors) == 0: commons.showOk('Pep8 Ok') return errors_to_show = [] if IGNORE_PEP8_ERRORS: pep8.options.ignore.extend(IGNORE_PEP8_ERRORS) pep8.options.ignore = list(set(pep8.options.ignore)) # Paint errors found for error in errors: if pep8.ignore_code(error[2]): continue errors_to_show.append({ "line": error[0], "column": error[1] + 1, "filename": path, "message": error[3], }) if errors_to_show: commons.showErrors('Pep8 Errors:', errors_to_show, mark_key, currentDocument, move_cursor=move_cursor) else: commons.showOk('Pep8 Ok')
def configure(self, options, config): plugins.Plugin.configure(self, options, config) self.conf = config self.tissue_packages = [] self.tissue_statistics = options.tissue_statistics if options.tissue_packages: for pkgs in [util.tolist(x) for x in options.tissue_packages]: self.tissue_packages.extend(pkgs) self.tissue_inclusive = options.tissue_inclusive if self.tissue_packages: log.info("PEP8 report will include only packages: %s", self.coverPackages) # NOTE(jkoelker) Monkey-patch pep8 to not print directly def message(text): # if the output has a filename, then it should be colored if # the tissue_color option is used if ':' in text and os.path.exists(text.split(':')[0]): if options.tissue_color: if 'E' in text.split(':')[-1]: text = in_color('red', text) else: text = in_color('yellow', text) # if using the tissue_show_source or tissue_show_pep8, it # should separate the filename from the information if options.tissue_show_pep8 or options.tissue_show_source: text = "\n%s\n" % text self.messages.append(text) pep8.message = message # NOTE(jkoelker) Urgh! Really? Global options? At least there is a # function that takes the arglist ;( arglist = [] if options.tissue_repeat: arglist.append("--repeat") if options.tissue_select: arglist.append("--select") arglist.append(options.tissue_select) if options.tissue_ignore: arglist.append("--ignore") arglist.append(options.tissue_ignore) if options.tissue_show_source: arglist.append("--show-source") if options.tissue_show_pep8: arglist.append("--show-pep8") # NOTE(jkoelker) PEP8 requires something to be left over in args arglist.append("hozer") tissue_options, tissue_args = pep8.process_options(arglist)
def check(self, code, filename): """Run pep8 on code and return the output.""" options = {'reporter': self.get_report()} type_map = { 'select': [], 'ignore': [], 'max-line-length': 0, 'max-complexity': 0 } self.build_options(options, type_map, transform=lambda s: s.replace('-', '_')) final_options = options.copy() # Try to read options from pep8 default configuration files (.pep8, tox.ini). # If present, they will override the ones defined by Sublime Linter's config. try: # `onError` will be called by `process_options` when no pep8 configuration file is found. # Override needed to supress OptionParser.error() output in the default parser. def onError(msg): pass from pep8 import process_options, get_parser parser = get_parser() parser.error = onError pep8_options, _ = process_options([os.curdir], True, True, parser=parser) # Merge options only if the pep8 config file actually exists; # pep8 always returns a config filename, even when it doesn't exist! if os.path.isfile(pep8_options.config): pep8_options = vars(pep8_options) pep8_options.pop('reporter', None) for opt_n, opt_v in pep8_options.items(): if isinstance(final_options.get(opt_n, None), list): final_options[opt_n] += opt_v else: final_options[opt_n] = opt_v except SystemExit: # Catch and ignore parser.error() when no config files are found. pass if persist.debug_mode(): persist.printf('{} ST options: {}'.format(self.name, options)) persist.printf('{} options: {}'.format(self.name, final_options)) checker = self.module.StyleGuide(**final_options) return checker.input_file(filename=os.path.basename(filename), lines=code.splitlines(keepends=True))
def _execute_pep8(self, targetfile): """execute pep8 via python method calls.""" pep8.options, pep8.args = pep8.process_options(['pep8', '-r', self.filename]) sys_stdout = sys.stdout fake_stdout = StringIO() sys.stdout = fake_stdout tmp_checker = pep8.Checker(self.filename, lines=self.source) errors = tmp_checker.check_all() sys.stdout = sys_stdout result = fake_stdout.getvalue() return StringIO(result).readlines()
def teardown_test_environment(self, **kwargs): locations = [os.path.dirname(get_app(app_name.split('.')[-1]).__file__) \ for app_name in get_apps_under_test(self.test_labels, self.test_all)] pep8.process_options(self.pep8_options + locations) # run pep8 tool with captured output def report_error(instance, line_number, offset, text, check): code = text[:4] if pep8.ignore_code(code): return filepath = relpath(instance.filename) message = re.sub(r'([WE]\d+)', r'[\1] PEP8:', text) sourceline = instance.line_offset + line_number self.output.write('%s:%s: %s\n' % (filepath, sourceline, message)) pep8.Checker.report_error = report_error for location in locations: pep8.input_dir(location, runner=pep8.input_file) self.output.close()
def _check_pep8(self, path): if not install_flg['pep8']: return [{'line': 0, 'msg': 'no install pep8'}] pep8.process_options(['']) t = tempfile.TemporaryFile() sys.stdout = t pep8.input_file(path) t.seek(0) s = t.read() sys.stdout.close() sys.stdout = sys.__stdout__ res = [] arr = s.split('\n') for e in arr: if e.strip() == '': continue cols = e.split(':') res.append({'line': int(cols[1]), 'msg': ':'.join(cols[3:])}) return res
def display_pep8(self, path, summary=True): pep8_out = StringIO() try: with RedirectedIO(target=pep8_out, close_target=False): pep8.process_options([path]) pep8.input_file(path) error_stats = pep8.get_error_statistics() warning_stats = pep8.get_warning_statistics() val = pep8_out.getvalue().splitlines() for line in [ x.split(':',1)[1] for x in val if ':' in x]: self.writeln(line) if summary: self.writeln() self.writeln("Summary:") for e in error_stats: self.writeln(e) for w in warning_stats: self.writeln(w) self.writeln() except tokenize.TokenError: self.boxed_text(["PEP8 processing failed - check your source code"], symbol="#")
def configure(self, options, config): plugins.Plugin.configure(self, options, config) self.conf = config self.tissue_packages = [] self.tissue_statistics = options.tissue_statistics if options.tissue_packages: for pkgs in [util.tolist(x) for x in options.tissue_packages]: self.tissue_packages.extend(pkgs) self.tissue_inclusive = options.tissue_inclusive if self.tissue_packages: log.info("PEP8 report will include only packages: %s", self.tissue_packages) # NOTE(jkoelker) Urgh! Really? Global options? At least there is a # function that takes the arglist ;( arglist = [] if options.tissue_repeat: arglist.append("--repeat") if options.tissue_select: arglist.append("--select") arglist.append(options.tissue_select) if options.tissue_ignore: arglist.append("--ignore") arglist.append(options.tissue_ignore) if options.tissue_show_source: arglist.append("--show-source") if options.tissue_show_pep8: arglist.append("--show-pep8") options, paths = pep8.process_options(arglist) self.pep8 = pep8.StyleGuide(**options.__dict__) self.pep8.init_report(TissueReport)
#!/usr/local/bin/python3.2 import tkinter from tkinter.filedialog import askopenfilename import sys import pep8 if __name__ == "__main__": root = tkinter.Tk() py_file = askopenfilename(title="Select a Python file to stylecheck ...", initialdir=".") if not py_file: sys.exit("No file selected") pep8.process_options(['-v', '--count', py_file]) pep8.input_file(py_file) if pep8.get_statistics() == []: print("Congrats! No style errors were detected.")
def __init__(self, pep8_args): pep8.process_options(pep8_args + ['dummy'])
def __init__(self, pep8_args, propose_arc_fixes=False): pep8.process_options(pep8_args + ['dummy']) self._propose_arc_fixes = propose_arc_fixes
def __init__(self): BaseChecker.__init__(self) arglist = ['--repeat', '--show-source'] arglist.append('dummy') pep8.process_options(arglist=arglist)
Like most Python projects, we try to adhere to :pep:`8` (Style Guide for Python Code) and :pep:`257` (Docstring Conventions) with the modifications documented in the :ref:`coding-style-guide`. Be sure to read those documents if you intend to contribute code to ObsPy. Here are the results of the automatic PEP 8 syntax checker: """) # backup stdout stdout = sys.stdout sys.stdout = StringIO() # clean up runner options options, args = process_options() options.repeat = True input_dir(path, runner=input_file) sys.stdout.seek(0) data = sys.stdout.read() statistic = get_statistics('') count = get_count() if count == 0: fh.write("The PEP 8 checker didn't find any issues.\n") else: fh.write("\n") fh.write(".. rubric:: Statistic\n") fh.write("\n") fh.write("======= =====================================================\n") fh.write("Count PEP 8 message string \n")