Skip to content

Commit e24e936

Browse files
[refactor] Create a config parsing class indpendant from Pylinter
This will permit to parse config file without having to instantiate a linter for tool working on pylint configuration like #5462
1 parent 893cb78 commit e24e936

File tree

1 file changed

+56
-23
lines changed

1 file changed

+56
-23
lines changed

pylint/config/config_file_parser.py

+56-23
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,23 @@
2323
from pylint.lint import PyLinter
2424

2525

26-
class _ConfigurationFileParser:
26+
class _RawConfParser:
2727
"""Class to parse various formats of configuration files."""
2828

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.
3232
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+
"""
3535
parser = configparser.ConfigParser(inline_comment_prefixes=("#", ";"))
36-
3736
# Use this encoding in order to strip the BOM marker, if any.
3837
with open(file_path, encoding="utf_8_sig") as fp:
3938
parser.read_file(fp)
4039

4140
config_content: dict[str, str] = {}
4241
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)
4443
for section in parser.sections():
4544
if ini_file_with_sections and not section.startswith("pylint"):
4645
continue
@@ -58,15 +57,14 @@ def _ini_file_with_sections(file_path: Path) -> bool:
5857
return True
5958
return False
6059

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.
6963
64+
Raises tomllib.TOMLDecodeError
65+
"""
66+
with open(file_path, mode="rb") as fp:
67+
content = tomllib.load(fp)
7068
try:
7169
sections_values = content["tool"]["pylint"]
7270
except KeyError:
@@ -86,12 +84,16 @@ def _parse_toml_file(self, file_path: Path) -> tuple[dict[str, str], list[str]]:
8684
options += [f"--{opt}", values]
8785
return config_content, options
8886

87+
@staticmethod
8988
def parse_config_file(
90-
self, file_path: Path | None
89+
file_path: Path | None, verbose: bool
9190
) -> 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+
"""
9395
if file_path is None:
94-
if self.verbose_mode:
96+
if verbose:
9597
print(
9698
"No config file found, using default configuration", file=sys.stderr
9799
)
@@ -101,13 +103,44 @@ def parse_config_file(
101103
if not file_path.exists():
102104
raise OSError(f"The config file {file_path} doesn't exist!")
103105

104-
if self.verbose_mode:
106+
if verbose:
105107
print(f"Using config file {file_path}", file=sys.stderr)
106108

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."""
107142
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)
111144
except (configparser.Error, tomllib.TOMLDecodeError) as e:
112145
self.linter.add_message("config-parse-error", line=0, args=str(e))
113146
return {}, []

0 commit comments

Comments
 (0)