Skip to content

Commit a18b393

Browse files
authored
Merge pull request #623 from mathoudebine/dev/358-configurepy-mainpy-cannot-be-run-from-another-folder
2 parents d90560d + 6c71bc5 commit a18b393

File tree

11 files changed

+55
-46
lines changed

11 files changed

+55
-46
lines changed

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

Lines changed: 3 additions & 3 deletions
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

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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 11 additions & 9 deletions
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

Lines changed: 9 additions & 7 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def DisplayText(
211211
y: int = 0,
212212
width: int = 0,
213213
height: int = 0,
214-
font: str = "roboto-mono/RobotoMono-Regular.ttf",
214+
font: str = "./res/fonts/roboto-mono/RobotoMono-Regular.ttf",
215215
font_size: int = 20,
216216
font_color: Tuple[int, int, int] = (0, 0, 0),
217217
background_color: Tuple[int, int, int] = (255, 255, 255),
@@ -252,7 +252,7 @@ def DisplayText(
252252

253253
# Get text bounding box
254254
if (font, font_size) not in self.font_cache:
255-
self.font_cache[(font, font_size)] = ImageFont.truetype("./res/fonts/" + font, font_size)
255+
self.font_cache[(font, font_size)] = ImageFont.truetype(font, font_size)
256256
font = self.font_cache[(font, font_size)]
257257
d = ImageDraw.Draw(text_image)
258258

@@ -354,6 +354,8 @@ def DisplayLineGraph(self, x: int, y: int, width: int, height: int,
354354
line_width: int = 2,
355355
graph_axis: bool = True,
356356
axis_color: Tuple[int, int, int] = (0, 0, 0),
357+
font: str = "./res/fonts/roboto/Roboto-Black.ttf",
358+
font_size: int = 10,
357359
background_color: Tuple[int, int, int] = (255, 255, 255),
358360
background_image: str = None):
359361
# Generate a plot graph and display it
@@ -432,16 +434,16 @@ def DisplayLineGraph(self, x: int, y: int, width: int, height: int,
432434
# Draw Legend
433435
draw.line([0, 0, 1, 0], fill=axis_color)
434436
text = f"{int(max_value)}"
435-
font = ImageFont.truetype("./res/fonts/" + "roboto/Roboto-Black.ttf", 10)
436-
left, top, right, bottom = font.getbbox(text)
437+
ttfont = ImageFont.truetype(font, font_size)
438+
left, top, right, bottom = ttfont.getbbox(text)
437439
draw.text((2, 0 - top), text,
438-
font=font, fill=axis_color)
440+
font=ttfont, fill=axis_color)
439441

440442
text = f"{int(min_value)}"
441-
font = ImageFont.truetype("./res/fonts/" + "roboto/Roboto-Black.ttf", 10)
442-
left, top, right, bottom = font.getbbox(text)
443+
ttfont = ImageFont.truetype(font, font_size)
444+
left, top, right, bottom = ttfont.getbbox(text)
443445
draw.text((width - 1 - right, height - 2 - bottom), text,
444-
font=font, 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: 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: Tuple[int, int, int] = (0, 0, 0),
485487
bar_color: Tuple[int, int, int] = (0, 0, 0),
@@ -657,7 +659,7 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
657659
if with_text:
658660
if text is None:
659661
text = f"{int(pct * 100 + .5)}%"
660-
font = ImageFont.truetype("./res/fonts/" + font, font_size)
662+
font = ImageFont.truetype(font, font_size)
661663
left, top, right, bottom = font.getbbox(text)
662664
w, h = right - left, bottom - top
663665
draw.text((radius - w / 2 + text_offset[0], radius - top - h / 2 + text_offset[1]), text,

library/stats.py

Lines changed: 2 additions & 2 deletions
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)),

main.py

Lines changed: 5 additions & 2 deletions
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',
69 Bytes
Loading

simple-program.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ def sighandler(signum, frame):
122122

123123
# Display custom text with solid background
124124
lcd_comm.DisplayText("Custom italic multiline text\nright-aligned", 5, 120,
125-
font="roboto/Roboto-Italic.ttf",
125+
font="res/fonts/roboto/Roboto-Italic.ttf",
126126
font_size=20,
127127
font_color=(0, 0, 255),
128128
background_color=(255, 255, 0),
129129
align='right')
130130

131131
# Display custom text with transparent background
132132
lcd_comm.DisplayText("Transparent bold text", 5, 180,
133-
font="geforce/GeForce-Bold.ttf",
133+
font="res/fonts/geforce/GeForce-Bold.ttf",
134134
font_size=30,
135135
font_color=(255, 255, 255),
136136
background_image=background)
@@ -140,7 +140,7 @@ def sighandler(signum, frame):
140140
while not stop:
141141
start = time.perf_counter()
142142
lcd_comm.DisplayText(str(datetime.now().time()), 160, 2,
143-
font="roboto/Roboto-Bold.ttf",
143+
font="res/fonts/roboto/Roboto-Bold.ttf",
144144
font_size=20,
145145
font_color=(255, 0, 0),
146146
background_image=background)
@@ -177,7 +177,7 @@ def sighandler(signum, frame):
177177
value=bar_value,
178178
bar_color=(255, 255, 0),
179179
text=f"{10 * int(bar_value / 10)}°C",
180-
font="geforce/GeForce-Bold.ttf",
180+
font="res/fonts/geforce/GeForce-Bold.ttf",
181181
font_size=20,
182182
font_color=(255, 255, 0),
183183
background_image=background)

0 commit comments

Comments
 (0)