vmmgr/usr/lib/python3.8/vmmgr/config.py

63 lines
1.1 KiB
Python

# -*- coding: utf-8 -*-
import json
import os
from spoc.flock import locked
from .paths import CONF_FILE, CONF_LOCK
data = {}
mtime = None
def load():
global data
global mtime
file_mtime = os.stat(CONF_FILE).st_mtime
if mtime != file_mtime:
with open(CONF_FILE, 'r') as f:
data = json.load(f)
mtime = file_mtime
def save():
global mtime
with open(CONF_FILE, 'w') as f:
json.dump(data, f, sort_keys=True, indent=4)
mtime = os.stat(CONF_FILE).st_mtime
@locked(CONF_LOCK)
def get_apps():
load()
return data['apps']
@locked(CONF_LOCK)
def get_host():
load()
return data['host']
@locked(CONF_LOCK)
def register_app(name, definition):
load()
data['apps'][name] = definition
save()
@locked(CONF_LOCK)
def unregister_app(name):
load()
try:
del data['apps'][name]
save()
except KeyError:
pass
@locked(CONF_LOCK)
def set_host_value(key, value):
load()
data['host'][key] = value
save()
@locked(CONF_LOCK)
def set_app_value(name, key, value):
load()
data['apps'][name][key] = value
save()