1
0
forked from IPKM/nmreval
nmreval/nmreval/configs.py
2022-03-08 10:27:40 +01:00

70 lines
1.7 KiB
Python

import configparser
import pathlib
import pickle
import logging.handlers
__all__ = ['config_paths', 'read_configuration', 'write_configuration', 'allowed_values', 'write_state', 'read_state']
def config_paths() -> pathlib.Path:
# TODO adjust for different OS
searchpaths = ['~/.local/share/auswerten', '~/.auswerten', '/usr/share/nmreval']
path = None
for p in searchpaths:
path = pathlib.Path(p).expanduser()
if path.exists():
break
if path is None:
raise FileNotFoundError('No valid configuration path found')
return path
def read_configuration() -> configparser.ConfigParser:
try:
config_file = config_paths() / 'nmreval.cfg'
if not config_file.exists():
write_configuration({'GUI': {'theme': 'normal', 'color': 'light'}})
# raise FileNotFoundError('Configuration file not found')
#
except FileNotFoundError as e:
raise e
parser = configparser.ConfigParser()
parser.read(config_file)
return parser
def write_configuration(opts: dict):
config_file = config_paths() / 'nmreval.cfg'
parser = configparser.ConfigParser()
parser.read_dict(opts)
with config_file.open('w') as f:
parser.write(f)
allowed_values = {
('GUI', 'theme'): ['normal', 'pokemon'],
('GUI', 'color'): ['light', 'dark'],
}
def write_state(opts: dict):
config_file = config_paths() / 'guistate.ini'
with config_file.open('wb') as f:
pickle.dump(opts, f)
def read_state() -> dict:
config_file = config_paths() / 'guistate.ini'
if not config_file.exists():
return {}
with config_file.open('rb') as f:
return pickle.load(f)