mirror of
https://github.com/Paillat-dev/FABLE.git
synced 2026-01-02 01:06:20 +00:00
fix(.gitignore): add 'settings.yaml' to the list of ignored files
fix(main.py): remove unused import statements and update function name to check_license_agreement fix(main.py): remove unnecessary comments and sleep statements feat(main.py): add check_license_agreement function to prompt user to accept license agreement fix(requirements.txt): add 'watchdog' package to the list of requirements feat(utils/settings.py): create Settings class to handle loading and updating settings from settings.yaml file
This commit is contained in:
64
utils/settings.py
Normal file
64
utils/settings.py
Normal file
@@ -0,0 +1,64 @@
|
||||
import yaml
|
||||
import os
|
||||
import inspect
|
||||
|
||||
from os import getcwd
|
||||
from os.path import abspath, join, exists
|
||||
|
||||
class Settings:
|
||||
def __init__(self):
|
||||
self.forbidden = ['path', 'settings', 'last_modified', 'load_settings', 'create_settings', 'update_settings', 'set_setting']
|
||||
|
||||
self.path = abspath(join(getcwd(), 'settings.yaml'))
|
||||
|
||||
self.last_modified = os.path.getmtime(self.path)
|
||||
self.settings = self.load_settings()
|
||||
self._updating = False
|
||||
|
||||
def load_settings(self):
|
||||
settings = {}
|
||||
if exists(self.path):
|
||||
with open(self.path, 'r', encoding='utf-8') as f:
|
||||
settings = yaml.safe_load(f) or {}
|
||||
else:
|
||||
self.create_settings()
|
||||
for key, value in settings.items():
|
||||
if key not in self.forbidden:
|
||||
self.__dict__[key] = value
|
||||
return settings
|
||||
|
||||
def create_settings(self):
|
||||
with open(self.path, 'w', encoding='utf-8') as f:
|
||||
f.write("")
|
||||
|
||||
def update_settings(self):
|
||||
self.settings = self.load_settings()
|
||||
|
||||
def __getattribute__ (self, name):
|
||||
try:
|
||||
# Try the default behaviour first
|
||||
return super().__getattribute__(name)
|
||||
except AttributeError:
|
||||
# If an AttributeError was raised, return None
|
||||
return None
|
||||
|
||||
def set_setting(self, key, value):
|
||||
if key in self.forbidden:
|
||||
raise ValueError(f'Key "{key}" is forbidden.')
|
||||
self.settings[key] = value
|
||||
with open(self.path, 'w', encoding='utf-8') as f:
|
||||
f.write(yaml.dump(self.settings))
|
||||
self.update_settings()
|
||||
|
||||
settings = Settings()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
#do some testing
|
||||
print(settings.settings)
|
||||
print(settings.path)
|
||||
print(settings.test)
|
||||
print(settings.eee)
|
||||
settings.set_setting('test', 'uu')
|
||||
print(settings.test)
|
||||
settings.set_setting('path', 'AAAAAAAAAAA')
|
||||
Reference in New Issue
Block a user