Coverage for savcfg/edit.py: 100%

1 import abc

2 import json

3

4 from .core import Group, Primitive

5

6

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

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