Some checks failed
Build / Test on ubuntu-latest (push) Successful in 3m6s
Lint / lint (push) Successful in 22s
Build / typecheck (ubuntu-latest, 3.9) (push) Successful in 1m49s
Lint / format (push) Successful in 42s
Build / Test on macOS-latest (push) Has been cancelled
Build / Test on windows-latest (push) Has been cancelled
CodeQL / Analyze (python) (push) Failing after 1m31s
47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
import json
|
|
from pathlib import Path
|
|
|
|
from aw_core.dirs import get_config_dir
|
|
|
|
from .profile import profile_from_env, profile_suffix
|
|
|
|
|
|
class Settings:
|
|
def __init__(self, testing: bool):
|
|
# Dir isolation (AW_PROFILE) already separates profiles; the filename
|
|
# suffix is the pre-profile workaround and stays so --testing still
|
|
# finds settings-testing.json. Named profiles get the same shape.
|
|
filename = f"settings{profile_suffix(profile_from_env(testing=testing))}.json"
|
|
self.config_file = Path(get_config_dir("aw-server")) / filename
|
|
self.load()
|
|
|
|
def __getitem__(self, key):
|
|
return self.get(key)
|
|
|
|
def __setitem__(self, key, value):
|
|
return self.set(key, value)
|
|
|
|
def load(self):
|
|
if self.config_file.exists():
|
|
with open(self.config_file) as f:
|
|
self.data = json.load(f)
|
|
else:
|
|
self.data = {}
|
|
|
|
def save(self):
|
|
with open(self.config_file, "w") as f:
|
|
json.dump(self.data, f, indent=4)
|
|
|
|
def get(self, key: str, default=None):
|
|
if not key:
|
|
return self.data
|
|
return self.data.get(key, default)
|
|
|
|
def set(self, key, value):
|
|
if value:
|
|
self.data[key] = value
|
|
else:
|
|
if key in self.data:
|
|
del self.data[key]
|
|
self.save()
|