Coverage for src / c41811 / config / processor / python_literal.py: 100%
29 statements
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-09 01:06 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-09 01:06 +0000
1# cython: language_level = 3 # noqa: ERA001
4"""Python字面量配置文件处理器"""
6import pprint
7from ast import literal_eval
8from typing import Any
9from typing import override
11from .._protocols import SupportsReadAndReadline
12from .._protocols import SupportsWrite
13from ..abc import ABCConfigFile
14from ..basic.core import ConfigFile
15from ..main import BasicLocalFileConfigSL
18class PythonLiteralSL(BasicLocalFileConfigSL):
19 """Python字面量序列化处理器"""
21 @property
22 @override
23 def processor_reg_name(self) -> str:
24 return "python_literal"
26 @property
27 @override
28 def supported_file_patterns(self) -> tuple[str, ...]:
29 return ".python_literal", ".pyl", ".py"
31 supported_file_classes = [ConfigFile] # noqa: RUF012
33 @override
34 def save_file(
35 self, config_file: ABCConfigFile[Any], target_file: SupportsWrite[str], *merged_args: Any, **merged_kwargs: Any
36 ) -> None:
37 with self.raises():
38 target_file.write(pprint.pformat(config_file.config.data, *merged_args, **merged_kwargs))
40 @override
41 def load_file(
42 self, source_file: SupportsReadAndReadline[str], *merged_args: Any, **merged_kwargs: Any
43 ) -> ConfigFile[Any]:
44 with self.raises():
45 data = literal_eval(source_file.read())
47 return ConfigFile(data, config_format=self.reg_name)
50__all__ = ("PythonLiteralSL",)