-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.py
More file actions
61 lines (48 loc) · 1.89 KB
/
config.py
File metadata and controls
61 lines (48 loc) · 1.89 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Class to handle the configuration for Plaid2Firefly"""
import json
from pathlib import Path
from typing import Any
import logging
_LOGGER = logging.getLogger(__name__)
class Config:
"""Configuration class for Plaid2Firefly"""
def __init__(self) -> None:
self.path = Path("data/config.json")
if not self.path.exists():
_LOGGER.info("Creating configuration file at %s", self.path)
self.path.write_text("{}", encoding="utf-8")
self._load()
def _load(self) -> None:
"""Load the configuration from the JSON file"""
with open(self.path, "r", encoding="utf-8") as f:
self._config = json.load(f)
def get(self, key: str, default=None) -> Any:
"""Get a configuration value, always using the latest available"""
self._load()
return self._config.get(key, default)
def set(self, key: str, value: Any) -> None:
"""Set a configuration value"""
self._config[key] = value
_LOGGER.info("Saving configuration: %s to %s", key, value)
self._save()
def update(self, new_values: dict) -> None:
"""Update multiple configuration values"""
self._config.update(new_values)
self._save()
def delete(self, key: str) -> None:
"""Delete a configuration value"""
if key in self._config:
del self._config[key]
_LOGGER.info("Deleting configuration: %s", key)
self._save()
else:
_LOGGER.warning("Key %s not found in configuration", key)
def reset(self) -> None:
"""Reset the configuration"""
_LOGGER.info("Resetting configuration")
self._config = {}
self._save()
def _save(self) -> None:
"""Save the current configuration to the JSON file"""
with open(self.path, "w", encoding="utf-8") as f:
json.dump(self._config, f, indent=4)