Skip to content

Commit

Permalink
https://github.com/life4/flakehell/pull/86
Browse files Browse the repository at this point in the history
  • Loading branch information
thejcannon committed Jul 7, 2020
1 parent 56b2ac7 commit 0a99a42
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 32 deletions.
4 changes: 2 additions & 2 deletions flakehell/_logic/_discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
}


def get_installed(app) -> Iterator[Dict[str, Any]]:
def get_installed(app, argv=[]) -> Iterator[Dict[str, Any]]:
plugins_codes = defaultdict(list)
versions = dict()

app.initialize([])
app.initialize(argv)
codes: Iterable[str]

for check_type in ('ast_plugins', 'logical_line_plugins', 'physical_line_plugins'):
Expand Down
42 changes: 15 additions & 27 deletions flakehell/_patched/_app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# built-in
import sys
from argparse import ArgumentParser
from itertools import chain
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
from typing import Any, Dict, List

# external
from flake8.main.application import Application
Expand Down Expand Up @@ -57,35 +56,17 @@ def option_manager(self, manager):
group.add_argument('--safe', action='store_true', help='suppress exceptions from plugins')
self._option_manager = manager

def get_toml_config(self, path: Path = None) -> Dict[str, Any]:
def get_toml_config(self, path: Path = None, additional_bases: List[Path] = []) -> Dict[str, Any]:
if path is not None:
return read_config(path)
return read_config(*additional_bases, path)
# lookup for config from current dir up to root
root = Path().resolve()
for dir_path in chain([root], root.parents):
path = dir_path / 'pyproject.toml'
if path.exists():
return read_config(path)
return read_config(*additional_bases, path)
return dict()

@staticmethod
def extract_toml_config_path(argv: List[str]) -> Tuple[Optional[Path], List[str]]:
if not argv:
return None, argv

if '--help' in argv:
argv = argv.copy()
argv.remove('--help')
if not argv:
return None, ['--help']

parser = ArgumentParser()
parser.add_argument('--config')
known, unknown = parser.parse_known_args(argv)
if known.config and known.config.endswith('.toml'):
return Path(known.config).expanduser(), unknown
return None, argv

def parse_configuration_and_cli(self, config_finder, argv: List[str]) -> None:
parser = self.option_manager.parser
for action in parser._actions.copy():
Expand All @@ -96,9 +77,16 @@ def parse_configuration_and_cli(self, config_finder, argv: List[str]) -> None:
continue
parser._handle_conflict_resolve(None, [(name, action)])

# if passed `--config` with path to TOML-config, we should extract it
# before passing into flake8 mechanisms
config_path, argv = self.extract_toml_config_path(argv=argv)
# Read --config file
config_path = None
if config_finder.config_file and config_finder.config_file.endswith('.toml'):
config_path = Path(config_finder.config_file).expanduser()

# Read --append-config files and treat them as additional bases
additional_bases = []
for extra_config_file in config_finder.extra_config_files:
if extra_config_file.endswith('.toml'):
additional_bases.append(Path(extra_config_file).expanduser())

# make default config
config, _ = self.option_manager.parse_args([])
Expand All @@ -107,7 +95,7 @@ def parse_configuration_and_cli(self, config_finder, argv: List[str]) -> None:
# patch config wtih TOML
# If config is explicilty passed, it will be used
# If config isn't specified, flakehell will lookup for it
config.__dict__.update(self.get_toml_config(config_path))
config.__dict__.update(self.get_toml_config(config_path, additional_bases))

# Parse CLI options and legacy flake8 configs.
# Based on `aggregate_options`.
Expand Down
2 changes: 1 addition & 1 deletion flakehell/commands/_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def code_command(argv) -> CommandResult:
code = argv[0]

app = FlakeHellApplication(program=NAME, version=VERSION)
plugins = sorted(get_installed(app=app), key=lambda p: p['name'])
plugins = sorted(get_installed(app=app, argv=argv), key=lambda p: p['name'])
if not plugins:
return ExitCode.NO_PLUGINS_INSTALLED, 'no plugins installed'

Expand Down
2 changes: 1 addition & 1 deletion flakehell/commands/_missed.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def missed_command(argv) -> CommandResult:
return ExitCode.TOO_MANY_ARGS, 'the command does not accept arguments'

app = FlakeHellApplication(program=NAME, version=VERSION)
installed_plugins = sorted(get_installed(app=app), key=lambda p: p['name'])
installed_plugins = sorted(get_installed(app=app, argv=argv), key=lambda p: p['name'])
if not installed_plugins:
return ExitCode.NO_PLUGINS_INSTALLED, 'no plugins installed'

Expand Down
2 changes: 1 addition & 1 deletion flakehell/commands/_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def plugins_command(argv) -> CommandResult:
"""Show all installed plugins, their codes prefix, and matched rules from config.
"""
app = FlakeHellApplication(program=NAME, version=VERSION)
plugins = sorted(get_installed(app=app), key=lambda p: p['name'])
plugins = sorted(get_installed(app=app, argv=argv), key=lambda p: p['name'])
if not plugins:
return ExitCode.NO_PLUGINS_INSTALLED, 'no plugins installed'

Expand Down

0 comments on commit 0a99a42

Please sign in to comment.