6
6
7
7
from . import meta
8
8
import flake8 .main .cli
9
+ import flake8 .options .aggregator
9
10
import flake8 .options .config
10
11
import sys
11
12
if sys .version_info >= (3 , 11 ):
20
21
# Hook #
21
22
########################################
22
23
23
- # Remember original Flake8 object.
24
+ # Remember original Flake8 objects.
25
+ flake8_aggregate_options = flake8 .options .aggregator .aggregate_options
24
26
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 )
25
50
26
51
27
52
def parse_config (option_manager , cfg , cfg_dir ):
@@ -35,27 +60,18 @@ def parse_config(option_manager, cfg, cfg_dir):
35
60
If a custom TOML file was specified via the `--toml-config`
36
61
command-line option, we read the section from that file instead.
37
62
"""
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 :
49
65
pyproject = toml .load (stream )
50
66
if 'tool' in pyproject and 'flake8' in pyproject ['tool' ]:
51
- parser = configparser .RawConfigParser ()
67
+ parser = configparser .RawConfigParser ()
52
68
section = 'flake8'
53
69
parser .add_section (section )
54
70
for (key , value ) in pyproject ['tool' ]['flake8' ].items ():
55
71
if isinstance (value , (bool , int , float )):
56
72
value = str (value )
57
73
parser .set (section , key , value )
58
- (cfg , cfg_dir ) = (parser , str (file . resolve () .parent ))
74
+ (cfg , cfg_dir ) = (parser , str (toml_config .parent ))
59
75
60
76
return flake8_parse_config (option_manager , cfg , cfg_dir )
61
77
@@ -72,6 +88,7 @@ class Plugin:
72
88
"""
73
89
@classmethod
74
90
def add_options (cls , parser ):
91
+ flake8 .options .aggregator .aggregate_options = aggregate_options
75
92
flake8 .options .config .parse_config = parse_config
76
93
parser .add_option (
77
94
'--toml-config' , metavar = 'TOML_COMFIG' ,
0 commit comments