10
10
import os
11
11
import sys
12
12
from pathlib import Path
13
- from typing import TYPE_CHECKING
13
+ from typing import TYPE_CHECKING , Dict , List , Tuple
14
14
15
15
from pylint .config .utils import _parse_rich_type_value
16
16
22
22
if TYPE_CHECKING :
23
23
from pylint .lint import PyLinter
24
24
25
+ PylintConfigFileData = Tuple [Dict [str , str ], List [str ]]
25
26
26
- class _ConfigurationFileParser :
27
+
28
+ class _RawConfParser :
27
29
"""Class to parse various formats of configuration files."""
28
30
29
- def __init__ ( self , verbose : bool , linter : PyLinter ) -> None :
30
- self . verbose_mode = verbose
31
- self . linter = linter
31
+ @ staticmethod
32
+ def parse_ini_file ( file_path : Path ) -> PylintConfigFileData :
33
+ """Parse and handle errors of an ini configuration file.
32
34
33
- def _parse_ini_file ( self , file_path : Path ) -> tuple [ dict [ str , str ], list [ str ]]:
34
- """Parse and handle errors of a ini configuration file."""
35
+ Raises ``configparser.Error``.
36
+ """
35
37
parser = configparser .ConfigParser (inline_comment_prefixes = ("#" , ";" ))
36
-
37
38
# Use this encoding in order to strip the BOM marker, if any.
38
39
with open (file_path , encoding = "utf_8_sig" ) as fp :
39
40
parser .read_file (fp )
40
41
41
42
config_content : dict [str , str ] = {}
42
43
options : list [str ] = []
43
- ini_file_with_sections = self ._ini_file_with_sections (file_path )
44
+ ini_file_with_sections = _RawConfParser ._ini_file_with_sections (file_path )
44
45
for section in parser .sections ():
45
46
if ini_file_with_sections and not section .startswith ("pylint" ):
46
47
continue
@@ -58,15 +59,14 @@ def _ini_file_with_sections(file_path: Path) -> bool:
58
59
return True
59
60
return False
60
61
61
- def _parse_toml_file (self , file_path : Path ) -> tuple [dict [str , str ], list [str ]]:
62
- """Parse and handle errors of a toml configuration file."""
63
- try :
64
- with open (file_path , mode = "rb" ) as fp :
65
- content = tomllib .load (fp )
66
- except tomllib .TOMLDecodeError as e :
67
- self .linter .add_message ("config-parse-error" , line = 0 , args = str (e ))
68
- return {}, []
62
+ @staticmethod
63
+ def parse_toml_file (file_path : Path ) -> PylintConfigFileData :
64
+ """Parse and handle errors of a toml configuration file.
69
65
66
+ Raises ``tomllib.TOMLDecodeError``.
67
+ """
68
+ with open (file_path , mode = "rb" ) as fp :
69
+ content = tomllib .load (fp )
70
70
try :
71
71
sections_values = content ["tool" ]["pylint" ]
72
72
except KeyError :
@@ -86,12 +86,16 @@ def _parse_toml_file(self, file_path: Path) -> tuple[dict[str, str], list[str]]:
86
86
options += [f"--{ opt } " , values ]
87
87
return config_content , options
88
88
89
+ @staticmethod
89
90
def parse_config_file (
90
- self , file_path : Path | None
91
- ) -> tuple [dict [str , str ], list [str ]]:
92
- """Parse a config file and return str-str pairs."""
91
+ file_path : Path | None , verbose : bool
92
+ ) -> PylintConfigFileData :
93
+ """Parse a config file and return str-str pairs.
94
+
95
+ Raises ``tomllib.TOMLDecodeError``, ``configparser.Error``.
96
+ """
93
97
if file_path is None :
94
- if self . verbose_mode :
98
+ if verbose :
95
99
print (
96
100
"No config file found, using default configuration" , file = sys .stderr
97
101
)
@@ -101,13 +105,25 @@ def parse_config_file(
101
105
if not file_path .exists ():
102
106
raise OSError (f"The config file { file_path } doesn't exist!" )
103
107
104
- if self . verbose_mode :
108
+ if verbose :
105
109
print (f"Using config file { file_path } " , file = sys .stderr )
106
110
111
+ if file_path .suffix == ".toml" :
112
+ return _RawConfParser .parse_toml_file (file_path )
113
+ return _RawConfParser .parse_ini_file (file_path )
114
+
115
+
116
+ class _ConfigurationFileParser :
117
+ """Class to parse various formats of configuration files."""
118
+
119
+ def __init__ (self , verbose : bool , linter : PyLinter ) -> None :
120
+ self .verbose_mode = verbose
121
+ self .linter = linter
122
+
123
+ def parse_config_file (self , file_path : Path | None ) -> PylintConfigFileData :
124
+ """Parse a config file and return str-str pairs."""
107
125
try :
108
- if file_path .suffix == ".toml" :
109
- return self ._parse_toml_file (file_path )
110
- return self ._parse_ini_file (file_path )
126
+ return _RawConfParser .parse_config_file (file_path , self .verbose_mode )
111
127
except (configparser .Error , tomllib .TOMLDecodeError ) as e :
112
128
self .linter .add_message ("config-parse-error" , line = 0 , args = str (e ))
113
129
return {}, []
0 commit comments