blob: fe78f05bf4f9d1dafaf020774918d9f9a6d2e0fd (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
import configparser
from io import StringIO
from typing import Any, Type
from .base_parser import BaseParser
class IniParser(BaseParser):
def __init__(self, file_contents: str):
super().__init__(file_contents)
@property
def decode_error(self) -> Type[Exception]:
return configparser.Error
def loads(self, contents: str) -> dict:
parser = configparser.ConfigParser()
parser.read_string(contents)
return self.__create_configparser_dict(parser)
def dumps(self, loaded_contents: dict) -> str:
parser = configparser.ConfigParser()
buffer = StringIO()
parser.read_dict(loaded_contents)
parser.write(buffer)
return buffer.getvalue()
def __create_configparser_dict(self, parser: configparser.ConfigParser) -> dict:
result = {}
items = dict(parser.items())
for section in parser.sections():
result.update({str(section): dict(items[section])})
return result
def get(self, search_key: str) -> Any:
retrieved_value = super().get(search_key)
return (
retrieved_value if type(retrieved_value) is dict else str(retrieved_value)
)
|