dockge-cli/dockge_cli/components/storage.py

27 lines
805 B
Python
Raw Normal View History

2024-06-29 12:14:18 +00:00
import pathlib
import base64
import yaml
_storagepath = pathlib.Path(__file__).parents[1] / ".temp"
_file = _storagepath / "storage.yaml"
_storagepath.mkdir(exist_ok=True, parents=True)
def set(key: str, value: str):
with open(_file, "w+") as file:
content: dict[str, str] = yaml.load(file, Loader=yaml.SafeLoader) or {}
content.update({ key: base64.b64encode(value.encode()) })
yaml.dump(content, file, Dumper=yaml.SafeDumper)
def get(key: str):
value: str = None
if not _file.exists():
return None
with open(_file, "r") as file:
content: dict[str, str] = yaml.load(file, Loader=yaml.SafeLoader)
value = content.get(key, None)
return base64.b64decode(value).decode() if value is not None else None
def clear():
_file.unlink()