2011-05-03 51 views
16

選項我有這樣如何使用排除與pep8.py

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

的目錄結構,並希望在排除/path/to/dir/a/foo

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

目錄/path/to/dir/和PEP8的預期輸出運行PEP8是的,它不應該包含來自/a/foo/的文件

但是pep8正在檢查/a/foo/裏的文件也

當我做這個

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

它不包括這兩個文件和a/foo/b/foo/

什麼是必須考慮到PEP8模式排除選項,這樣它只能從/a/foo/排除的文件,但不是從b/foo/

回答

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

你排除例如不工作:( – sorin 2012-05-22 14:36:59

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

其實,我覺得我只是不理解unix正則表達式(?),並想使用Python自己的正則表達式。 – 2013-01-26 20:43:48