Added cascade settings loading from ~/.maigret/settings.json and ./settings.json

This commit is contained in:
Soxoj
2021-11-05 13:40:21 +03:00
parent 4f5222df1c
commit f81a500d72
8 changed files with 79 additions and 38 deletions
+44 -15
View File
@@ -1,28 +1,57 @@
import os
import os.path as path
import json
SETTINGS_FILES_PATHS = [
path.join(path.dirname(path.realpath(__file__)), "resources/settings.json"),
'~/.maigret/settings.json',
path.join(os.getcwd(), 'settings.json'),
]
class Settings:
# main maigret setting
retries_count: int
# submit mode settings
presence_strings: list
supposed_usernames: list
def __init__(self, filename):
data = {}
def __init__(self):
pass
try:
with open(filename, "r", encoding="utf-8") as file:
try:
def load(self, paths=None):
was_inited = False
if not paths:
paths = SETTINGS_FILES_PATHS
for filename in paths:
data = {}
try:
with open(filename, "r", encoding="utf-8") as file:
data = json.load(file)
except Exception as error:
raise ValueError(
f"Problem with parsing json contents of "
f"settings file '{filename}': {str(error)}."
)
except FileNotFoundError as error:
raise FileNotFoundError(
f"Problem while attempting to access settings file '{filename}'."
) from error
except FileNotFoundError:
# treast as a normal situation
pass
except Exception as error:
return False, ValueError(
f"Problem with parsing json contents of "
f"settings file '{filename}': {str(error)}."
)
self.__dict__.update(data)
self.__dict__.update(data)
import logging
logging.error(data)
if data:
was_inited = True
return (
was_inited,
f'None of the default settings files found: {", ".join(paths)}',
)
@property
def json(self):