Skip to content

Commit

Permalink
Merge branch 'feature/output-file' into 'master'
Browse files Browse the repository at this point in the history
Add --output-file option

Addresses #15

This is, however, unfortunately affected by #17. That will need to be fixed before this can be merged.

See merge request !15
  • Loading branch information
sigmavirus24 committed Dec 27, 2014
2 parents c761d22 + 8adb88c commit 6b27451
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
27 changes: 27 additions & 0 deletions flake8/callbacks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import atexit
import sys


def install_vcs_hook(option, option_str, value, parser):
# For now, there's no way to affect a change in how pep8 processes
# options. If no args are provided and there's no config file present,
# it will error out because no input was provided. To get around this,
# when we're using --install-hook, we'll say that there were arguments so
# we can actually attempt to install the hook.
# See: https://gitlab.com/pycqa/flake8/issues/2 and
# https://github.com/jcrocholl/pep8/blob/4c5bf00cb613be617c7f48d3b2b82a1c7b895ac1/pep8.py#L1912
# for more context.
parser.values.install_hook = True
parser.rargs.append('.')


def restore_stdout(old_stdout):
sys.stdout.close()
sys.stdout = old_stdout


def redirect_stdout(option, option_str, value, parser):
fd = open(value, 'w')
old_stdout, sys.stdout = sys.stdout, fd

atexit.register(restore_stdout, old_stdout)
20 changes: 6 additions & 14 deletions flake8/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pep8

from flake8 import __version__
from flake8 import callbacks
from flake8.reporter import multiprocessing, BaseQReport, QueueReport
from flake8.util import OrderedSet, is_windows, is_using_stdin

Expand Down Expand Up @@ -38,19 +39,6 @@ def _register_extensions():
return extensions, parser_hooks, options_hooks, ignored_hooks


def _install_hook_cb(option, option_str, value, parser):
# For now, there's no way to affect a change in how pep8 processes
# options. If no args are provided and there's no config file present,
# it will error out because no input was provided. To get around this,
# when we're using --install-hook, we'll say that there were arguments so
# we can actually attempt to install the hook.
# See: https://gitlab.com/pycqa/flake8/issues/2 and
# https://github.com/jcrocholl/pep8/blob/4c5bf00cb613be617c7f48d3b2b82a1c7b895ac1/pep8.py#L1912
# for more context.
parser.values.install_hook = True
parser.rargs.append('.')


def get_parser():
"""This returns an instance of optparse.OptionParser with all the
extensions registered and options set. This wraps ``pep8.get_parser``.
Expand Down Expand Up @@ -81,7 +69,11 @@ def get_parser():
parser.add_option('--install-hook', default=False, dest='install_hook',
help='Install the appropriate hook for this '
'repository.', action='callback',
callback=_install_hook_cb)
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

Expand Down

0 comments on commit 6b27451

Please sign in to comment.