Python pep8.StyleGuide() Examples

The following are 30 code examples of pep8.StyleGuide(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pep8 , or try the search function .
Example #1
Source File: test_pep8.py    From hydrus with MIT License 8 votes vote down vote up
def test_pep8(self):
        """Test method to check PEP8 compliance over the entire project."""
        self.file_structure = dirname(dirname(abspath(__file__)))
        print("Testing for PEP8 compliance of python files in {}".format(
            self.file_structure))
        style = pep8.StyleGuide()
        style.options.max_line_length = 100  # Set this to desired maximum line length
        filenames = []
        # Set this to desired folder location
        for root, _, files in os.walk(self.file_structure):
            python_files = [f for f in files if f.endswith(
                '.py') and "examples" not in root]
            for file in python_files:
                if len(root.split('samples')) != 2:     # Ignore samples directory
                    filename = '{0}/{1}'.format(root, file)
                    filenames.append(filename)
        check = style.check_files(filenames)
        self.assertEqual(check.total_errors, 0, 'PEP8 style errors: %d' %
                         check.total_errors) 
Example #2
Source File: code_helpers.py    From fiasko_bro with MIT License 6 votes vote down vote up
def count_pep8_violations(repository_info, max_line_length=79, path_whitelist=None):
    path_whitelist = path_whitelist or []
    pep8style = pep8.StyleGuide(
        paths=['--max-line-length', str(max_line_length)],
        quiet=True
    )
    python_file_paths = [parsed_file.path for parsed_file in repository_info.get_parsed_py_files()]
    validatable_paths = []
    for python_file_path in python_file_paths:
        for whitelisted_path_part in path_whitelist:
            if whitelisted_path_part in python_file_path:
                break
        else:
            validatable_paths.append(python_file_path)
    result = pep8style.check_files(validatable_paths)
    return result.total_errors 
Example #3
Source File: test_style.py    From CGATPipelines with MIT License 6 votes vote down vote up
def check_style(filename):
    '''check style of filename.
    '''

    p = pep8.StyleGuide(quiet=True)
    report = p.check_files([filename])

    # count errors/warning excluding
    # those to ignore
    take = [y for x, y
            in list(report.counters.items()) if x not in IGNORE]
    found = ['%s:%i' % (x, y) for x, y
             in list(report.counters.items()) if x not in IGNORE]
    total = sum(take)
    ok_(total == 0,
        'pep8 style violations: %s' % ','.join(found)) 
Example #4
Source File: test_style.py    From UMI-tools with MIT License 6 votes vote down vote up
def check_style(filename):
    '''check style of filename.
    '''

    p = pep8.StyleGuide(quiet=True)
    report = p.check_files([filename])

    # count errors/warning excluding
    # those to ignore
    take = [y for x, y
            in report.counters.items() if x not in IGNORE]
    found = ['%s:%i' % (x, y) for x, y
             in report.counters.items() if x not in IGNORE]
    total = sum(take)
    ok_(total == 0,
        'pep8 style violations in %s: %s' % (filename, ','.join(found))) 
Example #5
Source File: test_pep8.py    From dymos with Apache License 2.0 6 votes vote down vote up
def test_pep8(self):
        """ Tests that all files in this, directory, the parent directory, and test
        sub-directories are PEP8 compliant.

        Notes
        -----
        max_line_length has been set to 100 for this test.
        """
        dymos_path = os.path.split(dymos.__file__)[0]
        pyfiles = _discover_python_files(dymos_path)

        style = pep8.StyleGuide(ignore=['E201', 'E226', 'E241', 'E402'])
        style.options.max_line_length = 120

        save = sys.stdout
        sys.stdout = msg = StringIO()
        try:
            report = style.check_files(pyfiles)
        finally:
            sys.stdout = save

        if report.total_errors > 0:
            self.fail("Found pep8 errors:\n%s" % msg.getvalue()) 
Example #6
Source File: pavement.py    From qgis-geoserver-plugin with GNU General Public License v2.0 6 votes vote down vote up
def pep8(args):
    """Check code for PEP8 violations"""
    try:
        import pep8
    except:
        error('pep8 not found! Run "paver install_devtools".')
        sys.exit(1)

    # Errors to ignore
    ignore = ['E203', 'E121', 'E122', 'E123', 'E124', 'E125', 'E126', 'E127',
        'E128', 'E402']
    styleguide = pep8.StyleGuide(ignore=ignore,
                                 exclude=['*/extlibs/*', '*/ext-src/*'],
                                 repeat=True, max_line_length=79,
                                 parse_argv=args)
    styleguide.input_dir(options.plugin.source_dir)
    info('===== PEP8 SUMMARY =====')
    styleguide.options.report.print_statistics() 
Example #7
Source File: test_pep8.py    From nc-time-axis with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_pep8_conformance(self):
        # Tests the nc-time-axis code base against the "pep8" 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 = pep8.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(nc_time_axis.__file__)
        result = pep8style.check_files([os.path.dirname(root)])
        self.assertEqual(result.total_errors, 0, "Found code syntax "
                                                 "errors (and warnings).") 
Example #8
Source File: test_coding_standards.py    From emva1288 with GNU General Public License v3.0 6 votes vote down vote up
def test_pep8_conformance():

    dirs = []
    dirname = os.path.dirname(emva1288.__file__)
    dirs.append(dirname)
    examplesdir = os.path.join(dirname, '..', 'examples')
    examplesdir = os.path.abspath(examplesdir)
    dirs.append(examplesdir)

    pep8style = pep8.StyleGuide()

    # Extend the number of PEP8 guidelines which are not checked.
    pep8style.options.ignore = (pep8style.options.ignore +
                                tuple(PEP8_ADDITIONAL_IGNORE))
    pep8style.options.exclude.extend(EXCLUDE_FILES)

    result = pep8style.check_files(dirs)
    msg = "Found code syntax errors (and warnings)."
    assert_equal(result.total_errors, 0, msg) 
Example #9
Source File: tests.py    From tbvaccine with MIT License 5 votes vote down vote up
def test_pep8(self):
        pep8style = pep8.StyleGuide(
            [
                ["statistics", True],
                ["show-sources", True],
                ["repeat", True],
                ["ignore", "E501"],
                ["paths", [os.path.dirname(os.path.abspath(__file__))]],
            ],
            parse_argv=False,
        )
        report = pep8style.check_files()
        assert report.total_errors == 0 
Example #10
Source File: analyzer.py    From checkmate with MIT License 5 votes vote down vote up
def analyze(self,file_revision):
        pep8style = pep8.StyleGuide(quiet = True)
        try:
            handle,temp_filename = tempfile.mkstemp()
            fh = os.fdopen(handle,"wb")
            fh.write(file_revision.get_file_content())
            fh.close()
            pep8style.init_report(Reporter)
            result = pep8style.check_files([temp_filename])
        finally:
            os.unlink(temp_filename)
        #we add the fingerprints...
        for issue in result.issues:
            issue['fingerprint'] = self.get_fingerprint_from_code(file_revision, issue['location'], extra_data=issue['data'])
        return {'issues' : result.issues} 
Example #11
Source File: pep8_view_test_mixin.py    From DjanGoat with MIT License 5 votes vote down vote up
def test_pep8_conformance_init(self):
        file_path = self.path + '__init__.py'
        pep8style = pep8.StyleGuide(quiet=True)
        result = pep8style.check_files([file_path])
        self._validate(result, file_path)

    # Helper method to validate whether or not there are pep8 errors in the code specified by the path 
Example #12
Source File: pep8_view_test_mixin.py    From DjanGoat with MIT License 5 votes vote down vote up
def test_pep8_conformance_urls(self):
        file_path = self.path + 'urls.py'
        pep8style = pep8.StyleGuide(quiet=True)
        result = pep8style.check_files([file_path])
        self._validate(result, file_path) 
Example #13
Source File: pep8_view_test_mixin.py    From DjanGoat with MIT License 5 votes vote down vote up
def test_pep8_conformance_views(self):
        file_path = self.path + 'views.py'
        pep8style = pep8.StyleGuide(quiet=True)
        result = pep8style.check_files([file_path])
        self._validate(result, file_path) 
Example #14
Source File: pep8_model_test_mixin.py    From DjanGoat with MIT License 5 votes vote down vote up
def test_pep8_conformance_views(self):
        pep8style = pep8.StyleGuide(quiet=True)
        result = pep8style.check_files([self.path])

        error_message = ""
        if result.total_errors != 0:
            error_message = "Style errors in: " + self.path + "\n" + "\n".join(result.get_statistics())

        self.assertEqual(result.total_errors, 0, error_message) 
Example #15
Source File: test_pep8.py    From django-seo-js with MIT License 5 votes vote down vote up
def test_pep8_conformance(self):
        """Test that we conform to PEP8."""
        ALL_FILES = []
        IGNORED_CODES = ["E501", "E226", "E302", "E41", "E128", "E127", ]

        project_root = os.path.abspath(os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            "..",
            "..",
        ))
        ignored_folders = [
            ".git",
            "venv",
            ".tox",
        ]

        pep8style = pep8.StyleGuide(
            # quiet=True,
            report=pep8.StandardReport,
            show_source=True,
            max_line_length=120,
        )
        for code in IGNORED_CODES:
            pep8style.ignore_code(code)

        for root, dirnames, filenames in os.walk(project_root):
            if not any(folder in root for folder in ignored_folders):
                for filename in filenames:
                    if filename[-3:] == ".py":
                        ALL_FILES.append(os.path.join(root, filename))
        result = pep8style.check_files(ALL_FILES)

        self.assertEqual(result.total_errors, 0, "Found %s code style errors (and warnings)." % (result.total_errors,)) 
Example #16
Source File: test_jsane.py    From jsane with MIT License 5 votes vote down vote up
def test_pep8(self):
        pep8style = pep8.StyleGuide([['statistics', True],
                                     ['show-sources', True],
                                     ['repeat', True],
                                     ['ignore', "E501"],
                                     ['paths', [os.path.dirname(
                                         os.path.abspath(__file__))]]],
                                    parse_argv=False)
        report = pep8style.check_files()
        assert report.total_errors == 0 
Example #17
Source File: test_pep8.py    From linux_thermaltake_riing with GNU General Public License v2.0 5 votes vote down vote up
def test_pep8(self):
        """
        verify our codebase complies with code style guidelines
        """
        style = pep8.StyleGuide(quiet=False)
        # accepted pep8 guideline deviances
        style.options.max_line_length = 122  # generally accepted limit
        style.options.ignore = ('W503', 'E402')  # operator at start of line

        errors = 0
        # check main project directory
        for root, _not_used, files in os.walk(os.path.join(os.getcwd(), 'linux_thermaltake_riing')):
            if not isinstance(files, list):
                files = [files]
            for f in files:
                if not str(f).endswith('.py'):
                    continue
                file_path = ['{}/{}'.format(root, f)]
                result = style.check_files(file_path)  # type: pep8.BaseReport
                errors = result.total_errors

        # check tests directory
        for root, _not_used, files in os.walk(os.path.join(os.getcwd(), 'tests')):
            if not isinstance(files, list):
                files = [files]
            for f in files:
                if not str(f).endswith('.py'):
                    continue
                file_path = ['{}/{}'.format(root, f)]
                result = style.check_files(file_path)  # type: pep8.BaseReport
                errors = result.total_errors

        self.assertEqual(0, errors, 'PEP8 style errors: {}'.format(errors)) 
Example #18
Source File: test_pep8.py    From hier_config with MIT License 5 votes vote down vote up
def test_pep8(self):
        style = pep8.StyleGuide()
        style.options.ignore += ('E501',)
        errors = 0
        python_files = set()

        for root, dirs, files in os.walk('hier_config'):
            for f in files:
                if os.path.isfile(os.path.join(root, f)):
                    if f.endswith('.py'):
                        python_files.add(os.path.join(root, f))

        errors += style.check_files(python_files).total_errors

        self.assertEqual(errors, 0, 'PEP8 style errors: {}'.format(errors)) 
Example #19
Source File: test_pep8.py    From urdf2webots with Apache License 2.0 5 votes vote down vote up
def test_pep8_conformance(self):
        """Test that the tests are PEP8 compliant."""
        checker = pep8.StyleGuide(
            quiet=True,
            paths=glob.glob(packageDirectory + os.sep + '*.py') + glob.glob(packageDirectory + os.sep + 'tests' + os.sep + '*.py'),
            reporter=CustomReport
        )
        checker.options.ignore = ('E501')  # E501: line too long (> 80 characters)
        report = checker.check_files()
        self.assertEqual(
            report.total_errors, 0,
            msg='PEP8 errors:\n%s' % (report)
        ) 
Example #20
Source File: workers.py    From Turing with MIT License 5 votes vote down vote up
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 pep8
    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']
    pep8.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 = pep8.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 
Example #21
Source File: test_coding_standards.py    From pyoptools with GNU General Public License v3.0 5 votes vote down vote up
def test_pep8_conformance():

    dirname = os.path.dirname(pyoptools.__file__)
    pep8style = pep8.StyleGuide()

    # Extend the number of PEP8 guidelines which are not checked.
    pep8style.options.ignore = (pep8style.options.ignore +
                                tuple(PEP8_ADDITIONAL_IGNORE))
    pep8style.options.exclude.extend(EXCLUDE_FILES)
    pep8style.options.filename = ['*.py', '*.pyx']

    result = pep8style.check_files([dirname])
    msg = "Found code syntax errors (and warnings)."
    assert result.total_errors == 0, msg 
Example #22
Source File: pep8_magic.py    From cube-in-a-box with MIT License 5 votes vote down vote up
def pep8(line, cell):
    """pep8 cell magic"""

    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='w') as f:
        # save to file
        f.write(cell + '\n')
        # make sure it's written
        f.flush()
        # 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'
        pep8style = pep8_module.StyleGuide(format=format)
        # check the filename
        # reusing the file is not allowed under windows
        pep8style.check_files(paths=[f.name])
        # split lines
        stdout = sys.stdout.getvalue().splitlines()
    for line in stdout:
        logger.info(line)
    # restore
    sys.stdout = old_stdout
    return 
Example #23
Source File: test_pep8.py    From ros_buildfarm with Apache License 2.0 5 votes vote down vote up
def test_pep8_conformance():
    """Test source code for PEP8 conformance."""
    pep8style = StyleGuide(max_line_length=100)
    report = pep8style.options.report
    report.start()
    base_path = os.path.join(os.path.dirname(__file__), '..')
    pep8style.input_dir(os.path.join(base_path, 'ros_buildfarm'))
    pep8style.input_dir(os.path.join(base_path, 'scripts'))
    report.stop()
    assert report.total_errors == 0, \
        'Found %d code style errors (and warnings)' % report.total_errors 
Example #24
Source File: test_pep8.py    From isofit with Apache License 2.0 5 votes vote down vote up
def test_pep8_conformance():
    """Test that we conform to PEP8."""

    config_file = 'data/pep8_config.txt'
    files = []
    for fi in glob('../*.py'):
        files.append(fi)

    # After acceptance, this will be uncommented
    pep8style = pep8.StyleGuide(config_file=config_file, quiet=False)
    result = pep8style.check_files(files)
    if result.total_errors != 0:
        print("Found PEP8 conformance error.")
        print("Please fix your style with autopep8.")
    assert result.total_errors == 0 
Example #25
Source File: analyze.py    From blackmamba with MIT License 5 votes vote down vote up
def _pep8_annotations(text, ignore=None, max_line_length=None):
    import pep8

    class _Pep8AnnotationReport(pep8.BaseReport):
        def __init__(self, options):
            super().__init__(options)
            self.annotations = []

        def error(self, line_number, offset, text, check):
            # If super doesn't return code, this one is ignored
            if not super().error(line_number, offset, text, check):
                return

            annotation = _AnalyzerAnnotation(self.line_offset + line_number, text, _Source.pep8, Style.warning)
            self.annotations.append(annotation)

    # pep8 requires you to include \n at the end of lines
    lines = text.splitlines(True)

    style_guide = pep8.StyleGuide(reporter=_Pep8AnnotationReport, )
    options = style_guide.options

    if ignore:
        options.ignore = tuple(ignore)
    else:
        options.ignore = tuple()

    if max_line_length:
        options.max_line_length = max_line_length

    checker = pep8.Checker(None, lines, options, None)
    checker.check_all()

    return checker.report.annotations


#
# pyflakes
# 
Example #26
Source File: test_pycodestyle.py    From uctf with Apache License 2.0 5 votes vote down vote up
def test_pycodestyle():
    style = pycodestyle.StyleGuide()
    base_path = os.path.dirname(os.path.dirname(__file__))
    report = style.check_files([
        os.path.join(base_path, 'script', 'control_team_blue'),
        os.path.join(base_path, 'script', 'control_team_gold'),
        os.path.join(base_path, 'script', 'rqt_uctf'),
        os.path.join(base_path, 'script', 'spawn_blue'),
        os.path.join(base_path, 'script', 'spawn_gold'),
        os.path.join(base_path, 'src'),
        os.path.join(base_path, 'test'),
    ])
    assert not report.total_errors 
Example #27
Source File: test_style.py    From datacats with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_pep8_conformance(self):
        """
        Tests pep8 conformence.
        """
        pep8style = pep8.StyleGuide(quiet=False, ignore=IGNORE_PEP8, max_line_length=100)
        result = pep8style.check_files(['datacats'])
        self.assertEqual(result.total_errors, 0, 'Found code style errors.') 
Example #28
Source File: pylint_tests.py    From mysql-utilities with GNU General Public License v2.0 5 votes vote down vote up
def process_items(reporter, items, tester):
    """Process list of modules or packages.
    """
    test_pylint = tester in ("pylint", "all",)
    test_pep8 = tester in ("pep8", "all",)

    if test_pep8:
        # PEP8 report instance setup
        pep8style = StyleGuide(parse_argv=False, config_file=False)
        if reporter.name == "csv":
            pep8style.options.report = CsvPep8Report(pep8style.options,
                                                     reporter.writer)
        else:
            colorized = (reporter.name == "colorized")
            pep8style.options.report = Pep8Report(pep8style.options,
                                                  reporter.line_format,
                                                  reporter.out,
                                                  colorized)

    pylint_rc_path = os.path.join(_CURRENT_PATH, "pylint.rc")
    for item in items:
        path = os.path.join(_BASE_PATH, item)
        if test_pylint:
            # Pylint tests
            lint.Run([path, "--rcfile={0}".format(pylint_rc_path)],
                     reporter=reporter, exit=False)
        if test_pep8:
            # Pep8 tests
            if item.endswith(".py"):
                pep8style.input_file(path)
            else:
                pep8style.input_dir(path) 
Example #29
Source File: test_pep8.py    From pySDC with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def check_files(dir):
    style = pep8.StyleGuide()
    style.options.max_line_length = 120
    style.options.ignore = 'E402'
    python_files = []
    for root, _, files in os.walk(dir):
        python_files += [os.path.join(root, f) for f in files if f.endswith('.py')]

    for file in python_files:
        report = style.check_files([os.path.join(BASE_PATH, file)])
        report.print_statistics()
        nose.tools.assert_equal(report.total_errors, 0, "File %s has some PEP8 errors: %d" % (file, report.total_errors)) 
Example #30
Source File: test_coding_standards.py    From neural-network-animation with MIT License 4 votes vote down vote up
def assert_pep8_conformance(module=matplotlib, exclude_files=EXCLUDE_FILES,
                            extra_exclude_file=EXTRA_EXCLUDE_FILE,
                            pep8_additional_ignore=PEP8_ADDITIONAL_IGNORE):
    """
    Tests the matplotlib codebase against the "pep8" 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.
    """

    if not HAS_PEP8:
        raise SkipTest('The pep8 tool is required for this test')

    # to get a list of bad files, rather than the specific errors, add
    # "reporter=pep8.FileReport" to the StyleGuide constructor.
    pep8style = pep8.StyleGuide(quiet=False,
                                reporter=StandardReportWithExclusions)
    reporter = pep8style.options.reporter

    # Extend the number of PEP8 guidelines which are not checked.
    pep8style.options.ignore = (pep8style.options.ignore +
                                tuple(pep8_additional_ignore))

    # Support for egg shared object wrappers, which are not PEP8 compliant,
    # nor part of the matplotlib repository.
    # DO NOT ADD FILES *IN* THE REPOSITORY TO THIS LIST.
    pep8style.options.exclude.extend(exclude_files)

    # Allow users to add their own exclude list.
    if extra_exclude_file is not None and os.path.exists(extra_exclude_file):
        with open(extra_exclude_file, 'r') as fh:
            extra_exclude = [line.strip() for line in fh if line.strip()]
        pep8style.options.exclude.extend(extra_exclude)

    result = pep8style.check_files([os.path.dirname(module.__file__)])
    if reporter is StandardReportWithExclusions:
        msg = ("Found code syntax errors (and warnings):\n"
               "{0}".format('\n'.join(reporter._global_deferred_print)))
    else:
        msg = "Found code syntax errors (and warnings)."
    assert_equal(result.total_errors, 0, msg)

    # If we've been using the exclusions reporter, check that we didn't
    # exclude files unnecessarily.
    if reporter is StandardReportWithExclusions:
        unexpectedly_good = sorted(set(reporter.expected_bad_files) -
                                   reporter.matched_exclusions)

        if unexpectedly_good:
            raise ValueError('Some exclude patterns were unnecessary as the '
                             'files they pointed to either passed the PEP8 '
                             'tests or do not point to a file:\n  '
                             '{}'.format('\n  '.join(unexpectedly_good)))