-
-
Notifications
You must be signed in to change notification settings - Fork 346
Expand file tree
/
Copy pathconfig.py
More file actions
84 lines (74 loc) · 3.31 KB
/
config.py
File metadata and controls
84 lines (74 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# SPDX-License-Identifier: GPL-3.0-or-later
#
# turing-smart-screen-python - a Python system monitor and library for USB-C displays like Turing Smart Screen or XuanFang
# https://github.com/mathoudebine/turing-smart-screen-python/
#
# Copyright (C) 2021 Matthieu Houdebine (mathoudebine)
# Copyright (C) 2022 Rollbacke
# Copyright (C) 2022 Ebag333
# Copyright (C) 2025 ColdWindScholar
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
from queue import Queue
import sys
from pathlib import Path
from yaml import safe_load
from library.log import logger
class Config:
def __init__(self):
self.MAIN_DIRECTORY = Path(__file__).parent.parent.resolve()
self.FONTS_DIR = str(self.MAIN_DIRECTORY / "res" / "fonts") + "/"
self.CONFIG_DATA = self.load_yaml(self.MAIN_DIRECTORY / "config.yaml")
self.THEME_DEFAULT = self.load_yaml(self.MAIN_DIRECTORY / "res/themes/default.yaml")
self.THEME_DATA: dict = {}
# Load theme on import
self.load_theme()
# Queue containing the serial requests to send to the screen
self.update_queue = Queue()
@staticmethod
def load_yaml(configfile: str | Path):
with open(configfile, "rt", encoding='utf8') as stream:
return safe_load(stream)
def copy_default(self, default: dict, theme: dict):
"""recursively supply default values into a dict of dicts of dicts ...."""
for k, v in default.items():
if k not in theme:
theme[k] = v
if isinstance(v, dict):
self.copy_default(default[k], theme[k])
def load_theme(self):
try:
theme_path = Path(f"res/themes/{self.CONFIG_DATA['config']['THEME']}")
logger.info(f"Loading theme {self.CONFIG_DATA['config']['THEME']} from {theme_path / 'theme.yaml'}")
self.THEME_DATA = self.load_yaml(self.MAIN_DIRECTORY / theme_path / "theme.yaml")
self.THEME_DATA['PATH'] = str(self.MAIN_DIRECTORY / theme_path) + "/"
except:
logger.error("Theme not found or contains errors!")
logger.exception('load_theme')
try:
sys.exit(0)
except:
os._exit(0)
self.copy_default(self.THEME_DEFAULT, self.THEME_DATA)
def check_theme_compatible(self, display_size: str):
# Check if theme is compatible with hardware revision
if display_size != self.THEME_DATA['display'].get("DISPLAY_SIZE", '3.5"'):
logger.error(
f"The selected theme {self.CONFIG_DATA['config']['THEME']} is not compatible with your display revision {self.CONFIG_DATA["display"]["REVISION"]}")
try:
sys.exit(0)
except:
os._exit(0)
config = Config()