开发者

how to use exclude option with pep8.py

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/ a开发者_如何学编程lso

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/ ?


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


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.*')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜