Coverage for savcfg / edit.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-12-10 15:30 +0100

1import abc 

2import json 

3 

4from .core import Group, Primitive 

5 

6 

7class VarsEditor(abc.ABC): 

8 

9 @abc.abstractmethod 

10 def write(self, key: str, value: str): ... 

11 

12 @abc.abstractmethod 

13 def list_vars(self) -> list[str]: ... 

14 

15 @abc.abstractmethod 

16 def read(self, key: str) -> str: ... 

17 

18 

19class ConfigEditor: 

20 def __init__(self, editor: VarsEditor): 

21 self._editor = editor 

22 

23 def list(self) -> list[str]: 

24 return self._editor.list_vars() 

25 

26 def get(self, key: str) -> Group | Primitive: 

27 var = self._editor.read(key) 

28 value = json.loads(var) 

29 if type(value) is dict: 

30 return Group(value) 

31 return value 

32 

33 def set(self, key: str, value: Group | Primitive): 

34 self._editor.write(key, json.dumps(value))