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