Spotter-VM/basic/srv/vm/mgr/config.py

28 lines
716 B
Python
Raw Normal View History

2018-10-21 10:09:02 +02:00
# -*- coding: utf-8 -*-
2018-10-28 18:19:45 +01:00
import fcntl
2018-10-21 10:09:02 +02:00
import json
CONF_FILE = '/srv/vm/config.json'
2018-10-28 18:19:45 +01:00
# Locking is needed in order to prevent race conditions in WSGI threads
LOCK_FILE = '/srv/vm/config.lock'
2018-10-21 10:09:02 +02:00
class Config:
def __init__(self):
self.load()
def load(self):
2018-10-28 18:19:45 +01:00
with open(LOCK_FILE, 'w') as l:
fcntl.flock(l, fcntl.LOCK_EX)
with open(CONF_FILE, 'r') as f:
self.data = json.load(f)
2018-10-21 10:09:02 +02:00
def save(self):
2018-10-28 18:19:45 +01:00
with open(LOCK_FILE, 'w') as l:
fcntl.flock(l, fcntl.LOCK_EX)
with open(CONF_FILE, 'w') as f:
json.dump(self.data, f, sort_keys=True, indent=4)
2018-10-21 10:09:02 +02:00
def __getitem__(self, attr):
return self.data[attr]