Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use exclude option with pep8.py

Tags:

python

pep8

I have a directory structure like this

/path/to/dir/a/foo
/path/to/dir/b/foo

and want to run pep8 on the directory /path/to/dir/ excluding /path/to/dir/a/foo

pep8 --exclude='/path/to/dir/a/foo' /path/to/dir

and the expected output of pep8 is, it should not include the files from /a/foo/

but pep8 is checking the files inside the /a/foo/ also

when I do this

pep8 --exclude='foo' /path/to/dir

it is excluding the files from both and a/foo /b/foo/

what is the pattern to be given to pep8 exclude option so that it exclude the files only from /a/foo/ but not from b/foo/ ?

like image 566
Ashok Avatar asked May 03 '11 06:05

Ashok


People also ask

What does Pycodestyle do?

pycodestyle is a tool to check your Python code against some of the style conventions in PEP 8. This package used to be called pep8 but was renamed to pycodestyle to reduce confusion.

What is PEP 8 and why is it important?

PEP 8 is a document that provides various guidelines to write the readable in Python. PEP 8 describes how the developer can write beautiful code. It was officially written in 2001 by Guido van Rossum, Barry Warsaw, and Nick Coghlan. The main aim of PEP is to enhance the readability and consistency of code.


2 Answers

You can try something like this:

pep8 --exclude='*/a/foo*' /path/to/dir

The exclude portion uses fnmatch to match against the path as seen in the source code.

def excluded(filename):
    """
    Check if options.exclude contains a pattern that matches filename.
    """
    basename = os.path.basename(filename)
    for pattern in options.exclude:
        if fnmatch(basename, pattern):
            # print basename, 'excluded because it matches', pattern
            return True
like image 178
Senthil Kumaran Avatar answered Oct 11 '22 15:10

Senthil Kumaran


I'm sure I'm reinventing the wheel here, but I have also been unable to get the API working:

import os
import re
from pep8 import StyleGuide


def get_pyfiles(directory=None, exclusions=None, ftype='.py'):
    '''generator of all ftype files in all subdirectories.
    if directory is None, will look in current directory.
    exclusions should be a regular expression.

    '''
    if directory is None:
        directory = os.getcwd()

    pyfiles = (os.path.join(dpath, fname)
               for dpath, dnames, fnames in os.walk(directory)
               for fname in [f for f in fnames
                             if f.endswith(ftype)])

    if exclusions is not None:
        c = re.compile(exclusions)
        pyfiles = (fname for fname in pyfiles if c.match(fname) is None)

    return pyfiles


def get_pep8_counter(directory=None, exclusions=None):
    if directory is None:
        directory = os.getcwd()
    paths = list(get_pyfiles(directory=directory, exclusions=exclusions))
    # I am only interested in counters (but you could do something else)
    return StyleGuide(paths=paths).check_files().counters

counter = get_pep8_counter(exclusions='.*src.*|.*doc.*')
like image 31
Andy Hayden Avatar answered Oct 11 '22 14:10

Andy Hayden