28 lines
782 B
Python
Raw Normal View History

2018-11-01 10:10:35 +01:00
# -*- coding: utf-8 -*-
2018-11-02 19:54:20 +01:00
import fcntl
2018-11-01 10:10:35 +01:00
import json
from .paths import CONF_FILE, CONF_LOCK
2018-11-01 10:10:35 +01:00
class Config:
def __init__(self):
self.load()
def load(self):
2018-11-02 19:54:20 +01:00
# Load configuration from file. Uses file lock as interprocess mutex
with open(CONF_LOCK, 'w') as lock:
2018-11-02 19:54:20 +01:00
fcntl.lockf(lock, fcntl.LOCK_EX)
2018-11-01 10:10:35 +01:00
with open(CONF_FILE, 'r') as f:
self.data = json.load(f)
def save(self):
2018-11-02 19:54:20 +01:00
# Save configuration to a file. Uses file lock as interprocess mutex
with open(CONF_LOCK, 'w') as lock:
2018-11-02 19:54:20 +01:00
fcntl.lockf(lock, fcntl.LOCK_EX)
2018-11-01 10:10:35 +01:00
with open(CONF_FILE, 'w') as f:
json.dump(self.data, f, sort_keys=True, indent=4)
def __getitem__(self, attr):
return self.data[attr]