Example #1
0
def get_parser():
    """This returns an instance of optparse.OptionParser with all the
    extensions registered and options set. This wraps ``pep8.get_parser``.
    """
    (extensions, parser_hooks, options_hooks) = _register_extensions()
    details = ', '.join(['%s: %s' % ext for ext in extensions])
    python_version = get_python_version()
    parser = pep8.get_parser(
        'flake8', '%s (%s) %s' % (__version__, details, python_version))
    for opt in ('--repeat', '--testsuite', '--doctest'):
        try:
            parser.remove_option(opt)
        except ValueError:
            pass
    parser.add_option('--exit-zero',
                      action='store_true',
                      help="exit with code 0 even if there are errors")
    for parser_hook in parser_hooks:
        parser_hook(parser)
    parser.add_option('--install-hook',
                      default=False,
                      action='store_true',
                      help='Install the appropriate hook for this '
                      'repository.',
                      dest='install_hook')
    return parser, options_hooks
Example #2
0
def get_parser():
    """Create a custom OptionParser"""
    from flake8 import __version__
    import flakey
    parser = pep8.get_parser()

    def version(option, opt, value, parser):
        parser.print_usage()
        parser.print_version()
        sys.exit(0)

    parser.version = '{0} (pep8: {1}, flakey: {2})'.format(
        __version__, pep8.__version__, flakey.__version__)
    parser.remove_option('--version')
    parser.add_option('--builtins', default='', dest='builtins',
                      help="append builtin functions to flakey's "
                           "_MAGIC_BUILTINS")
    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('--install-hook', default=False, action='store_true',
                      help='Install the appropriate hook for this '
                      'repository.', dest='install_hook')
    # don't overlap with pep8's verbose option
    parser.add_option('-V', '--version', action='callback',
                      callback=version,
                      help='Print the version info for flake8')
    return parser
def test_I101_google_style():
    base_path = os.path.dirname(__file__)
    test_case_path = os.path.join(base_path, "test_cases")
    fullpath = os.path.join(test_case_path, "bad_order_google.py")
    data = open(fullpath).read()
    tree = ast.parse(data, fullpath)

    argv = [
        "--application-import-names=flake8_import_order,tests",
        "--import-order-style=google",
    ]

    parser = pep8.get_parser('', '')
    Linter.add_options(parser)
    options, args = parser.parse_args(argv)
    Linter.parse_options(options)

    checker = Linter(tree, fullpath)
    codes = []
    messages = []
    for lineno, col_offset, msg, instance in checker.run():
        code, message = msg.split(" ", 1)
        codes.append(code)
        messages.append(message)
    assert codes == ["I101"]
    assert messages == [
        "Imported names are in the wrong order. Should be A, c, D"
    ]
Example #4
0
def _register_pyflakes_check():
    """Register the pyFlakes checker into PEP8 set of checks."""
    from flake8_isort import Flake8Isort
    from flake8_blind_except import check_blind_except

    # Resolving conflicts between pep8 and pyflakes.
    codes = {
        "UnusedImport": "F401",
        "ImportShadowedByLoopVar": "F402",
        "ImportStarUsed": "F403",
        "LateFutureImport": "F404",
        "Redefined": "F801",
        "RedefinedInListComp": "F812",
        "UndefinedName": "F821",
        "UndefinedExport": "F822",
        "UndefinedLocal": "F823",
        "DuplicateArgument": "F831",
        "UnusedVariable": "F841",
    }

    for name, obj in vars(pyflakes.messages).items():
        if name[0].isupper() and obj.message:
            obj.tpl = "{0} {1}".format(codes.get(name, "F999"), obj.message)

    pep8.register_check(_PyFlakesChecker, codes=['F'])
    # FIXME parser hack
    parser = pep8.get_parser('', '')
    Flake8Isort.add_options(parser)
    options, args = parser.parse_args([])
    # end of hack
    pep8.register_check(Flake8Isort, codes=['I'])
    pep8.register_check(check_blind_except, codes=['B90'])
Example #5
0
def _register_pyflakes_check():
    """Register the pyFlakes checker into PEP8 set of checks."""
    from flake8_isort import Flake8Isort
    from flake8_blind_except import check_blind_except

    # Resolving conflicts between pep8 and pyflakes.
    codes = {
        "UnusedImport": "F401",
        "ImportShadowedByLoopVar": "F402",
        "ImportStarUsed": "F403",
        "LateFutureImport": "F404",
        "Redefined": "F801",
        "RedefinedInListComp": "F812",
        "UndefinedName": "F821",
        "UndefinedExport": "F822",
        "UndefinedLocal": "F823",
        "DuplicateArgument": "F831",
        "UnusedVariable": "F841",
    }

    for name, obj in vars(pyflakes.messages).items():
        if name[0].isupper() and obj.message:
            obj.tpl = "{0} {1}".format(codes.get(name, "F999"), obj.message)

    pep8.register_check(_PyFlakesChecker, codes=['F'])
    # FIXME parser hack
    parser = pep8.get_parser('', '')
    Flake8Isort.add_options(parser)
    options, args = parser.parse_args([])
    # end of hack
    pep8.register_check(Flake8Isort, codes=['I'])
    pep8.register_check(check_blind_except, codes=['B90'])
Example #6
0
def get_parser():
    """This returns an instance of optparse.OptionParser with all the
    extensions registered and options set. This wraps ``pep8.get_parser``.
    """
    (extensions, parser_hooks, options_hooks) = _register_extensions()
    details = ', '.join(['%s: %s' % ext for ext in extensions])
    python_version = get_python_version()
    parser = pep8.get_parser('flake8', '%s (%s) %s' % (
        __version__, details, python_version
    ))
    for opt in ('--repeat', '--testsuite', '--doctest'):
        try:
            parser.remove_option(opt)
        except ValueError:
            pass

    if multiprocessing:
        parser.config_options.append('jobs')
        parser.add_option('-j', '--jobs', type='string', default='auto',
                          help="number of jobs to run simultaneously, "
                          "or 'auto'. This is ignored on Windows.")

    parser.add_option('--exit-zero', action='store_true',
                      help="exit with code 0 even if there are errors")
    for parser_hook in parser_hooks:
        parser_hook(parser)
    # See comment above regarding why this has to be a callback.
    parser.add_option('--install-hook', default=False, dest='install_hook',
                      help='Install the appropriate hook for this '
                      'repository.', action='callback',
                      callback=_install_hook_cb)
    return parser, options_hooks
Example #7
0
def test_I101_google_style():
    base_path = os.path.dirname(__file__)
    test_case_path = os.path.join(base_path, "test_cases")
    fullpath = os.path.join(test_case_path, "bad_order_google.py")
    data = open(fullpath).read()
    tree = ast.parse(data, fullpath)

    argv = [
        "--application-import-names=flake8_import_order,tests",
        "--import-order-style=google",
    ]

    parser = pep8.get_parser('', '')
    Linter.add_options(parser)
    options, args = parser.parse_args(argv)
    Linter.parse_options(options)

    checker = Linter(tree, fullpath)
    codes = []
    messages = []
    for lineno, col_offset, msg, instance in checker.run():
        code, message = msg.split(" ", 1)
        codes.append(code)
        messages.append(message)
    assert codes == ["I101"]
    assert messages == [
        "Imported names are in the wrong order. Should be A, c, D"
    ]
Example #8
0
    def check(self, code, filename):
        """Run pep8 on code and return the output."""
        options = {'reporter': self.get_report()}

        type_map = {
            'select': [],
            'ignore': [],
            'max-line-length': 0,
            'max-complexity': 0
        }

        self.build_options(options,
                           type_map,
                           transform=lambda s: s.replace('-', '_'))

        final_options = options.copy()

        # Try to read options from pep8 default configuration files (.pep8, tox.ini).
        # If present, they will override the ones defined by Sublime Linter's config.
        try:
            # `onError` will be called by `process_options` when no pep8 configuration file is found.
            # Override needed to supress OptionParser.error() output in the default parser.
            def onError(msg):
                pass

            from pep8 import process_options, get_parser
            parser = get_parser()
            parser.error = onError
            pep8_options, _ = process_options([os.curdir],
                                              True,
                                              True,
                                              parser=parser)

            # Merge options only if the pep8 config file actually exists;
            # pep8 always returns a config filename, even when it doesn't exist!
            if os.path.isfile(pep8_options.config):
                pep8_options = vars(pep8_options)
                pep8_options.pop('reporter', None)
                for opt_n, opt_v in pep8_options.items():
                    if isinstance(final_options.get(opt_n, None), list):
                        final_options[opt_n] += opt_v
                    else:
                        final_options[opt_n] = opt_v
        except SystemExit:
            # Catch and ignore parser.error() when no config files are found.
            pass

        if persist.debug_mode():
            persist.printf('{} ST options: {}'.format(self.name, options))
            persist.printf('{} options: {}'.format(self.name, final_options))

        checker = self.module.StyleGuide(**final_options)

        return checker.input_file(filename=os.path.basename(filename),
                                  lines=code.splitlines(keepends=True))
Example #9
0
    def check(self, code, filename):
        """Run pep8 on code and return the output."""
        options = {
            'reporter': self.get_report()
        }

        type_map = {
            'select': [],
            'ignore': [],
            'max-line-length': 0,
            'max-complexity': 0
        }

        self.build_options(options, type_map, transform=lambda s: s.replace('-', '_'))

        final_options = options.copy()

        # Try to read options from pep8 default configuration files (.pep8, tox.ini).
        # If present, they will override the ones defined by Sublime Linter's config.
        try:
            # `onError` will be called by `process_options` when no pep8 configuration file is found.
            # Override needed to supress OptionParser.error() output in the default parser.
            def onError(msg):
                pass

            from pep8 import process_options, get_parser
            parser = get_parser()
            parser.error = onError
            pep8_options, _ = process_options([os.curdir], True, True, parser=parser)

            # Merge options only if the pep8 config file actually exists;
            # pep8 always returns a config filename, even when it doesn't exist!
            if os.path.isfile(pep8_options.config):
                pep8_options = vars(pep8_options)
                pep8_options.pop('reporter', None)
                for opt_n, opt_v in pep8_options.items():
                    if isinstance(final_options.get(opt_n, None), list):
                        final_options[opt_n] += opt_v
                    else:
                        final_options[opt_n] = opt_v
        except SystemExit:
            # Catch and ignore parser.error() when no config files are found.
            pass

        if persist.debug_mode():
            persist.printf('{} ST options: {}'.format(self.name, options))
            persist.printf('{} options: {}'.format(self.name, final_options))

        checker = self.module.StyleGuide(**final_options)

        return checker.input_file(
            filename=os.path.basename(filename),
            lines=code.splitlines(keepends=True)
        )
Example #10
0
def get_parser():
    """This returns an instance of optparse.OptionParser with all the
    extensions registered and options set. This wraps ``pep8.get_parser``.
    """
    (extensions, parser_hooks, options_hooks, ignored) = _register_extensions()
    details = ', '.join('%s: %s' % ext for ext in extensions)
    python_version = get_python_version()
    parser = pep8.get_parser(
        'flake8', '%s (%s) %s' % (__version__, details, python_version))
    for opt in ('--repeat', '--testsuite', '--doctest'):
        try:
            parser.remove_option(opt)
        except ValueError:
            pass

    if multiprocessing:
        parser.config_options.append('jobs')
        parser.add_option('-j',
                          '--jobs',
                          type='string',
                          default='auto',
                          help="number of jobs to run simultaneously, "
                          "or 'auto'. This is ignored on Windows.")

    parser.add_option('--exit-zero',
                      action='store_true',
                      help="exit with code 0 even if there are errors")
    for parser_hook in parser_hooks:
        parser_hook(parser)
    # See comment above regarding why this has to be a callback.
    parser.add_option('--install-hook',
                      default=False,
                      dest='install_hook',
                      help='Install the appropriate hook for this '
                      'repository.',
                      action='callback',
                      callback=callbacks.install_vcs_hook)
    parser.add_option('--output-file',
                      default=None,
                      help='Redirect report to a file.',
                      type='string',
                      nargs=1,
                      action='callback',
                      callback=callbacks.redirect_stdout)
    parser.add_option('--enable-extensions',
                      default='',
                      dest='enabled_extensions',
                      help='Enable plugins and extensions that are disabled '
                      'by default',
                      type='string')
    parser.config_options.extend(['output_file', 'enable_extensions'])
    parser.ignored_extensions = ignored
    return parser, options_hooks
def test_expected_error(tree, filename, expected):
    parser = pep8.get_parser('', '')
    Linter.add_options(parser)
    options, args = parser.parse_args(
        ['--import-order-style=google'] if 'google' in filename else [])
    Linter.parse_options(options)

    checker = Linter(tree, filename)
    errors = []
    for lineno, col_offset, msg, instance in checker.run():
        errors.append(msg.split()[0])
    assert errors == expected
Example #12
0
def test_expected_error(tree, filename, expected):
    parser = pep8.get_parser('', '')
    Linter.add_options(parser)
    options, args = parser.parse_args(
        ['--import-order-style=google'] if 'google' in filename else [])
    Linter.parse_options(options)

    checker = Linter(tree, filename)
    errors = []
    for lineno, col_offset, msg, instance in checker.run():
        errors.append(msg.split()[0])
    assert errors == expected
Example #13
0
def get_parser():
    (extensions, parser_hooks, options_hooks) = register_extensions()
    details = ', '.join(['%s: %s' % ext for ext in extensions])
    parser = pep8.get_parser('flint', '%s (%s)' % (__version__, details))
    for opt in ('--repeat', '--testsuite', '--doctest'):
        try:
            parser.remove_option(opt)
        except ValueError:
            pass
    parser.add_option('--exit-zero', action='store_true',
                      help="exit with code 0 even if there are errors")
    for parser_hook in parser_hooks:
        parser_hook(parser)
    return parser, options_hooks
Example #14
0
def merge_opts(pep8_opts, our_opts):
    pep8_parser = pep8.get_parser()

    for o in pep8_parser.option_list:
        if not (o.dest and getattr(our_opts, o.dest)):
            continue

        new_val = getattr(our_opts, o.dest)
        old_val = getattr(pep8_opts, o.dest)
        if isinstance(old_val, list):
            old_val.extend(new_val)
            continue
        elif isinstance(old_val, tuple):
            new_val = tuple(new_val)
        setattr(pep8_opts, o.dest, new_val)
Example #15
0
    def run(path, code=None, params=None, **meta):
        """Check code with PEP8.

        :return list: List of errors.
        """
        parser = get_parser()
        for option in parser.option_list:
            if option.dest and option.dest in params:
                value = params[option.dest]
                if not isinstance(value, str):
                    continue
                params[option.dest] = option.convert_value(option, params[option.dest])
        P8Style = StyleGuide(reporter=_PEP8Report, **params)
        buf = StringIO(code)
        return P8Style.input_file(path, lines=buf.readlines())
Example #16
0
    def run(path, code=None, params=None, **meta):
        """Check code with PEP8.

        :return list: List of errors.
        """
        parser = get_parser()
        for option in parser.option_list:
            if option.dest and option.dest in params:
                value = params[option.dest]
                if not isinstance(value, str):
                    continue
                params[option.dest] = option.convert_value(
                    option, params[option.dest])
        P8Style = StyleGuide(reporter=_PEP8Report, **params)
        buf = StringIO(code)
        return P8Style.input_file(path, lines=buf.readlines())
def get_parser():
    """This returns an instance of optparse.OptionParser with all the
    extensions registered and options set. This wraps ``pep8.get_parser``.
    """
    (extensions, parser_hooks, options_hooks, ignored) = _register_extensions()
    details = ", ".join(["%s: %s" % ext for ext in extensions])
    python_version = get_python_version()
    parser = pep8.get_parser("flake8", "%s (%s) %s" % (__version__, details, python_version))
    for opt in ("--repeat", "--testsuite", "--doctest"):
        try:
            parser.remove_option(opt)
        except ValueError:
            pass

    if multiprocessing:
        parser.config_options.append("jobs")
        parser.add_option(
            "-j",
            "--jobs",
            type="string",
            default="auto",
            help="number of jobs to run simultaneously, " "or 'auto'. This is ignored on Windows.",
        )

    parser.add_option("--exit-zero", action="store_true", help="exit with code 0 even if there are errors")
    for parser_hook in parser_hooks:
        parser_hook(parser)
    # See comment above regarding why this has to be a callback.
    parser.add_option(
        "--install-hook",
        default=False,
        dest="install_hook",
        help="Install the appropriate hook for this " "repository.",
        action="callback",
        callback=callbacks.install_vcs_hook,
    )
    parser.add_option(
        "--output-file",
        default=None,
        help="Redirect report to a file.",
        type="string",
        nargs=1,
        action="callback",
        callback=callbacks.redirect_stdout,
    )
    parser.ignored_extensions = ignored
    return parser, options_hooks
Example #18
0
def get_parser():
    """This returns an instance of optparse.OptionParser with all the
    extensions registered and options set. This wraps ``pep8.get_parser``.
    """
    (extensions, parser_hooks, options_hooks) = _register_extensions()
    details = ', '.join(['%s: %s' % ext for ext in extensions])
    python_version = get_python_version()
    parser = pep8.get_parser(
        'flake8', '%s (%s) %s' % (__version__, details, python_version))
    for opt in ('--repeat', '--testsuite', '--doctest'):
        try:
            parser.remove_option(opt)
        except ValueError:
            pass

    if multiprocessing:
        try:
            auto = multiprocessing.cpu_count() or 1
        except NotImplementedError:
            auto = 1
        parser.config_options.append('jobs')
        parser.add_option('-j',
                          '--jobs',
                          type='string',
                          default='auto',
                          help="number of jobs to run simultaneously, "
                          "or 'auto'. This is ignored on Windows.")

    parser.add_option('--exit-zero',
                      action='store_true',
                      help="exit with code 0 even if there are errors")
    for parser_hook in parser_hooks:
        parser_hook(parser)
    # See comment above regarding why this has to be a callback.
    parser.add_option('--install-hook',
                      default=False,
                      dest='install_hook',
                      help='Install the appropriate hook for this '
                      'repository.',
                      action='callback',
                      callback=_install_hook_cb)
    return parser, options_hooks
Example #19
0
def get_parser():
    """This returns an instance of optparse.OptionParser with all the
    extensions registered and options set. This wraps ``pep8.get_parser``.
    """
    (extensions, parser_hooks, options_hooks) = _register_extensions()
    details = ', '.join(['%s: %s' % ext for ext in extensions])
    parser = pep8.get_parser('flake8', '%s (%s)' % (__version__, details))
    for opt in ('--repeat', '--testsuite', '--doctest'):
        try:
            parser.remove_option(opt)
        except ValueError:
            pass
    parser.add_option('--exit-zero', action='store_true',
                      help="exit with code 0 even if there are errors")
    for parser_hook in parser_hooks:
        parser_hook(parser)
    parser.add_option('--install-hook', default=False, action='store_true',
                      help='Install the appropriate hook for this '
                      'repository.', dest='install_hook')
    return parser, options_hooks
Example #20
0
def test_expected_error(tree, filename, expected_codes, expected_messages):
    argv = ["--application-import-names=flake8_import_order,tests"]

    if 'google' in filename:
        argv.append('--import-order-style=google')

    parser = pep8.get_parser('', '')
    Linter.add_options(parser)
    options, args = parser.parse_args(argv)
    Linter.parse_options(options)

    checker = Linter(tree, filename)
    codes = []
    messages = []
    for lineno, col_offset, msg, instance in checker.run():
        code, message = msg.split(" ", 1)
        codes.append(code)
        messages.append(message)
    assert codes == expected_codes
    assert set(messages) >= set(expected_messages)
Example #21
0
def get_parser():
    """This returns an instance of optparse.OptionParser with all the
    extensions registered and options set. This wraps ``pep8.get_parser``.
    """
    (extensions, parser_hooks, options_hooks) = _register_extensions()
    details = ", ".join(["%s: %s" % ext for ext in extensions])
    python_version = get_python_version()
    parser = pep8.get_parser("flake8", "%s (%s) %s" % (__version__, details, python_version))
    for opt in ("--repeat", "--testsuite", "--doctest"):
        try:
            parser.remove_option(opt)
        except ValueError:
            pass

    if multiprocessing:
        try:
            auto = multiprocessing.cpu_count() or 1
        except NotImplementedError:
            auto = 1
        parser.config_options.append("jobs")
        parser.add_option(
            "-j",
            "--jobs",
            type="string",
            default="auto",
            help="number of jobs to run simultaneously, " "or 'auto'. This is ignored on Windows.",
        )

    parser.add_option("--exit-zero", action="store_true", help="exit with code 0 even if there are errors")
    for parser_hook in parser_hooks:
        parser_hook(parser)
    parser.add_option(
        "--install-hook",
        default=False,
        action="store_true",
        help="Install the appropriate hook for this " "repository.",
        dest="install_hook",
    )
    return parser, options_hooks
def test_expected_error(tree, filename, expected_codes, expected_messages):
    argv = [
        "--application-import-names=flake8_import_order,tests"
    ]

    if 'google' in filename:
        argv.append('--import-order-style=google')

    parser = pep8.get_parser('', '')
    Linter.add_options(parser)
    options, args = parser.parse_args(argv)
    Linter.parse_options(options)

    checker = Linter(tree, filename)
    codes = []
    messages = []
    for lineno, col_offset, msg, instance in checker.run():
        code, message = msg.split(" ", 1)
        codes.append(code)
        messages.append(message)
    assert codes == expected_codes
    assert set(messages) >= set(expected_messages)
def get_formatted_opts():
    opts = get_parser().config_options
    return [opt.replace("-", "_") for opt in opts if opt != "verbose"]