10
10
import optparse # pylint: disable=deprecated-module
11
11
import os
12
12
import sys
13
+ from pathlib import Path
13
14
from types import ModuleType
14
- from typing import Dict , List , Optional , TextIO , Tuple
15
+ from typing import Dict , List , Optional , TextIO , Tuple , Union
15
16
16
17
import toml
17
18
@@ -269,34 +270,14 @@ def read_config_file(self, config_file=None, verbose=None):
269
270
raise OSError (f"The config file { config_file } doesn't exist!" )
270
271
271
272
use_config_file = config_file and os .path .exists (config_file )
272
- if use_config_file : # pylint: disable=too-many-nested-blocks
273
+ if use_config_file :
273
274
parser = self .cfgfile_parser
274
-
275
275
if config_file .endswith (".toml" ):
276
- with open (config_file , encoding = "utf-8" ) as fp :
277
- content = toml .load (fp )
278
-
279
- try :
280
- sections_values = content ["tool" ]["pylint" ]
281
- except KeyError :
282
- pass
283
- else :
284
- for section , values in sections_values .items ():
285
- # TOML has rich types, convert values to
286
- # strings as ConfigParser expects.
287
- for option , value in values .items ():
288
- if isinstance (value , bool ):
289
- values [option ] = "yes" if value else "no"
290
- elif isinstance (value , (int , float )):
291
- values [option ] = str (value )
292
- elif isinstance (value , list ):
293
- values [option ] = "," .join (value )
294
- parser ._sections [section .upper ()] = values
276
+ self ._parse_toml (config_file , parser )
295
277
else :
296
278
# Use this encoding in order to strip the BOM marker, if any.
297
279
with open (config_file , encoding = "utf_8_sig" ) as fp :
298
280
parser .read_file (fp )
299
-
300
281
# normalize sections'title
301
282
for sect , values in list (parser ._sections .items ()):
302
283
if sect .startswith ("pylint." ):
@@ -311,6 +292,30 @@ def read_config_file(self, config_file=None, verbose=None):
311
292
msg = "No config file found, using default configuration"
312
293
print (msg , file = sys .stderr )
313
294
295
+ @staticmethod
296
+ def _parse_toml (
297
+ config_file : Union [Path , str ], parser : configparser .ConfigParser
298
+ ) -> None :
299
+ """Upgrade the parser from a toml configuration file."""
300
+ with open (config_file , encoding = "utf-8" ) as fp :
301
+ content = toml .load (fp )
302
+ try :
303
+ sections_values = content ["tool" ]["pylint" ]
304
+ except KeyError :
305
+ pass
306
+ else :
307
+ for section , values in sections_values .items ():
308
+ # TOML has rich types, convert values to
309
+ # strings as ConfigParser expects.
310
+ for option , value in values .items ():
311
+ if isinstance (value , bool ):
312
+ values [option ] = "yes" if value else "no"
313
+ elif isinstance (value , (int , float )):
314
+ values [option ] = str (value )
315
+ elif isinstance (value , list ):
316
+ values [option ] = "," .join (value )
317
+ parser ._sections [section .upper ()] = values # type: ignore
318
+
314
319
def load_config_file (self ):
315
320
"""Dispatch values previously read from a configuration file to each
316
321
options provider)"""
0 commit comments