Close #14 Make font customizable. Also manage user settings via a settings.json
file. Also improve 'dark' and 'elegant_dark' themes. Finally improve 'Signal's wiki' button behaviour. Also fix a bug in forecast/now view which caused a crash if solar activity was inactive
This commit is contained in:
44
src/settings.py
Normal file
44
src/settings.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import os.path
|
||||
from constants import Constants
|
||||
import json
|
||||
|
||||
|
||||
class Settings:
|
||||
"""Dynamically save and load the settings of the application."""
|
||||
|
||||
def __init__(self):
|
||||
self._dct = {}
|
||||
|
||||
def load(self):
|
||||
"""Load the setiings.json file."""
|
||||
if not os.path.exists(Constants.SETTINGS_FILE):
|
||||
return
|
||||
try:
|
||||
with open(Constants.SETTINGS_FILE, 'r') as settings_file:
|
||||
self._dct = json.load(settings_file)
|
||||
except Exception:
|
||||
pass # Invalid file.
|
||||
|
||||
def save(self, **kwargs):
|
||||
"""Save the settings.json file.
|
||||
|
||||
Also update the current settings specified in kwargs.
|
||||
New settings can be dynamically added via this method."""
|
||||
for k, v in kwargs.items():
|
||||
self._dct[k] = v
|
||||
with open(Constants.SETTINGS_FILE, mode='w') as settings_file:
|
||||
json.dump(
|
||||
self._dct,
|
||||
settings_file,
|
||||
sort_keys=True,
|
||||
indent=4
|
||||
)
|
||||
|
||||
def __getattr__(self, attr):
|
||||
"""Return the corresponding setting.
|
||||
|
||||
Return None if there is no such setting yet."""
|
||||
try:
|
||||
return self._dct[attr]
|
||||
except Exception:
|
||||
return None
|
||||
Reference in New Issue
Block a user