Store settings in $HOME

This commit is contained in:
Timotej Lazar 2023-04-07 13:32:26 +02:00
parent f8c9341315
commit 0afcd33a99

View file

@ -4,7 +4,7 @@ import pathlib
import time import time
def lock(name): def lock(name):
lockfile = pathlib.Path(f'{name}.lock') lockfile = pathlib.Path.home() / f'{name}.lock'
for i in range(5): for i in range(5):
try: try:
lockfile.symlink_to('/dev/null') lockfile.symlink_to('/dev/null')
@ -14,7 +14,7 @@ def lock(name):
raise TimeoutError(f'could not lock {name}') raise TimeoutError(f'could not lock {name}')
def unlock(name): def unlock(name):
lockfile = pathlib.Path(f'{name}.lock') lockfile = pathlib.Path.home() / f'{name}.lock'
lockfile.unlink(missing_ok=True) lockfile.unlink(missing_ok=True)
@contextlib.contextmanager @contextlib.contextmanager
@ -26,12 +26,12 @@ def locked(name):
unlock(name) unlock(name)
def read(name): def read(name):
with open(f'{name}.json', 'a+', encoding='utf-8') as f: with open(pathlib.Path.home() / f'{name}.json', 'a+', encoding='utf-8') as f:
f.seek(0) f.seek(0)
return json.loads(f.read() or '{}') return json.loads(f.read() or '{}')
def write(name, data): def write(name, data):
with open(f'{name}.json', 'w', encoding='utf-8') as f: with open(pathlib.Path.home() / f'{name}.json', 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2) json.dump(data, f, indent=2)
f.close() f.close()