def register_checks(): import pep8 from sentry.lint.absolute_import_check import AbsoluteImportCheck from sentry.lint.mock_check import MockCheck pep8.register_check(MockCheck, codes=[MockCheck.code]) pep8.register_check(AbsoluteImportCheck, codes=[AbsoluteImportCheck.code])
def run_check(self, code): pep8.register_check(self.get_checker()) lines = textwrap.dedent(code).strip().splitlines(True) checker = pep8.Checker(lines=lines) checker.check_all() checker.report._deferred_print.sort() return checker.report._deferred_print
def patch_pep8(): if autopep8_c_i in p8._checks['logical_line']: del p8._checks['logical_line'][autopep8_c_i] p8.register_check(p8.continued_indentation) try: yield finally: del p8._checks['logical_line'][p8.continued_indentation] p8.register_check(autopep8_c_i)
def _run_check(self, code, checker, filename=None): pep8.register_check(checker) lines = textwrap.dedent(code).strip().splitlines(True) checker = pep8.Checker(filename=filename, lines=lines) checker.check_all() checker.report._deferred_print.sort() return checker.report._deferred_print
def run_check(self, code): pep8.register_check(checks.check_oslo_namespace_imports) lines = textwrap.dedent(code).strip().splitlines(True) checker = pep8.Checker(lines=lines) checker.check_all() checker.report._deferred_print.sort() return checker.report._deferred_print
def run_check(self, code, checker, filename=None): pep8.register_check(checker) lines = textwrap.dedent(code).strip().splitlines(True) checker = pep8.Checker(filename=filename, lines=lines) with mock.patch('pep8.StandardReport.get_file_results'): checker.check_all() checker.report._deferred_print.sort() return checker.report._deferred_print
def test_check_nullbytes(self): pep8.register_check(DummyChecker, ['Z701']) pep8style = pep8.StyleGuide() count_errors = pep8style.input_file('stdin', lines=['\x00\n']) self.assertTrue(sys.stdout[0].startswith("stdin:1:1: E901 TypeError")) self.assertFalse(sys.stderr) self.assertEqual(count_errors, 1)
def test_register_physical_check(self): def check_dummy(physical_line, line_number): if False: yield pep8.register_check(check_dummy, ['Z001']) self.assertTrue(check_dummy in pep8._checks['physical_line']) codes, args = pep8._checks['physical_line'][check_dummy] self.assertTrue('Z001' in codes) self.assertEqual(args, ['physical_line', 'line_number'])
def test_register_ast_check(self): pep8.register_check(DummyChecker, ['Z701']) self.assertTrue(DummyChecker in pep8._checks['tree']) codes, args = pep8._checks['tree'][DummyChecker] self.assertTrue('Z701' in codes) self.assertTrue(args is None) options = pep8.StyleGuide().options self.assertTrue( any(cls == DummyChecker for name, cls, args in options.ast_checks))
def test_register_ast_check(self): pep8.register_check(DummyChecker, ['Z701']) self.assertTrue(DummyChecker in pep8._checks['tree']) codes, args = pep8._checks['tree'][DummyChecker] self.assertTrue('Z701' in codes) self.assertTrue(args is None) options = pep8.StyleGuide().options self.assertTrue(any(cls == DummyChecker for name, cls, args in options.ast_checks))
def run_check(self, code): pep8.register_check(self.get_checker()) lines = textwrap.dedent(code).strip().splitlines(True) # Load all keystone hacking checks, they are of the form Kddd, # where ddd can from range from 000-999 guide = pep8.StyleGuide(select='K') checker = pep8.Checker(lines=lines, options=guide.options) checker.check_all() checker.report._deferred_print.sort() return checker.report._deferred_print
def test_styleguide_unmatched_triple_quotes(self): pep8.register_check(DummyChecker, ['Z701']) lines = [ 'def foo():\n', ' """test docstring""\'\n', ] pep8style = pep8.StyleGuide() pep8style.input_file('stdin', lines=lines) stdout = sys.stdout.getvalue() expected = 'stdin:2:5: E901 TokenError: EOF in multi-line string' self.assertTrue(expected in stdout)
def test_check_unicode(self): # Do not crash if lines are Unicode (Python 2.x) pep8.register_check(DummyChecker, ['Z701']) source = '#\n' if hasattr(source, 'decode'): source = source.decode('ascii') pep8style = pep8.StyleGuide() count_errors = pep8style.input_file('stdin', lines=[source]) self.assertFalse(sys.stdout) self.assertFalse(sys.stderr) self.assertEqual(count_errors, 0)
def _run_check(self, code, checker, filename=None): pep8.register_check(checker) lines = textwrap.dedent(code).strip().splitlines(True) checker = pep8.Checker(filename=filename, lines=lines) # NOTE(sdague): the standard reporter has printing to stdout # as a normal part of check_all, which bleeds through to the # test output stream in an unhelpful way. This blocks that printing. with mock.patch('pep8.StandardReport.get_file_results'): checker.check_all() checker.report._deferred_print.sort() return checker.report._deferred_print
def test_register_physical_check(self): def check_dummy(physical_line, line_number): if False: yield pep8.register_check(check_dummy, ['Z001']) self.assertTrue(check_dummy in pep8._checks['physical_line']) codes, args = pep8._checks['physical_line'][check_dummy] self.assertTrue('Z001' in codes) self.assertEqual(args, ['physical_line', 'line_number']) options = pep8.StyleGuide().options self.assertTrue(any(func == check_dummy for name, func, args in options.physical_checks))
def test_register_ast_check(self): class DummyChecker(object): def __init__(self, tree, filename): pass def run(self): if False: yield pep8.register_check(DummyChecker, ['Z701']) self.assertTrue(DummyChecker in pep8._checks['tree']) codes, args = pep8._checks['tree'][DummyChecker] self.assertTrue('Z701' in codes) self.assertTrue(args is None)
def test_check_nullbytes(self): pep8.register_check(DummyChecker, ['Z701']) pep8style = pep8.StyleGuide() count_errors = pep8style.input_file('stdin', lines=['\x00\n']) stdout = sys.stdout.getvalue() if 'SyntaxError' in stdout: # PyPy 2.2 returns a SyntaxError expected = "stdin:1:2: E901 SyntaxError" else: expected = "stdin:1:1: E901 TypeError" self.assertTrue(stdout.startswith(expected), msg='Output {0!r} does not start with {1!r}'.format(stdout, expected)) self.assertFalse(sys.stderr) self.assertEqual(count_errors, 1)
def test_register_invalid_check(self): class InvalidChecker(DummyChecker): def __init__(self, filename): pass def check_dummy(logical, tokens): if False: yield pep8.register_check(InvalidChecker, ['Z741']) pep8.register_check(check_dummy, ['Z441']) for checkers in pep8._checks.values(): self.assertTrue(DummyChecker not in checkers) self.assertTrue(check_dummy not in checkers) self.assertRaises(TypeError, pep8.register_check)
def test_register_physical_check(self): def check_dummy(physical_line, line_number): if False: yield pep8.register_check(check_dummy, ['Z001']) self.assertTrue(check_dummy in pep8._checks['physical_line']) codes, args = pep8._checks['physical_line'][check_dummy] self.assertTrue('Z001' in codes) self.assertEqual(args, ['physical_line', 'line_number']) options = pep8.StyleGuide().options self.assertTrue( any(func == check_dummy for name, func, args in options.physical_checks))
def test_api_version_decorator_good(self): code = """ class SomeController(): @wsgi.api_version("2.2") def my_method(): pass """ lines = textwrap.dedent(code).strip().splitlines(True) pep8.register_check(checks.check_api_version_decorator) checker = pep8.Checker(filename=None, lines=lines) checker.check_all() checker.report._deferred_print.sort() actual_error = checker.report._deferred_print self.assertEqual(0, len(actual_error))
def test_check_nullbytes(self): pep8.register_check(DummyChecker, ['Z701']) pep8style = pep8.StyleGuide() count_errors = pep8style.input_file('stdin', lines=['\x00\n']) stdout = sys.stdout.getvalue() if 'SyntaxError' in stdout: # PyPy 2.2 returns a SyntaxError expected = "stdin:1:2: E901 SyntaxError" else: expected = "stdin:1:1: E901 TypeError" self.assertTrue(stdout.startswith(expected), msg='Output %r does not start with %r' % (stdout, expected)) self.assertFalse(sys.stderr) self.assertEqual(count_errors, 1)
def add_options(cls, parser): # We're looking for local checks, so we need to include the local # dir in the search path sys.path.append('.') local_check = CONF.get_multiple('local-check', default=[]) for check_path in set(local_check): if check_path.strip(): checker = pbr.util.resolve_name(check_path) pep8.register_check(checker) local_check_fact = CONF.get('local-check-factory') if local_check_fact: factory = pbr.util.resolve_name(local_check_fact) factory(pep8.register_check) sys.path.pop()
def _run_check(self, code, checker, filename=None): # We are patching pep8 so that only the check under test is actually # installed. mock_checks = {'physical_line': {}, 'logical_line': {}, 'tree': {}} with mock.patch('pep8._checks', mock_checks): pep8.register_check(checker) lines = textwrap.dedent(code).strip().splitlines(True) checker = pep8.Checker(filename=filename, lines=lines) # NOTE(sdague): the standard reporter has printing to stdout # as a normal part of check_all, which bleeds through to the # test output stream in an unhelpful way. This blocks that # printing. with mock.patch('pep8.StandardReport.get_file_results'): checker.check_all() checker.report._deferred_print.sort() return checker.report._deferred_print
def test_register_ast_check(self): class DummyChecker(object): def __init__(self, tree, filename): pass def run(self): if False: yield pep8.register_check(DummyChecker, ['Z701']) self.assertTrue(DummyChecker in pep8._checks['tree']) codes, args = pep8._checks['tree'][DummyChecker] self.assertTrue('Z701' in codes) self.assertTrue(args is None) options = pep8.StyleGuide().options self.assertTrue(any(cls == DummyChecker for name, cls, args in options.ast_checks))
def test_register_ast_check(self): class DummyChecker(object): def __init__(self, tree, filename): pass def run(self): if False: yield pep8.register_check(DummyChecker, ['Z701']) self.assertTrue(DummyChecker in pep8._checks['tree']) codes, args = pep8._checks['tree'][DummyChecker] self.assertTrue('Z701' in codes) self.assertTrue(args is None) options = pep8.StyleGuide().options self.assertTrue( any(cls == DummyChecker for name, cls, args in options.ast_checks))
def _register_extensions(): """Register all the extensions.""" extensions = OrderedSet() extensions.add(('pep8', pep8.__version__)) parser_hooks = [] options_hooks = [] try: from pkg_resources import iter_entry_points except ImportError: pass else: for entry in iter_entry_points('flake8.extension'): checker = entry.load() pep8.register_check(checker, codes=[entry.name]) extensions.add((checker.name, checker.version)) if hasattr(checker, 'add_options'): parser_hooks.append(checker.add_options) if hasattr(checker, 'parse_options'): options_hooks.append(checker.parse_options) return extensions, parser_hooks, options_hooks
def test_styleguide_continuation_line_outdented(self): pep8.register_check(DummyChecker, ['Z701']) lines = [ 'def foo():\n', ' pass\n', '\n', '\\\n', '\n', 'def bar():\n', ' pass\n', ] pep8style = pep8.StyleGuide() count_errors = pep8style.input_file('stdin', lines=lines) self.assertEqual(count_errors, 2) stdout = sys.stdout.getvalue() expected = ('stdin:6:1: ' 'E122 continuation line missing indentation or outdented') self.assertTrue(expected in stdout) expected = 'stdin:6:1: E302 expected 2 blank lines, found 1' self.assertTrue(expected in stdout)
def _register_pyflakes_check(): """Register the pyFlakes checker into PEP8 set of checks.""" from flake8_isort import Flake8Isort from flake8_blind_except import check_blind_except # Resolving conflicts between pep8 and pyflakes. codes = { "UnusedImport": "F401", "ImportShadowedByLoopVar": "F402", "ImportStarUsed": "F403", "LateFutureImport": "F404", "Redefined": "F801", "RedefinedInListComp": "F812", "UndefinedName": "F821", "UndefinedExport": "F822", "UndefinedLocal": "F823", "DuplicateArgument": "F831", "UnusedVariable": "F841", } for name, obj in vars(pyflakes.messages).items(): if name[0].isupper() and obj.message: obj.tpl = "{0} {1}".format(codes.get(name, "F999"), obj.message) pep8.register_check(_PyFlakesChecker, codes=['F']) # FIXME parser hack parser = pep8.get_parser('', '') Flake8Isort.add_options(parser) options, args = parser.parse_args([]) # end of hack pep8.register_check(Flake8Isort, codes=['I']) pep8.register_check(check_blind_except, codes=['B90'])
def main(paths=["."]): pep8.register_check(checkNames) pep8.register_check(blankLines) pep8.register_check(checkStrings) ignore = [] ignore.append("E501") # Ignore line length violations. ignore.append("E226") # Ignore too many leading # in comment block. critical = [] critical.append("E301") # expected 1 blank line, found 0 critical.append("U302") # expected 2 blank lines, found 0 critical.append("E304") # blank lines found after function decorator critical.append("E401") # multiple imports on one line critical.append("E402") # module level import not at top of file critical.append("E713") # test for membership should be ‘not in’ critical.append("W191") # indentation contains tabs critical.append("U9") # Parsing errors (error in this script, or error in code that we're trying to parse) critical.append("U") # All Ultimaker specific stuff. pep8style = pep8.StyleGuide(quiet=False, select=critical, ignore=ignore, show_source=True) pep8style.init_report(XmlReport) for path in paths: if path.endswith(".py") and "_pb2.py" not in path: pep8style.paths.append(path) for base_path, _, filenames in os.walk(path): for filename in filenames: if filename.endswith(".py") and "_pb2.py" not in filename: pep8style.paths.append(os.path.join(base_path, filename)) result = pep8style.check_files() print("----------------------------------") result.print_statistics() print("Total: %d" % (result.get_count())) result.getJUnitXml().write('pep8_output.xml', 'utf-8', True)
def _register_pyflakes_check(): """Register the pyFlakes checker into PEP8 set of checks.""" # Resolving conflicts between pep8 and pyflakes. codes = { "UnusedImport": "F401", "ImportShadowedByLoopVar": "F402", "ImportStarUsed": "F403", "LateFutureImport": "F404", "Redefined": "F801", "RedefinedInListComp": "F812", "UndefinedName": "F821", "UndefinedExport": "F822", "UndefinedLocal": "F823", "DuplicateArgument": "F831", "UnusedVariable": "F841", } for name, obj in vars(pyflakes.messages).items(): if name[0].isupper() and obj.message: obj.tpl = "{0} {1}".format(codes.get(name, "F999"), obj.message) pep8.register_check(_PyFlakesChecker, codes=['F'])
def check_easyconfigs_style(easyconfigs, verbose=False): """ Check the given list of easyconfigs for style :param: easyconfigs list of file paths to easyconfigs :param: verbose print our statistics and be verbose about the errors and warning :return: the number of warnings and errors """ # importing autopep8 changes some pep8 functions. # We reload it to be sure to get the real pep8 functions. reload(pep8) # register the extra checks before using pep8: # any function in this module starting with `_eb_check_` will be used. cands = globals() for check_function in sorted([ cands[f] for f in cands if callable(cands[f]) and f.startswith(EB_CHECK) ]): _log.debug("Adding custom style check %s", check_function) pep8.register_check(check_function) pep8style = pep8.StyleGuide(quiet=False, config_file=None) options = pep8style.options # we deviate from standard pep8 and allow 120 chars # on a line: the default of 79 is too narrow. options.max_line_length = 120 # we ignore some tests # note that W291 has be replaced by our custom W299 options.ignore = ( 'W291', # replaced by W299 ) options.verbose = int(verbose) result = pep8style.check_files(easyconfigs) if verbose: result.print_statistics() return result.total_errors
def add_options(cls, parser): # Abusing this method because of when it gets called if not os.path.exists('tox.ini'): return tox_ini = ConfigParser.RawConfigParser() tox_ini.read('tox.ini') if not tox_ini.has_section('hacking'): return # We're looking for local checks, so we need to include the local # dir in the search path sys.path.append('.') if tox_ini.has_option('hacking', 'local-check'): for check_path in set( tox_ini.get('hacking', 'local-check').split(",")): if check_path.strip(): checker = d2to1.util.resolve_name(check_path) pep8.register_check(checker) if tox_ini.has_option('hacking', 'local-check-factory'): factory = d2to1.util.resolve_name( tox_ini.get('hacking', 'local-check-factory')) factory(pep8.register_check) sys.path.pop()
def _register_extensions(): """Register all the extensions.""" extensions = OrderedSet() extensions.add(("pep8", pep8.__version__)) parser_hooks = [] options_hooks = [] ignored_hooks = [] try: from pkg_resources import iter_entry_points except ImportError: pass else: for entry in iter_entry_points("flake8.extension"): checker = entry.load() pep8.register_check(checker, codes=[entry.name]) extensions.add((checker.name, checker.version)) if hasattr(checker, "add_options"): parser_hooks.append(checker.add_options) if hasattr(checker, "parse_options"): options_hooks.append(checker.parse_options) if getattr(checker, "off_by_default", False) is True: ignored_hooks.append(entry.name) return extensions, parser_hooks, options_hooks, ignored_hooks
def test_api_version_decorator(self): code = """ @some_other_decorator @wsgi.api_version("2.2") def my_method(): pass """ lines = textwrap.dedent(code).strip().splitlines(True) pep8.register_check(checks.check_api_version_decorator) checker = pep8.Checker(filename=None, lines=lines) checker.check_all() checker.report._deferred_print.sort() actual_error = checker.report._deferred_print[0] self.assertEqual(2, actual_error[0]) self.assertEqual(0, actual_error[1]) self.assertEqual('S321', actual_error[2]) self.assertEqual( ' The api_version decorator must be the first ' 'decorator on a method.', actual_error[3])
def test_styleguide_continuation_line_outdented(self): pep8.register_check(DummyChecker, ['Z701']) lines = [ 'def foo():\n', ' pass\n', '\n', '\\\n', '\n', 'def bar():\n', ' pass\n', ] pep8style = pep8.StyleGuide() count_errors = pep8style.input_file('stdin', lines=lines) self.assertEqual(count_errors, 2) stdout = sys.stdout.getvalue() expected = ( 'stdin:6:1: ' 'E122 continuation line missing indentation or outdented' ) self.assertTrue(expected in stdout) expected = 'stdin:6:1: E302 expected 2 blank lines, found 1' self.assertTrue(expected in stdout)
def test_api_version_decorator(self): code = """ @some_other_decorator @wsgi.api_version("2.2") def my_method(): pass """ lines = textwrap.dedent(code).strip().splitlines(True) pep8.register_check(checks.check_api_version_decorator) checker = pep8.Checker(filename=None, lines=lines) checker.check_all() checker.report._deferred_print.sort() actual_error = checker.report._deferred_print[0] self.assertEqual(2, actual_error[0]) self.assertEqual(0, actual_error[1]) self.assertEqual('S321', actual_error[2]) self.assertEqual(' The api_version decorator must be the first ' 'decorator on a method.', actual_error[3])
def check_easyconfigs_style(easyconfigs, verbose=False): """ Check the given list of easyconfigs for style :param: easyconfigs list of file paths to easyconfigs :param: verbose print our statistics and be verbose about the errors and warning :return: the number of warnings and errors """ # importing autopep8 changes some pep8 functions. # We reload it to be sure to get the real pep8 functions. reload(pep8) # register the extra checks before using pep8: # any function in this module starting with `_eb_check_` will be used. cands = globals() for check_function in sorted([cands[f] for f in cands if callable(cands[f]) and f.startswith(EB_CHECK)]): _log.debug("Adding custom style check %s", check_function) pep8.register_check(check_function) pep8style = pep8.StyleGuide(quiet=False, config_file=None) options = pep8style.options # we deviate from standard pep8 and allow 120 chars # on a line: the default of 79 is too narrow. options.max_line_length = 120 # we ignore some tests # note that W291 has be replaced by our custom W299 options.ignore = ( 'W291', # replaced by W299 ) options.verbose = int(verbose) result = pep8style.check_files(easyconfigs) if verbose: result.print_statistics() return result.total_errors
def _register_extensions(): """Register all the extensions.""" extensions = util.OrderedSet() extensions.add(('pep8', pep8.__version__)) parser_hooks = [] options_hooks = [] ignored_hooks = [] try: from pkg_resources import iter_entry_points except ImportError: pass else: for entry in iter_entry_points('flake8.extension'): # Do not verify that the requirements versions are valid checker = entry.load(require=False) pep8.register_check(checker, codes=[entry.name]) extensions.add((checker.name, checker.version)) if hasattr(checker, 'add_options'): parser_hooks.append(checker.add_options) if hasattr(checker, 'parse_options'): options_hooks.append(checker.parse_options) if getattr(checker, 'off_by_default', False) is True: ignored_hooks.append(entry.name) return extensions, parser_hooks, options_hooks, ignored_hooks
def main(): parser = argparse.ArgumentParser( description="pep8 Ultimaker style checker") parser.add_argument("paths", type=str, nargs="*", help="List of paths to check (files or directories)", default=["."]) parser.add_argument("--xml", type=str, help="Output JUnit XML file") args = parser.parse_args() pep8.register_check(checkNames) pep8.register_check(blankLines) pep8.register_check(checkStrings) ignore = [] ignore.append("E501") # Ignore line length violations. ignore.append("E226") # Ignore too many leading # in comment block. critical = [] critical.append("E301") # expected 1 blank line, found 0 critical.append("U302") # expected 2 blank lines, found 0 critical.append("E304") # blank lines found after function decorator critical.append("E401") # multiple imports on one line critical.append("E402") # module level import not at top of file critical.append("E713") # test for membership should be ‘not in’ critical.append("W191") # indentation contains tabs critical.append( "U9" ) # Parsing errors (error in this script, or error in code that we're trying to parse) critical.append("U") # All Ultimaker specific stuff. pep8style = pep8.StyleGuide(quiet=False, select=critical, ignore=ignore, show_source=True) if args.xml is not None: pep8style.init_report(XmlReport) for path in args.paths: if path.endswith(".py") and "_pb2.py" not in path: pep8style.paths.append(path) for base_path, _, filenames in os.walk(path): for filename in filenames: if filename.endswith(".py") and "_pb2.py" not in filename: pep8style.paths.append(os.path.join(base_path, filename)) result = pep8style.check_files() print("----------------------------------") result.print_statistics() total = result.get_count() print("Total: %d" % (total)) if args.xml is not None: result.getJUnitXml().write(args.xml, "utf-8", True) return total
def test_register_logical_check(self): def check_dummy(logical_line, tokens): if False: yield pep8.register_check(check_dummy, ['Z401']) self.assertTrue(check_dummy in pep8._checks['logical_line']) codes, args = pep8._checks['logical_line'][check_dummy] self.assertTrue('Z401' in codes) self.assertEqual(args, ['logical_line', 'tokens']) pep8.register_check(check_dummy, []) pep8.register_check(check_dummy, ['Z402', 'Z403']) codes, args = pep8._checks['logical_line'][check_dummy] self.assertEqual(codes, ['Z401', 'Z402', 'Z403']) self.assertEqual(args, ['logical_line', 'tokens']) options = pep8.StyleGuide().options self.assertTrue( any(func == check_dummy for name, func, args in options.logical_checks))
# -*- coding: utf-8 -*- # Copyright (c) 2011 - 2015 Detlev Offenbach <*****@*****.**> # """ Module implementing the code style checker. """ import sys import pep8 from NamingStyleChecker import NamingStyleChecker # register the name checker pep8.register_check(NamingStyleChecker, NamingStyleChecker.Codes) from DocStyleChecker import DocStyleChecker def initService(): """ Initialize the service and return the entry point. @return the entry point for the background client (function) """ return codeStyleCheck class CodeStyleCheckerReport(pep8.BaseReport): """ Class implementing a special report to be used with our dialog.
configured_by = None # This means that we don't have existing config to use. # Make sure pep8's code ignores are fully reset to zero before # adding prospector-flavoured configuration. # pylint: disable=attribute-defined-outside-init self.checker.options.select = () self.checker.options.ignore = tuple( prospector_config.get_disabled_messages('pep8')) if 'max-line-length' in prospector_config.tool_options('pep8'): self.checker.options.max_line_length = \ prospector_config.tool_options('pep8')['max-line-length'] else: configured_by = "Configuration found at %s" % external_config # if we have a command line --max-line-length argument, that # overrules everything max_line_length = prospector_config.max_line_length if max_line_length is not None: self.checker.options.max_line_length = max_line_length return configured_by def run(self, _): report = self.checker.check_files() return report.get_messages() # Load pep8ext_naming into pep8's configuration. register_check(NamingChecker)