Coverage for src / c41811 / config / processor / python_literal.py: 100%

29 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-06 06:04 +0000

1# cython: language_level = 3 # noqa: ERA001 

2 

3 

4"""Python字面量配置文件处理器""" 

5 

6import pprint 

7from ast import literal_eval 

8from typing import Any 

9from typing import override 

10 

11from .._protocols import SupportsReadAndReadline 

12from .._protocols import SupportsWrite 

13from ..abc import ABCConfigFile 

14from ..basic.core import ConfigFile 

15from ..main import BasicLocalFileConfigSL 

16 

17 

18class PythonLiteralSL(BasicLocalFileConfigSL): 

19 """Python字面量序列化处理器""" 

20 

21 @property 

22 @override 

23 def processor_reg_name(self) -> str: 

24 return "python_literal" 

25 

26 @property 

27 @override 

28 def supported_file_patterns(self) -> tuple[str, ...]: 

29 return ".python_literal", ".pyl", ".py" 

30 

31 supported_file_classes = [ConfigFile] # noqa: RUF012 

32 

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)) 

39 

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()) 

46 

47 return ConfigFile(data, config_format=self.reg_name) 

48 

49 

50__all__ = ("PythonLiteralSL",)