def test_styleguide(self): report = pycodestyle.StyleGuide().check_files() self.assertEqual(report.total_errors, 0) self.assertFalse(sys.stdout) self.assertFalse(sys.stderr) self.reset() report = pycodestyle.StyleGuide().check_files(['missing-file']) stdout = sys.stdout.getvalue().splitlines() self.assertEqual(len(stdout), report.total_errors) self.assertEqual(report.total_errors, 1) # < 3.3 returns IOError; >= 3.3 returns FileNotFoundError self.assertTrue(stdout[0].startswith("missing-file:1:1: E902 ")) self.assertFalse(sys.stderr) self.reset() report = pycodestyle.StyleGuide().check_files([E11]) stdout = sys.stdout.getvalue().splitlines() self.assertEqual(len(stdout), report.total_errors) self.assertEqual(report.total_errors, 17) self.assertFalse(sys.stderr) self.reset() # Passing the paths in the constructor gives same result report = pycodestyle.StyleGuide(paths=[E11]).check_files() stdout = sys.stdout.getvalue().splitlines() self.assertEqual(len(stdout), report.total_errors) self.assertEqual(report.total_errors, 17) self.assertFalse(sys.stderr) self.reset()
def test_pycodestyle_conformance(self): """Test that we conform to PEP8.""" dir = os.path.basename(os.getcwd()) dirParent = os.path.dirname(os.getcwd()) files = [f for f in os.listdir(os.getcwd()) if f[-3:] == '.py'] pep8style = pycodestyle.StyleGuide(quiet=False, config_file="pycode.cfg") result = pep8style.check_files(files) self.assertEqual(result.total_errors, 0, "Found code style errors (and warnings).") if dir != os.path.basename(dirParent): return files = [ os.path.join("..", f) for f in os.listdir(dirParent) if f[-3:] == '.py' ] pep8style = pycodestyle.StyleGuide(quiet=False, config_file="pycode.cfg") result = pep8style.check_files(files) print("Error count is {0}".format(result.total_errors)) self.assertEqual(result.total_errors, 0, "Found code style errors (and warnings).")
def test_pycodestyle_conformance(self): """Test that we conform to PEP8.""" dirParent = os.path.dirname(os.getcwd()) files = [f for f in os.listdir(os.getcwd()) if f[-3:] == '.py' and f[:8] != 'torture.'] errors = 0 pep8style = pycodestyle.StyleGuide(quiet=False, config_file="pycode.cfg") result = pep8style.check_files(files) errors += result.total_errors files = [os.path.join(dirParent, f) for f in os.listdir(dirParent) if f[-3:] == '.py'] pep8style = pycodestyle.StyleGuide(quiet=False, config_file="pycode.cfg") result = pep8style.check_files(files) errors += result.total_errors dirParent = os.path.join(dirParent, "xmldiff") files = [os.path.join(dirParent, f) for f in os.listdir(dirParent) if f[-3:] == '.py'] pep8style = pycodestyle.StyleGuide(quiet=False, config_file="pycode.cfg") result = pep8style.check_files(files) errors += result.total_errors self.assertEqual(errors, 0, "Found code style errors (and warnings).")
def test_styleguide_ignore_code(self): def parse_argv(argstring): _saved_argv = sys.argv sys.argv = shlex.split('pycodestyle %s /dev/null' % argstring) try: return pycodestyle.StyleGuide(parse_argv=True) finally: sys.argv = _saved_argv options = parse_argv('').options self.assertEqual(options.select, ()) self.assertEqual( options.ignore, ('E121', 'E123', 'E126', 'E226', 'E24', 'E704', 'W503')) options = parse_argv('--doctest').options self.assertEqual(options.select, ()) self.assertEqual(options.ignore, ()) options = parse_argv('--ignore E,W').options self.assertEqual(options.select, ()) self.assertEqual(options.ignore, ('E', 'W')) options = parse_argv('--select E,W').options self.assertEqual(options.select, ('E', 'W')) self.assertEqual(options.ignore, ('', )) options = parse_argv('--select E --ignore E24').options self.assertEqual(options.select, ('E', )) self.assertEqual(options.ignore, ('', )) options = parse_argv('--ignore E --select E24').options self.assertEqual(options.select, ('E24', )) self.assertEqual(options.ignore, ('', )) options = parse_argv('--ignore W --select E24').options self.assertEqual(options.select, ('E24', )) self.assertEqual(options.ignore, ('', )) pep8style = pycodestyle.StyleGuide(paths=[E11]) self.assertFalse(pep8style.ignore_code('E112')) self.assertFalse(pep8style.ignore_code('W191')) self.assertTrue(pep8style.ignore_code('E241')) pep8style = pycodestyle.StyleGuide(select='E', paths=[E11]) self.assertFalse(pep8style.ignore_code('E112')) self.assertTrue(pep8style.ignore_code('W191')) self.assertFalse(pep8style.ignore_code('E241')) pep8style = pycodestyle.StyleGuide(select='W', paths=[E11]) self.assertTrue(pep8style.ignore_code('E112')) self.assertFalse(pep8style.ignore_code('W191')) self.assertTrue(pep8style.ignore_code('E241')) pep8style = pycodestyle.StyleGuide(select=('F401', ), paths=[E11]) self.assertEqual(pep8style.options.select, ('F401', )) self.assertEqual(pep8style.options.ignore, ('', )) self.assertFalse(pep8style.ignore_code('F')) self.assertFalse(pep8style.ignore_code('F401')) self.assertTrue(pep8style.ignore_code('F402'))
def parse_argv(argstring): _saved_argv = sys.argv sys.argv = shlex.split('pycodestyle %s /dev/null' % argstring) try: return pycodestyle.StyleGuide(parse_argv=True) finally: sys.argv = _saved_argv
def test_code_style(self): """Test that the code in the repository conforms to PEP-8.""" try: import pycodestyle # type: ignore except ImportError: self.skipTest('pycodestyle library not available') else: # Get DAX directory from dax import __dax_dir__ as dax_dir # Get a path to the configuration file config_file = os.path.join(os.path.dirname(__file__), os.pardir, 'setup.cfg') if not os.path.isfile(config_file): self.skipTest('Could not find config file') # Create a style object using the config file style = pycodestyle.StyleGuide(config_file=config_file) # Buffer to store stdout output buf = io.StringIO() with contextlib.redirect_stdout(buf): # Check all files result = style.check_files([dax_dir]) # Format message and assert msg = f'\n\nCode style report:\n{buf.getvalue()}' self.assertEqual(result.total_errors, 0, msg)
def style(args): """Check for PEP8 compliance with the pycodestyle tool. This implements conventions lupHw1 and u1wSS9. The enforced maximum line length follows convention TZb0Uv. When all project Python files are compliant, this function returns None. When a non-compliant file is found, details about the non-compliance are printed on the standard output stream and this function returns 1. Runtime errors encountered by the style checker are printed on the standard error stream and raised as the appropriate exceptions. """ excludes = ['external_tools', 'pystache', 'tools', 'ply'] + args.excludes exclude_patterns = ','.join(excludes) options = [ '--exclude=' + exclude_patterns, '--max-line-length', '118', os.path.join(args.topdir, ".") ] logging.info('code-style check: ' + ' '.join(options)) style = pycodestyle.StyleGuide(arglist=options) if args.teamcity: style.init_report(_TeamcityReport) report = style.check_files() if report.total_errors: logging.error('Python code-style check found non-compliant files' ) # details on stdout return 1
def pycodestyle(line, cell): """pycodestyle cell magic for pep8""" logger.setLevel(logging.INFO) # output is written to stdout # remember and replace old_stdout = sys.stdout # temporary replace sys.stdout = io.StringIO() # store code in a file, todo unicode with tempfile.NamedTemporaryFile(mode='r+', delete=False) as f: # save to file f.write('# The %%pycodestyle cell magic was here\n' + cell + '\n') # make sure it's written f.flush() f.close() # now we can check the file by name. # we might be able to use 'stdin', have to check implementation format = '%(row)d:%(col)d: %(code)s %(text)s' pycodestyle = pycodestyle_module.StyleGuide(format=format) # check the filename pcs_result = pycodestyle.check_files(paths=[f.name]) # split lines stdout = sys.stdout.getvalue().splitlines() for line in stdout: logger.info(line) # restore sys.stdout = old_stdout try: os.remove(f.name) except OSError as e: ## if failed, report it back to the user ## logger.error("Error: %s - %s." % (e.filename, e.strerror)) return
def pylsp_lint(workspace, document): config = workspace._config settings = config.plugin_settings('pycodestyle', document_path=document.path) log.debug("Got pycodestyle settings: %s", settings) opts = { 'exclude': settings.get('exclude'), 'filename': settings.get('filename'), 'hang_closing': settings.get('hangClosing'), 'ignore': settings.get('ignore'), 'max_line_length': settings.get('maxLineLength'), 'indent_size': settings.get('indentSize'), 'select': settings.get('select'), } kwargs = {k: v for k, v in opts.items() if v} styleguide = pycodestyle.StyleGuide(kwargs) c = pycodestyle.Checker(filename=document.uri, lines=document.lines, options=styleguide.options, report=PyCodeStyleDiagnosticReport( styleguide.options)) c.check_all() diagnostics = c.report.diagnostics return diagnostics
def test_pep8_conformance(self): pep8style = pycodestyle.StyleGuide(quiet=False) pep8style.options.ignore = pep8style.options.ignore + tuple(['E501']) pep8style.input_dir('fuzzywuzzy') result = pep8style.check_files() self.assertEqual(result.total_errors, 0, "PEP8 POLICE - WOOOOOWOOOOOOOOOO")
def _check_spacing(all_files): """This function checks the number of blank lines above each class, function and method defintion. It also checks for whitespace after ',', ';' and ':'. """ print 'Starting spacing checks' print '----------------------------------------' print '' pycodestyle_config_path = os.path.join(os.getcwd(), 'tox.ini') summary_messages = [] # Select only Python files to check for errors files_to_check = [ filename for filename in all_files if not any( fnmatch.fnmatch(filename, pattern) for pattern in EXCLUDED_PATHS) and filename.endswith('.py') ] style_guide = pycodestyle.StyleGuide(config_file=pycodestyle_config_path) report = style_guide.check_files(files_to_check) report.print_statistics() print '----------------------------------------' print '' if report.get_count() != 0: summary_message = ('%s Spacing checks failed' % _MESSAGE_TYPE_FAILED) print summary_message summary_messages.append(summary_message) else: summary_message = ('%s Spacing checks passed' % _MESSAGE_TYPE_SUCCESS) print summary_message summary_messages.append(summary_message) print '' return summary_messages
def test_pep8_conformance(cookies): """Test that we conform to PEP-8.""" check_paths = [ 'my_python_project', 'tests', ] exclude_paths = [] project = cookies.bake( extra_context={'project_short_description': 'short description'}) print("PEP8 check of directories: {}\n".format(', '.join(check_paths))) # Get paths wrt package root package_root = str(project.project) for paths in (check_paths, exclude_paths): for i, path in enumerate(paths): paths[i] = os.path.join(package_root, path) style = pycodestyle.StyleGuide() style.options.exclude.extend(exclude_paths) success = style.check_files(check_paths).total_errors == 0 assert success, "The generated code does not conform to PEP8"
def pycodestyle_check(filepath, args): _fmt = "%(row)d:%(col)d: %(code)s %(text)s" options = {'ignore': args.pycodestyle_ignore, "show_source": True} pycodestyle_checker = pycodestyle.StyleGuide(options) fchecker = pycodestyle_checker.checker_class( filepath, options=pycodestyle_checker.options) fchecker.check_all() if fchecker.report.file_errors > 0: result_message_list = [] result_message_list.append("* PYCODESTYLE: [FAILED]: " + filepath) fchecker.report.print_statistics() for line_number, offset, code, text, doc in fchecker.report._deferred_print: result_message_list.append( _fmt % { 'path': filepath, 'row': fchecker.report.line_offset + line_number, 'col': offset + 1, 'code': code, 'text': text, }) publish_result(result_message_list, args) args.failed_message_list = args.failed_message_list + result_message_list return 1 else: if args.verbose: message = "* PYCODESTYLE: [PASSED]: " + filepath print_stderr(message) return 0
def run_pep8(request_data): """ Worker that run the pep8 tool on the current editor text. :returns a list of tuples (msg, msg_type, line_number) """ import pycodestyle from pyqode.python.backend.pep8utils import CustomChecker WARNING = 1 code = request_data['code'] path = request_data['path'] max_line_length = request_data['max_line_length'] ignore_rules = request_data['ignore_rules'] ignore_rules += ['W291', 'W292', 'W293', 'W391'] pycodestyle.MAX_LINE_LENGTH = max_line_length # setup our custom style guide with our custom checker which returns a list # of strings instread of spitting the results at stdout pep8style = pycodestyle.StyleGuide(parse_argv=False, config_file='', checker_class=CustomChecker) try: results = pep8style.input_file(path, lines=code.splitlines(True)) except Exception: _logger().exception('Failed to run PEP8 analysis with data=%r' % request_data) return [] else: messages = [] for line_number, offset, code, text, doc in results: if code in ignore_rules: continue messages.append( ('[PEP8] %s: %s' % (code, text), WARNING, line_number - 1)) return messages
def standard_compliance(project_path: str) -> float: """ Get the average of the standard compliance density for every file. Count the style offences of all python files and normalize it using the files size. Then calculate the average for all files. :param project_path: Full path to the project to be measured. :returns: Average standard compliance of all files """ _python_files = get_python_files(project_path) _style_checker = pycodestyle.StyleGuide(quiet=True) _style_results = _style_checker.check_files(_python_files) _lines = _style_results.counters['physical lines'] _style_errors = 0 for key, value in _style_results.counters.items(): if key[:1] in ['E', 'W']: _style_errors = _style_errors + value if _lines > 0: return 1 - float(_style_errors) / float(_lines) return 0
def test_pep8_conformance(self): """Test that we conform to PEP-8.""" style_guide = pycodestyle.StyleGuide() style_check_report = style_guide.check_files( discover_all_python_files()) self.assertEqual(style_check_report.total_errors, 0, "Found code style errors (and warnings).")
def test_conformance(self): style = pycodestyle.StyleGuide() test_dir = os.path.dirname(os.path.abspath(__file__)) src_dir = os.path.join(test_dir, '../src/blameandshame') result = style.check_files([src_dir]) self.assertEqual(result.total_errors, 0, "Found code style errors and/or warnings.")
def test_pycode_style_db(self): """ conforms to pycodestyle Style """ pycodestyle_test = pycodestyle.StyleGuide(quiet=True) errors = pycodestyle_test.check_files(['web_app/app.py']) self.assertEqual(errors.total_errors, 0, errors.messages)
def test_pep8_compliance(self): pep8_check = pycodestyle.StyleGuide() # Directories to skip in the recursive walk of the directory: skip_dirs = ('__pycache__', ) # These are pycodestyle errors and warnings to explicitly ignore. For # descriptions for each code see: # https://pep8.readthedocs.io/en/latest/intro.html#error-codes pep8_check.options.ignore += ( # ignored because... 'E402', # ...justified lower module imports in __init__ and units 'E501', # ...docstring examples include output lines >79 chars 'E722', # ...several "bare except" cases need to be addressed ) # Find all Python source code ('.py') files in the 'cfunits' directory, # including all unskipped sub-directories within e.g. test directory: python_files = [] for root_dir, dirs, filelist in os.walk('..'): # '..' == 'cfunits/' if os.path.basename(root_dir) in skip_dirs: continue python_files += [ os.path.join(root_dir, fname) for fname in filelist if fname.endswith('.py') ] pep8_issues = pep8_check.check_files(python_files).total_errors self.assertEqual( pep8_issues, 0, 'Detected {!s} PEP8 errors or warnings:'.format(pep8_issues))
def pep8_check(self, report, filename): """ Run pep8 checks on given filename, adding pep8 reports to report. """ # Try to use pycodestyle, and fallback on pep8 if not available. If # nothing is available, don't do anything. try: try: import pycodestyle as pep8 except ImportError: import pep8 except ImportError: return class CustomReport(pep8.BaseReport): def error(self, line_number, offset, text, check): # Due to the great architecture of PEP8/pycodestyle, we have to # duplicate this check here in order to not show certain (but # not all) errors that should be ignored. code = text[:4] if self._ignore_code(code): return report.add(text, filename, line_number, offset) sg = pep8.StyleGuide( quiet=True, ignore=["W503", "E121", "E123", "E126", "E226", "E24", "E704", "E402", "E721", "W504", "E741"] ) sg.init_report(CustomReport) sg.check_files([filename])
def run(self): import pylint.lint import pycodestyle files = (["bugzilla-cli", "bugzilla", "setup.py"] + glob.glob("examples/*.py") + glob.glob("tests/*.py")) output_format = sys.stdout.isatty() and "colorized" or "text" print("running pycodestyle") style_guide = pycodestyle.StyleGuide( config_file='tox.ini', format="pylint", paths=files, ) report = style_guide.check_files() if style_guide.options.count: sys.stderr.write(str(report.total_errors) + '\n') print("running pylint") pylint_opts = [ "--rcfile", ".pylintrc", "--output-format=%s" % output_format, ] pylint.lint.Run(files + pylint_opts)
def run(self): import pylint.lint import pycodestyle files = [ "setup.py", "virt-install", "virt-clone", "virt-convert", "virt-xml", "virt-manager", "virtcli", "virtinst", "virtconv", "virtManager", "tests" ] output_format = sys.stdout.isatty() and "colorized" or "text" print("running pycodestyle") style_guide = pycodestyle.StyleGuide(config_file='setup.cfg', format="pylint", paths=files) report = style_guide.check_files() if style_guide.options.count: sys.stderr.write(str(report.total_errors) + '\n') print("running pylint") pylint_opts = [ "--rcfile", "pylintrc", "--output-format=%s" % output_format, ] if self.jobs: pylint_opts += ["--jobs=%d" % self.jobs] pylint.lint.Run(files + pylint_opts)
def test_pep8(self): style = pycodestyle.StyleGuide() files = glob('aioipfs_api/**/*.py', recursive=True) files = [f for f in files if 'autoapi.py' not in f] result = style.check_files(files) if result.total_errors > 0: raise LintError("Code style errors found.")
def run(self): class PyCodeStyleReporter(pycodestyle.BaseReport): """ Custom reporter, nested as parent class will not have been imported if pycodestyle/pep8 wasn't available """ def __init__(self, *args): super().__init__(*args) self.diagnostics = [] def error(self, line_number, offset, text, check): errorcode = super().error(line_number, offset, text, check) if errorcode: loc = types.SourceLocation(line=line_number, column=offset + 1) severity = types.Diagnostic.Severity.INFO self.diagnostics.append( types.Diagnostic(severity=severity, locations=[loc.to_range()], message=text)) style_guide = pycodestyle.StyleGuide(reporter=PyCodeStyleReporter) reporter = style_guide.options.report style_guide.input_file(self.path, lines=self.source.splitlines(True)) return reporter.diagnostics
def test_pep8_conformance(self): """Test if files follow PEP8 style guide.""" paths = ['./setup.py', './pxpaypy/', './tests/'] styleguide = pycodestyle.StyleGuide() self.assertEqual(styleguide.check_files(paths).total_errors, 0)
def test_pep8_conformance(self): # Tests the hazimp code base against the "pycodestyle" tool. # # Users can add their own excluded files (should files exist in the # local directory which is not in the repository) by adding a # ".pep8_test_exclude.txt" file in the same directory as this test. # The file should be a line separated list of filenames/directories # as can be passed to the "pep8" tool's exclude list. pep8style = pycodestyle.StyleGuide(quiet=False) pep8style.options.exclude.extend(['*/_version.py']) # Allow users to add their own exclude list. extra_exclude_file = os.path.join(os.path.dirname(__file__), '.pep8_test_exclude.txt') if os.path.exists(extra_exclude_file): with open(extra_exclude_file, 'r') as fhandle: extra_exclude = [line.strip() for line in fhandle if line.strip()] pep8style.options.exclude.extend(extra_exclude) root = os.path.abspath(hazimp.__file__) result = pep8style.check_files([os.path.dirname(root)]) self.assertEqual(result.total_errors, 0, "Found code syntax errors (and warnings).")
def metatest_pep8(self, path): """ PEP8 conformance for {relpath} """ # We ignore these rules, because they're too dumb to follow # E501 - max line length # E265 - block comments # E402 - module level imports ignore_codes = ('E265', 'E501', 'E402') if pycodestyle is not None: pep8style = pycodestyle.StyleGuide() elif pep8 is not None: pep8style = pep8.StyleGuide() else: assert False, 'Install pycodestyle/pep8' pep8style.options.ignore += ignore_codes # pep8style.options.max_line_length = 100 result = pep8style.check_files([path]) total_errors = result.total_errors err = 'Found {} PEP8 style violations in: {}' err = err.format(total_errors, path) self.assertEqual(total_errors, 0, err)
def run(self): """ Call pycodestyle and pylint here. """ import pylint.lint import pycodestyle files = ["setup.py", "src/virtBootstrap/", "tests/"] output_format = "colorized" if sys.stdout.isatty() else "text" print(">>> Running pycodestyle ...") style_guide = pycodestyle.StyleGuide(paths=files) report = style_guide.check_files() if style_guide.options.count: sys.stderr.write(str(report.total_errors) + '\n') print(">>> Running pylint ...") pylint_opts = [ "--rcfile", "pylintrc", "--output-format=%s" % output_format ] if self.errors_only: pylint_opts.append("-E") pylint.lint.Run(files + pylint_opts)
def run(self): import pylint.lint import pycodestyle files = ["setup.py", "virt-install", "virt-clone", "virt-convert", "virt-xml", "virt-manager", "virtcli", "virtinst", "virtconv", "virtManager", "tests"] output_format = sys.stdout.isatty() and "colorized" or "text" exclude = ["virtinst/progress.py"] print("running pycodestyle") style_guide = pycodestyle.StyleGuide( config_file='tests/pycodestyle.cfg', paths=files ) style_guide.options.exclude = pycodestyle.normalize_paths( ','.join(exclude) ) report = style_guide.check_files() if style_guide.options.count: sys.stderr.write(str(report.total_errors) + '\n') print("running pylint") pylint_opts = [ "--rcfile", "tests/pylint.cfg", "--output-format=%s" % output_format, ] + ["--ignore"] + [os.path.basename(p) for p in exclude] if self.jobs: pylint_opts += ["--jobs=%d" % self.jobs] pylint.lint.Run(files + pylint_opts)
def test_styleguide_options(self): # Instantiate a simple checker pep8style = pycodestyle.StyleGuide(paths=[E11]) # Check style's attributes self.assertEqual(pep8style.checker_class, pycodestyle.Checker) self.assertEqual(pep8style.paths, [E11]) self.assertEqual(pep8style.runner, pep8style.input_file) self.assertEqual(pep8style.options.ignore_code, pep8style.ignore_code) self.assertEqual(pep8style.options.paths, pep8style.paths) # Check unset options for o in ('benchmark', 'config', 'count', 'diff', 'doctest', 'quiet', 'show_pep8', 'show_source', 'statistics', 'testsuite', 'verbose'): oval = getattr(pep8style.options, o) self.assertTrue(oval in (None, False), msg='%s = %r' % (o, oval)) # Check default options self.assertTrue(pep8style.options.repeat) self.assertEqual( pep8style.options.benchmark_keys, ['directories', 'files', 'logical lines', 'physical lines']) self.assertEqual( pep8style.options.exclude, ['.svn', 'CVS', '.bzr', '.hg', '.git', '__pycache__', '.tox']) self.assertEqual(pep8style.options.filename, ['*.py']) self.assertEqual(pep8style.options.format, 'default') self.assertEqual(pep8style.options.select, ()) self.assertEqual(pep8style.options.ignore, ('E226', 'E24')) self.assertEqual(pep8style.options.max_line_length, 79)