Skip to content

Commit 182eb8e

Browse files
authored
Don't assume sys.argv only contains Flake8's command-line options.
Issue #19 was addressed by overriding Flake8's `aggregate_options()` function, similar to how Flake8's `parse_config()` is being overwritten currently. This solution was seen as preferable to simply calling `parse_known_args()` because it allows this plug-in to work with calls to `flake8.main.cli.main()` (i.e. it doesn't assume that `--toml_config` is coming from the command line).
1 parent d4064f6 commit 182eb8e

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

flake8p/hook.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from . import meta
88
import flake8.main.cli
9+
import flake8.options.aggregator
910
import flake8.options.config
1011
import sys
1112
if sys.version_info >= (3, 11):
@@ -20,8 +21,32 @@
2021
# Hook #
2122
########################################
2223

23-
# Remember original Flake8 object.
24+
# Remember original Flake8 objects.
25+
flake8_aggregate_options = flake8.options.aggregator.aggregate_options
2426
flake8_parse_config = flake8.options.config.parse_config
27+
# Global variable pointing to TOML config.
28+
toml_config = Path('pyproject.toml')
29+
30+
31+
def aggregate_options(manager, cfg, cfg_dir, argv):
32+
"""
33+
Overrides Flake8's option aggregation.
34+
35+
If a custom TOML file was specified via the `--toml-config`
36+
command-line option, its value is stored in a global variable for
37+
later consumption in parse_config().
38+
39+
Finally, Flake8's aggregate_options() is called as usual.
40+
"""
41+
arguments = manager.parse_args(argv)
42+
global toml_config
43+
if arguments.toml_config:
44+
toml_config = Path(arguments.toml_config).resolve()
45+
if not toml_config.exists():
46+
raise FileNotFoundError(
47+
f'Plug-in {meta.title} could not find '
48+
f'custom configuration file "{toml_config}".')
49+
return flake8_aggregate_options(manager, cfg, cfg_dir, argv)
2550

2651

2752
def parse_config(option_manager, cfg, cfg_dir):
@@ -35,27 +60,18 @@ def parse_config(option_manager, cfg, cfg_dir):
3560
If a custom TOML file was specified via the `--toml-config`
3661
command-line option, we read the section from that file instead.
3762
"""
38-
arguments = option_manager.parser.parse_args()
39-
if arguments.toml_config:
40-
file = Path(arguments.toml_config)
41-
if not file.exists():
42-
raise FileNotFoundError(f'Plug-in {meta.title} could not find '
43-
f'custom configuration file "{file}".')
44-
else:
45-
file = Path('pyproject.toml')
46-
47-
if file.exists():
48-
with file.open('rb') as stream:
63+
if toml_config.exists():
64+
with toml_config.open('rb') as stream:
4965
pyproject = toml.load(stream)
5066
if 'tool' in pyproject and 'flake8' in pyproject['tool']:
51-
parser = configparser.RawConfigParser()
67+
parser = configparser.RawConfigParser()
5268
section = 'flake8'
5369
parser.add_section(section)
5470
for (key, value) in pyproject['tool']['flake8'].items():
5571
if isinstance(value, (bool, int, float)):
5672
value = str(value)
5773
parser.set(section, key, value)
58-
(cfg, cfg_dir) = (parser, str(file.resolve().parent))
74+
(cfg, cfg_dir) = (parser, str(toml_config.parent))
5975

6076
return flake8_parse_config(option_manager, cfg, cfg_dir)
6177

@@ -72,6 +88,7 @@ class Plugin:
7288
"""
7389
@classmethod
7490
def add_options(cls, parser):
91+
flake8.options.aggregator.aggregate_options = aggregate_options
7592
flake8.options.config.parse_config = parse_config
7693
parser.add_option(
7794
'--toml-config', metavar='TOML_COMFIG',

0 commit comments

Comments
 (0)