veilid/veilid-python/demo/config.py

34 lines
690 B
Python
Raw Normal View History

2023-08-02 06:02:33 +00:00
"""Load and save configuration."""
import json
from pathlib import Path
import veilid
2023-08-02 06:02:33 +00:00
KEYFILE = Path(".demokeys")
def read_keys() -> dict:
"""Load the stored keys from disk."""
try:
raw = KEYFILE.read_text()
2023-08-02 06:02:33 +00:00
except FileNotFoundError:
return {
"self": None,
"peers": {},
}
keys = json.loads(raw)
if keys["self"] is not None:
keys["self"] = veilid.KeyPair(keys["self"])
for name, pubkey in keys["peers"].items():
keys["peers"][name] = veilid.PublicKey(pubkey)
return keys
2023-08-02 06:02:33 +00:00
def write_keys(keydata: dict):
"""Save the keys to disk."""
KEYFILE.write_text(json.dumps(keydata, indent=2))