Compare commits

..

No commits in common. "ba87abad46824abcbba27c202cd79d2228404bcb" and "a267a679473b4f6504f9e4be6ad4f8597f8c5bdb" have entirely different histories.

2 changed files with 11 additions and 25 deletions

View File

@ -50,7 +50,6 @@ def main():
def _main(): def _main():
args = docopt(doc) args = docopt(doc)
config.init_confdir()
if args["--user"]: if args["--user"]:
return _user(args) return _user(args)
listen = args["-l"] listen = args["-l"]

View File

@ -7,7 +7,7 @@ from functools import wraps
from hashlib import sha256 from hashlib import sha256
from pathlib import Path, PurePath from pathlib import Path, PurePath
from time import time from time import time
from contextlib import suppress
import msgspec import msgspec
@ -37,22 +37,6 @@ config = None
conffile = None conffile = None
def init_confdir():
if p := os.environ.get("CISTA_HOME"):
home = Path(p)
else:
xdg = os.environ.get("XDG_CONFIG_HOME")
home = (
Path(xdg).expanduser() / "cista" if xdg else Path.home() / ".config/cista"
)
if not home.exists()
home.mkdir(parents=True, exist_ok=True)
home.chmod(0o700)
global conffile
conffile = home / "db.toml"
def derived_secret(*params, len=8) -> bytes: def derived_secret(*params, len=8) -> bytes:
"""Used to derive secret keys from the main secret""" """Used to derive secret keys from the main secret"""
# Each part is made the same length by hashing first # Each part is made the same length by hashing first
@ -78,8 +62,8 @@ def dec_hook(typ, obj):
def config_update(modify): def config_update(modify):
global config global config
if conffile is None: if not conffile.exists():
init_confdir() conffile.parent.mkdir(parents=True, exist_ok=True)
tmpname = conffile.with_suffix(".tmp") tmpname = conffile.with_suffix(".tmp")
try: try:
f = tmpname.open("xb") f = tmpname.open("xb")
@ -93,6 +77,10 @@ def config_update(modify):
old = conffile.read_bytes() old = conffile.read_bytes()
c = msgspec.toml.decode(old, type=Config, dec_hook=dec_hook) c = msgspec.toml.decode(old, type=Config, dec_hook=dec_hook)
except FileNotFoundError: except FileNotFoundError:
# No existing config file, make sure we have a folder...
confdir = conffile.parent
confdir.mkdir(parents=True, exist_ok=True)
confdir.chmod(0o700)
old = b"" old = b""
c = None c = None
c = modify(c) c = modify(c)
@ -105,9 +93,7 @@ def config_update(modify):
f.write(new) f.write(new)
f.close() f.close()
if sys.platform == "win32": if sys.platform == "win32":
# Windows doesn't support atomic replace conffile.unlink() # Windows doesn't support atomic replace
with suppress(FileNotFoundError):
conffile.unlink()
tmpname.rename(conffile) # Atomic replace tmpname.rename(conffile) # Atomic replace
except: except:
f.close() f.close()
@ -135,8 +121,9 @@ def modifies_config(modify):
def load_config(): def load_config():
global config, conffile global config, conffile
if conffile is None: conffile = (
init_confdir() Path(os.environ.get("CISTA_HOME")) or Path.home() / ".local/share/cista/db.toml"
)
config = msgspec.toml.decode(conffile.read_bytes(), type=Config, dec_hook=dec_hook) config = msgspec.toml.decode(conffile.read_bytes(), type=Config, dec_hook=dec_hook)