Skip to content

Commit b6118e6

Browse files
committed
Merge branch 'main' into fix-typing
2 parents 93d93dc + 54c6763 commit b6118e6

File tree

18 files changed

+133
-127
lines changed

18 files changed

+133
-127
lines changed

.github/workflows/themes-screenshot-on-pr.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ jobs:
3131
do
3232
if test -f "$dir/theme.yaml"; then
3333
# Keep only folder name
34-
theme=`basename ${dir%*/}`
34+
theme=`basename "${dir%*/}"`
3535
3636
# Setup selected theme in config.yaml
3737
echo "Using theme $theme"
38-
sed -i "/THEME:/c\ THEME: $theme" config.yaml
38+
sed -i '/THEME:/c\ THEME: "'"$theme"'"' config.yaml
3939
4040
# For tests there is no real HW: use simulated LCD mode
4141
# Check if theme is for 5"
42-
orientation=$(grep 'DISPLAY_SIZE' $dir/theme.yaml | sed 's/ //g')
42+
orientation=$(grep 'DISPLAY_SIZE' "$dir/theme.yaml" | sed 's/ //g')
4343
if [ "$orientation" == "DISPLAY_SIZE:5\"" ]; then
4444
sed -i "/REVISION:/c\ REVISION: SIMU5" config.yaml
4545
else
@@ -70,4 +70,4 @@ jobs:
7070
name: themes-screenshots
7171
path: |
7272
screenshot-*.png
73-
diff-*.png
73+
diff-*.png

.github/workflows/themes-screenshot-on-push.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ jobs:
4242
do
4343
if test -f "$dir/theme.yaml"; then
4444
# Keep only folder name
45-
theme=`basename ${dir%*/}`
45+
theme=`basename "${dir%*/}"`
4646
4747
# Setup selected theme in config.yaml
4848
echo "Using theme $theme"
49-
sed -i "/THEME:/c\ THEME: $theme" config.yaml
50-
49+
sed -i '/THEME:/c\ THEME: "'"$theme"'"' config.yaml
50+
5151
# For tests there is no real HW: use simulated LCD mode
5252
# Check if theme is for 5"
53-
orientation=$(grep 'DISPLAY_SIZE' $dir/theme.yaml | sed 's/ //g')
53+
orientation=$(grep 'DISPLAY_SIZE' "$dir/theme.yaml" | sed 's/ //g')
5454
if [ "$orientation" == "DISPLAY_SIZE:5\"" ]; then
5555
sed -i "/REVISION:/c\ REVISION: SIMU5" config.yaml
5656
else

configure.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import psutil
4141
import ruamel.yaml
4242
import sv_ttk
43+
from pathlib import Path
4344
from PIL import Image
4445
from serial.tools.list_ports import comports
4546
from tktooltip import ToolTip
@@ -92,11 +93,12 @@
9293
"STUB": "Fake random data", "STATIC": "Fake static data"}
9394
reverse_map = {False: "classic", True: "reverse"}
9495

95-
themes_dir = 'res/themes'
96+
MAIN_DIRECTORY = str(Path(__file__).parent.resolve()) + "/"
97+
THEMES_DIR = MAIN_DIRECTORY + 'res/themes'
9698

9799

98100
def get_theme_data(name: str):
99-
dir = os.path.join(themes_dir, name)
101+
dir = os.path.join(THEMES_DIR, name)
100102
# checking if it is a directory
101103
if os.path.isdir(dir):
102104
# Check if a theme.yaml file exists
@@ -111,7 +113,7 @@ def get_theme_data(name: str):
111113

112114
def get_themes(size: str):
113115
themes = []
114-
for filename in os.listdir(themes_dir):
116+
for filename in os.listdir(THEMES_DIR):
115117
theme_data = get_theme_data(filename)
116118
if theme_data and theme_data['display'].get("DISPLAY_SIZE", '3.5"') == size:
117119
themes.append(filename)
@@ -155,7 +157,7 @@ def __init__(self):
155157
self.window = Tk()
156158
self.window.title('Turing System Monitor configuration')
157159
self.window.geometry("770x570")
158-
self.window.iconphoto(True, PhotoImage(file="res/icons/monitor-icon-17865/64.png"))
160+
self.window.iconphoto(True, PhotoImage(file=MAIN_DIRECTORY + "res/icons/monitor-icon-17865/64.png"))
159161
# When window gets focus again, reload theme preview in case it has been updated by theme editor
160162
self.window.bind("<FocusIn>", self.on_theme_change)
161163
self.window.after(0, self.on_fan_speed_update)
@@ -266,9 +268,9 @@ def run(self):
266268

267269
def load_theme_preview(self):
268270
try:
269-
theme_preview = Image.open("res/themes/" + self.theme_cb.get() + "/preview.png")
271+
theme_preview = Image.open(MAIN_DIRECTORY + "res/themes/" + self.theme_cb.get() + "/preview.png")
270272
except:
271-
theme_preview = Image.open("res/docs/no-preview.png")
273+
theme_preview = Image.open(MAIN_DIRECTORY + "res/docs/no-preview.png")
272274
finally:
273275
if theme_preview.width > theme_preview.height:
274276
theme_preview = theme_preview.resize((300, 200), Image.Resampling.LANCZOS)
@@ -290,7 +292,7 @@ def load_theme_preview(self):
290292
self.theme_author.place(x=10, y=self.theme_preview_img.height() + 15)
291293

292294
def load_config_values(self):
293-
with open("config.yaml", "rt", encoding='utf8') as stream:
295+
with open(MAIN_DIRECTORY + "config.yaml", "rt", encoding='utf8') as stream:
294296
self.config, ind, bsi = ruamel.yaml.util.load_yaml_guess_indent(stream)
295297

296298
# Check if theme is valid
@@ -403,14 +405,14 @@ def on_theme_change(self, e=None):
403405
self.load_theme_preview()
404406

405407
def on_theme_editor_click(self):
406-
subprocess.Popen(os.path.join(os.getcwd(), "theme-editor.py") + " \"" + self.theme_cb.get() + "\"", shell=True)
408+
subprocess.Popen(MAIN_DIRECTORY + "theme-editor.py" + " \"" + self.theme_cb.get() + "\"", shell=True)
407409

408410
def on_save_click(self):
409411
self.save_config_values()
410412

411413
def on_saverun_click(self):
412414
self.save_config_values()
413-
subprocess.Popen(os.path.join(os.getcwd(), "main.py"), shell=True)
415+
subprocess.Popen(MAIN_DIRECTORY + "main.py", shell=True)
414416
self.window.destroy()
415417

416418
def on_brightness_change(self, e=None):

library/config.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import os
2222
import queue
2323
import sys
24-
24+
from pathlib import Path
2525
import yaml
2626

2727
from library.log import logger
@@ -34,8 +34,10 @@ def load_yaml(configfile):
3434

3535

3636
PATH = sys.path[0]
37-
CONFIG_DATA = load_yaml("config.yaml")
38-
THEME_DEFAULT = load_yaml("res/themes/default.yaml")
37+
MAIN_DIRECTORY = Path(__file__).parent.parent.resolve()
38+
FONTS_DIR = str(MAIN_DIRECTORY / "res" / "fonts") + "/"
39+
CONFIG_DATA = load_yaml(MAIN_DIRECTORY / "config.yaml")
40+
THEME_DEFAULT = load_yaml(MAIN_DIRECTORY / "res/themes/default.yaml")
3941
THEME_DATA = None
4042

4143

@@ -51,10 +53,10 @@ def copy_default(default, theme):
5153
def load_theme():
5254
global THEME_DATA
5355
try:
54-
theme_path = "res/themes/" + CONFIG_DATA['config']['THEME'] + "/"
55-
logger.info("Loading theme %s from %s" % (CONFIG_DATA['config']['THEME'], theme_path + "theme.yaml"))
56-
THEME_DATA = load_yaml(theme_path + "theme.yaml")
57-
THEME_DATA['PATH'] = theme_path
56+
theme_path = Path("res/themes/" + CONFIG_DATA['config']['THEME'])
57+
logger.info("Loading theme %s from %s" % (CONFIG_DATA['config']['THEME'], theme_path / "theme.yaml"))
58+
THEME_DATA = load_yaml(MAIN_DIRECTORY / theme_path / "theme.yaml")
59+
THEME_DATA['PATH'] = str(MAIN_DIRECTORY / theme_path) + "/"
5860
except:
5961
logger.error("Theme not found or contains errors!")
6062
try:

library/display.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def display_static_text(self):
128128
y=config.THEME_DATA['static_text'][text].get("Y", 0),
129129
width=config.THEME_DATA['static_text'][text].get("WIDTH", 0),
130130
height=config.THEME_DATA['static_text'][text].get("HEIGHT", 0),
131-
font=config.THEME_DATA['static_text'][text].get("FONT", "roboto-mono/RobotoMono-Regular.ttf"),
131+
font=config.FONTS_DIR + config.THEME_DATA['static_text'][text].get("FONT", "roboto-mono/RobotoMono-Regular.ttf"),
132132
font_size=config.THEME_DATA['static_text'][text].get("FONT_SIZE", 10),
133133
font_color=config.THEME_DATA['static_text'][text].get("FONT_COLOR", (0, 0, 0)),
134134
background_color=config.THEME_DATA['static_text'][text].get("BACKGROUND_COLOR", (255, 255, 255)),

library/lcd/lcd_comm.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def DisplayText(
225225
y: int = 0,
226226
width: int = 0,
227227
height: int = 0,
228-
font: str = "roboto-mono/RobotoMono-Regular.ttf",
228+
font: str = "./res/fonts/roboto-mono/RobotoMono-Regular.ttf",
229229
font_size: int = 20,
230230
font_color: Color = (0, 0, 0),
231231
background_color: Color = (255, 255, 255),
@@ -262,11 +262,11 @@ def DisplayText(
262262
text_image = self.open_image(background_image)
263263

264264
# Get text bounding box
265-
fontdata = self.open_font(font, font_size)
265+
ttfont = self.open_font(font, font_size)
266266
d = ImageDraw.Draw(text_image)
267267

268268
if width == 0 or height == 0:
269-
left, top, right, bottom = d.textbbox((x, y), text, font=fontdata, align=align, anchor=anchor)
269+
left, top, right, bottom = d.textbbox((x, y), text, font=ttfont, align=align, anchor=anchor)
270270

271271
# textbbox may return float values, which is not good for the bitmap operations below.
272272
# Let's extend the bounding box to the next whole pixel in all directions
@@ -290,7 +290,7 @@ def DisplayText(
290290
y = top
291291

292292
# Draw text onto the background image with specified color & font
293-
d.text((x, y), text, font=fontdata, fill=font_color, align=align, anchor=anchor)
293+
d.text((x, y), text, font=ttfont, fill=font_color, align=align, anchor=anchor)
294294

295295
# Restrict the dimensions if they overflow the display size
296296
left = max(left, 0)
@@ -360,6 +360,8 @@ def DisplayLineGraph(self, x: int, y: int, width: int, height: int,
360360
line_width: int = 2,
361361
graph_axis: bool = True,
362362
axis_color: Color = (0, 0, 0),
363+
axis_font: str = "./res/fonts/roboto/Roboto-Black.ttf",
364+
axis_font_size: int = 10,
363365
background_color: Color = (255, 255, 255),
364366
background_image: Optional[str] = None):
365367
# Generate a plot graph and display it
@@ -433,15 +435,15 @@ def DisplayLineGraph(self, x: int, y: int, width: int, height: int,
433435
# Draw Legend
434436
draw.line([0, 0, 1, 0], fill=axis_color)
435437
text = f"{int(max_value)}"
436-
fontdata = self.open_font("roboto/Roboto-Black.ttf", 10)
437-
_, top, right, bottom = fontdata.getbbox(text)
438+
ttfont = self.open_font(axis_font, axis_font_size)
439+
_, top, right, bottom = ttfont.getbbox(text)
438440
draw.text((2, 0 - top), text,
439-
font=fontdata, fill=axis_color)
441+
font=ttfont, fill=axis_color)
440442

441443
text = f"{int(min_value)}"
442-
_, top, right, bottom = fontdata.getbbox(text)
444+
_, top, right, bottom = ttfont.getbbox(text)
443445
draw.text((width - 1 - right, height - 2 - bottom), text,
444-
font=fontdata, fill=axis_color)
446+
font=ttfont, fill=axis_color)
445447

446448
self.DisplayPILImage(graph_image, x, y)
447449

@@ -479,7 +481,7 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
479481
value: int = 50,
480482
text: Optional[str] = None,
481483
with_text: bool = True,
482-
font: str = "roboto/Roboto-Black.ttf",
484+
font: str = "./res/fonts/roboto/Roboto-Black.ttf",
483485
font_size: int = 20,
484486
font_color: Color = (0, 0, 0),
485487
bar_color: Color = (0, 0, 0),
@@ -650,11 +652,11 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
650652
if with_text:
651653
if text is None:
652654
text = f"{int(pct * 100 + .5)}%"
653-
fontdata = self.open_font(font, font_size)
654-
left, top, right, bottom = fontdata.getbbox(text)
655+
ttfont = self.open_font(font, font_size)
656+
left, top, right, bottom = ttfont.getbbox(text)
655657
w, h = right - left, bottom - top
656658
draw.text((radius - w / 2 + text_offset[0], radius - top - h / 2 + text_offset[1]), text,
657-
font=fontdata, fill=font_color)
659+
font=ttfont, fill=font_color)
658660

659661
if custom_bbox[0] != 0 or custom_bbox[1] != 0 or custom_bbox[2] != 0 or custom_bbox[3] != 0:
660662
bar_image = bar_image.crop(box=custom_bbox)
@@ -671,5 +673,5 @@ def open_image(self, bitmap_path: str) -> Image.Image:
671673

672674
def open_font(self, name: str, size: int) -> ImageFont.FreeTypeFont:
673675
if (name, size) not in self.font_cache:
674-
self.font_cache[(name, size)] = ImageFont.truetype("./res/fonts/" + name, size)
676+
self.font_cache[(name, size)] = ImageFont.truetype(name, size)
675677
return self.font_cache[(name, size)]

library/stats.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def display_themed_value(theme_data, value, min_size=0, unit=''):
108108
y=theme_data.get("Y", 0),
109109
width=theme_data.get("WIDTH", 0),
110110
height=theme_data.get("HEIGHT", 0),
111-
font=theme_data.get("FONT", "roboto-mono/RobotoMono-Regular.ttf"),
111+
font=config.FONTS_DIR + theme_data.get("FONT", "roboto-mono/RobotoMono-Regular.ttf"),
112112
font_size=theme_data.get("FONT_SIZE", 10),
113113
font_color=theme_data.get("FONT_COLOR", (0, 0, 0)),
114114
background_color=theme_data.get("BACKGROUND_COLOR", (255, 255, 255)),
@@ -184,7 +184,7 @@ def display_themed_radial_bar(theme_data, value, min_size=0, unit='', custom_tex
184184
value=value,
185185
bar_color=theme_data.get("BAR_COLOR", (0, 0, 0)),
186186
text=text,
187-
font=theme_data.get("FONT", "roboto-mono/RobotoMono-Regular.ttf"),
187+
font=config.FONTS_DIR + theme_data.get("FONT", "roboto-mono/RobotoMono-Regular.ttf"),
188188
font_size=theme_data.get("FONT_SIZE", 10),
189189
font_color=theme_data.get("FONT_COLOR", (0, 0, 0)),
190190
background_color=theme_data.get("BACKGROUND_COLOR", (0, 0, 0)),
@@ -234,6 +234,8 @@ def display_themed_line_graph(theme_data, values):
234234
line_width=theme_data.get("LINE_WIDTH", 2),
235235
graph_axis=theme_data.get("AXIS", False),
236236
axis_color=theme_data.get("AXIS_COLOR", line_color), # If no color specified, use line color for axis
237+
axis_font=config.FONTS_DIR + theme_data.get("AXIS_FONT", "roboto/Roboto-Black.ttf"),
238+
axis_font_size=theme_data.get("AXIS_FONT_SIZE", 10),
237239
background_color=theme_data.get("BACKGROUND_COLOR", (0, 0, 0)),
238240
background_image=get_theme_file_path(theme_data.get("BACKGROUND_IMAGE", None))
239241
)

main.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import signal
4242
import subprocess
4343
import time
44+
from pathlib import Path
4445
from PIL import Image
4546

4647
if platform.system() == 'Windows':
@@ -68,6 +69,8 @@
6869
# If pystray cannot be loaded do not stop the program, just ignore it. The tray icon will not be displayed.
6970
pass
7071

72+
MAIN_DIRECTORY = str(Path(__file__).parent.resolve()) + "/"
73+
7174
if __name__ == "__main__":
7275

7376
# Apply system locale to this program
@@ -112,7 +115,7 @@ def on_signal_caught(signum, frame=None):
112115

113116
def on_configure_tray(tray_icon, item):
114117
logger.info("Configure from tray icon")
115-
subprocess.Popen(os.path.join(os.getcwd(), "configure.py"), shell=True)
118+
subprocess.Popen(MAIN_DIRECTORY + "configure.py", shell=True)
116119
clean_stop(tray_icon)
117120

118121

@@ -159,7 +162,7 @@ def on_win32_wm_event(hWnd, msg, wParam, lParam):
159162
tray_icon = pystray.Icon(
160163
name='Turing System Monitor',
161164
title='Turing System Monitor',
162-
icon=Image.open("res/icons/monitor-icon-17865/64.png"),
165+
icon=Image.open(MAIN_DIRECTORY + "res/icons/monitor-icon-17865/64.png"),
163166
menu=pystray.Menu(
164167
pystray.MenuItem(
165168
text='Configure',
-2 Bytes
Loading

0 commit comments

Comments
 (0)