如何在pep8.py中使用exclude选项

时间:2011-05-03 06:15:31

标签: python pep8

我有一个像这样的目录结构

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

并希望在/path/to/dir/

之外的目录/path/to/dir/a/foo上运行pep8
pep8 --exclude='/path/to/dir/a/foo' /path/to/dir

和pep8的预期输出是,它不应包含/a/foo/

中的文件

但是pep8正在检查/a/foo/内的文件

当我这样做时

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

它会从a/foo /b/foo/

中排除这两个文件

pep8排除选项的模式是什么,以便它仅从/a/foo/但不从b/foo/排除文件?

2 个答案:

答案 0 :(得分:14)

您可以尝试这样的事情:

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

排除部分使用fnmatch匹配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

答案 1 :(得分:2)

我确定我在这里重新发明轮子,但我也无法让API工作:

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