Skip to content
This repository has been archived by the owner on Jan 12, 2021. It is now read-only.

Fix --config handling and allow --append-config #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, don't use mutable default arguments for functions.

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
8 changes: 3 additions & 5 deletions flakehell/commands/_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ def code_command(argv) -> CommandResult:
"""
if not argv:
return ExitCode.NO_PLUGIN_NAME, 'no plugin name provided'
if argv[0] == '--help':
if '--help' in argv:
print(code_command.__doc__)
return ExitCode.OK, ''
if len(argv) > 1:
return ExitCode.TOO_MANY_ARGS, 'the command accept only one argument'
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'

code = app.args[0]
messages = []
checked = set()
for plugin in plugins:
Expand Down
4 changes: 1 addition & 3 deletions flakehell/commands/_missed.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ def missed_command(argv) -> CommandResult:
if argv and argv[0] == '--help':
print(missed_command.__doc__)
return ExitCode.OK, ''
if argv:
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