flake8 / flake8 / util.py

The branch 'refactor' does not exist.
Jannis Leidel cfb3d3b 
Tarek Ziadé 45642b2 
Tarek Ziadé 25ea751 
Ian Cordasco 10db2c2 

Tarek Ziadé 45642b2 
Ian Cordasco 9a326fc 

Tarek Ziadé 45642b2 
Ian Cordasco 10db2c2 

Ian Cordasco 25103cb 
Tarek Ziadé 45642b2 
Bruno Miguel Cus… 0aec4c3 
Ian Cordasco bc9673e 

Ian Cordasco 25103cb 

Tarek Ziadé 25ea751 

Tarek Ziadé 13d068a 

Tarek Ziadé 45642b2 

Tarek Ziadé a5af8c9 
Tarek Ziadé 45642b2 

cyberdelia d7ed866 
Tarek Ziadé 45642b2 

Jannis Leidel cfb3d3b 
Tarek Ziadé 45642b2 

Ian Cordasco 93aaaa4 

Tarek Ziadé 45642b2 

Jannis Leidel cfb3d3b 
Ian Cordasco 9a326fc 

from __future__ import with_statement
import re
import os
import optparse

    # Python 2
    from ConfigParser import ConfigParser
except ImportError:
    # Python 3
    from configparser import ConfigParser

pep8style = None

def get_parser():
    """Create a custom OptionParser"""
    from flake8 import __version__
    import flakey
    import pep8

    def version(option, opt, value, parser):

    def help(option, opt, value, parser):
        p = pep8.get_parser()

    # Create our own parser
    parser = optparse.OptionParser('%prog [options] [file.py|directory]',
                                   version=version, add_help_option=False)
    parser.version = '{0} (pep8: {1}, flakey: {2})'.format(
        __version__, pep8.__version__, flakey.__version__)
    # don't overlap with pep8's verbose option
    parser.add_option('--builtins', default='', dest='builtins',
                      help="append builtin functions to flakey's "
    parser.add_option('--ignore', default='',
                      help='skip errors and warnings (e.g. E4,W)')
    parser.add_option('--exit-zero', action='store_true', default=False,
                      help='Exit with status 0 even if there are errors')
    parser.add_option('--max-complexity', default=-1, action='store',
                      type='int', help='McCabe complexity threshold')
    parser.add_option('-V', '--version', action='callback',
                      help='Print the version info for flake8')
    parser.add_option('--install-hook', default=False, action='store_true',
                      help='Install the appropriate hook for this '
                      'repository.', dest='install_hook')
    parser.add_option('-h', '--help', action='callback', callback=help,
                      help='Print this message and exit')
    return parser

def skip_warning(warning, ignore=[]):
    # XXX quick dirty hack, just need to keep the line in the warning
    if not hasattr(warning, 'message'):
        # McCabe's warnings cannot be skipped afaik, and they're all strings.
        return False
    if warning.message.split()[0] in ignore:
        return True
    if not os.path.isfile(warning.filename):
        return False

    # XXX should cache the file in memory
    with open(warning.filename) as f:
        line = f.readlines()[warning.lineno - 1]

    return skip_line(line)

def skip_line(line):
    return line.strip().lower().endswith('# noqa')

_NOQA = re.compile(r'flake8[:=]\s*noqa', re.I | re.M)

def skip_file(path):
    """Returns True if this header is found in path

    # flake8: noqa
    if not os.path.isfile(path):
        return False
    f = open(path)
        content = f.read()
    return _NOQA.search(content) is not None

def _initpep8():
    # default pep8 setup
    global pep8style
    import pep8
    if pep8style is None:
        pep8style = pep8.StyleGuide(config_file=True)
    pep8style.options.physical_checks = pep8.find_checks('physical_line')
    pep8style.options.logical_checks = pep8.find_checks('logical_line')
    pep8style.options.counters = dict.fromkeys(pep8.BENCHMARK_KEYS, 0)
    pep8style.options.messages = {}
    pep8style.options.max_line_length = 79
    pep8style.args = []
    return pep8style
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.