Skip to content

Commit de46331

Browse files
committed
Fix pathlib path handling and theme preview loading
1 parent 1f41e53 commit de46331

File tree

3 files changed

+64
-41
lines changed

3 files changed

+64
-41
lines changed

config.yaml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
---
21
config:
32
# Configuration values to set up basic communication
43
# Set your COM port e.g. COM3 for Windows, /dev/ttyACM0 for Linux...
54
# Use AUTO for COM port auto-discovery (may not work on every setup)
65
# COM_PORT: "/dev/ttyACM0"
76
# COM_PORT: "COM3"
8-
COM_PORT: "AUTO"
7+
COM_PORT: AUTO
98

109
# Theme to use (located in res/themes)
1110
# Use the name of the folder as value
1211
# Choose a theme made for your screen size (see DISPLAY_SIZE inside theme.yaml)
13-
THEME: 3.5inchTheme2
12+
THEME: Gradient
1413

1514
# Hardware sensors reading
1615
# Choose the appropriate method for reading your hardware sensors:
1716
# - PYTHON use Python libraries (psutils, GPUtil...) to read hardware sensors (supports all OS but not all HW)
1817
# - LHM use LibreHardwareMonitor library to read hardware sensors (Windows only - NEEDS ADMIN RIGHTS)
1918
# - STUB / STATIC use random/static data instead of real hardware sensors
2019
# - AUTO use the best method based on your OS: Windows OS will use LHM, other OS will use Python libraries
21-
HW_SENSORS: AUTO
20+
HW_SENSORS: PYTHON
2221

2322
# Network interfaces
2423
# Linux/MacOS interfaces are named "eth0", "wlan0", "wlp1s0", "enp2s0"...
2524
# For Windows use the interfaces pretty name: "Ethernet 2", "Wi-Fi", ...
2625
# Leave the fields empty if the card does not exist on your setup
27-
ETH: "" # Ethernet Card
28-
WLO: "" # Wi-Fi Card
26+
ETH: Ethernet # Ethernet Card
27+
WLO: '' # Wi-Fi Card
2928

3029
# CPU fan
3130
# For Linux/MacOS platforms, the CPU fan is amongst all fan sensors gathered from the motherboard chipset
@@ -41,14 +40,14 @@ config:
4140

4241
# OpenWeatherMap API KEY. Can be obtained by creating a free account on https://home.openweathermap.org/users/sign_up.
4342
# You need to subscribe to the 3.0 OneCallAPI that has 1000 free daily calls
44-
WEATHER_API_KEY: ""
43+
WEATHER_API_KEY: ''
4544
# Location from which to display the weather. Use for example https://www.latlong.net/ to get latitude/longitude
46-
WEATHER_LATITUDE: 45.75
47-
WEATHER_LONGITUDE: 4.85
45+
WEATHER_LATITUDE: '45.75'
46+
WEATHER_LONGITUDE: '4.85'
4847
# Units used to display temperatures (metric - °C, imperial - °F, standard - °K)
4948
WEATHER_UNITS: metric
5049
# Language is used by the API. Find more here https://openweathermap.org/api/one-call-3#multi
51-
WEATHER_LANGUAGE: en
50+
WEATHER_LANGUAGE: de
5251

5352
display:
5453
# Display revision:
@@ -60,7 +59,7 @@ display:
6059
# - WEACT_B for WeAct Studio Display FS V1 0.96"
6160
# - SIMU for simulated display (image written in screencap.png). Width & height will be detected from the theme
6261
# To identify your smart screen: https://github.com/mathoudebine/turing-smart-screen-python/wiki/Hardware-revisions
63-
REVISION: A
62+
REVISION: C_USB
6463

6564
# Display Brightness
6665
# Set this as the desired %, 0 being completely dark and 100 being max brightness

configure.py

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,20 @@
124124
"sk": "Slovak", "sl": "Slovenian", "sp": "Spanish", "sv": "Swedish", "th": "Thai", "tr": "Turkish",
125125
"ua": "Ukrainian", "vi": "Vietnamese", "zu": "Zulu"}
126126

127-
MAIN_DIRECTORY = str(Path(__file__).parent.resolve()) + "/"
128-
THEMES_DIR = MAIN_DIRECTORY + 'res/themes'
127+
MAIN_DIRECTORY = Path(__file__).resolve().parent
128+
THEMES_DIR = MAIN_DIRECTORY / "res/themes"
129129

130-
circular_mask = Image.open(MAIN_DIRECTORY + "res/backgrounds/circular-mask.png")
130+
131+
circular_mask = Image.open(MAIN_DIRECTORY / "res/backgrounds/circular-mask.png")
131132

132133
def get_theme_data(name: str):
133-
dir = os.path.join(THEMES_DIR, name)
134+
dir = THEMES_DIR / name
135+
134136
# checking if it is a directory
135-
if os.path.isdir(dir):
136-
# Check if a theme.yaml file exists
137-
theme = os.path.join(dir, 'theme.yaml')
138-
if os.path.isfile(theme):
139-
# Get display size from theme.yaml
140-
with open(theme, "rt", encoding='utf8') as stream:
137+
if dir.is_dir():
138+
theme = dir / "theme.yaml"
139+
if theme.is_file():
140+
with open(theme, "rt", encoding="utf8") as stream:
141141
theme_data, ind, bsi = ruamel.yaml.util.load_yaml_guess_indent(stream)
142142
return theme_data
143143
return None
@@ -189,8 +189,7 @@ def __init__(self):
189189
self.window = Tk()
190190
self.window.title('Turing System Monitor configuration')
191191
self.window.geometry("820x580")
192-
self.window.iconphoto(True, PhotoImage(file=MAIN_DIRECTORY + "res/icons/monitor-icon-17865/64.png"))
193-
# When window gets focus again, reload theme preview in case it has been updated by theme editor
192+
self.window.iconphoto(True,PhotoImage(file=str(MAIN_DIRECTORY / "res/icons/monitor-icon-17865/64.png"))) # When window gets focus again, reload theme preview in case it has been updated by theme editor
194193
self.window.bind("<FocusIn>", self.on_theme_change)
195194
self.window.after(0, self.on_fan_speed_update)
196195

@@ -313,14 +312,17 @@ def run(self):
313312
def load_theme_preview(self):
314313
theme_data = get_theme_data(self.theme_cb.get())
315314

315+
if theme_data and theme_data['display'].get("DISPLAY_SIZE", '3.5"') == SIZE_2_1_INCH:
316+
theme_preview.paste(circular_mask, mask=circular_mask)
317+
316318
try:
317-
theme_preview = Image.open(MAIN_DIRECTORY + "res/themes/" + self.theme_cb.get() + "/preview.png")
319+
theme_preview = Image.open(MAIN_DIRECTORY / "res" / "themes" / self.theme_cb.get() / "preview.png")
318320

319321
if theme_data['display'].get("DISPLAY_SIZE", '3.5"') == SIZE_2_1_INCH:
320322
# This is a circular screen: apply a circle mask over the preview
321323
theme_preview.paste(circular_mask, mask=circular_mask)
322324
except:
323-
theme_preview = Image.open(MAIN_DIRECTORY + "res/docs/no-preview.png")
325+
theme_preview = Image.open(MAIN_DIRECTORY / "res/docs/no-preview.png")
324326
finally:
325327
theme_preview.thumbnail((320, 480), Image.Resampling.LANCZOS)
326328
self.theme_preview_img = ImageTk.PhotoImage(theme_preview)
@@ -338,7 +340,7 @@ def load_theme_preview(self):
338340
self.theme_author.place(x=10, y=self.theme_preview_img.height() + 15)
339341

340342
def load_config_values(self):
341-
with open(MAIN_DIRECTORY + "config.yaml", "rt", encoding='utf8') as stream:
343+
with open(MAIN_DIRECTORY / "config.yaml", "rt", encoding='utf8') as stream:
342344
self.config, ind, bsi = ruamel.yaml.util.load_yaml_guess_indent(stream)
343345

344346
# Check if theme is valid
@@ -450,7 +452,7 @@ def save_config_values(self):
450452
self.config['display']['DISPLAY_REVERSE'] = [k for k, v in reverse_map.items() if v == self.orient_cb.get()][0]
451453
self.config['display']['BRIGHTNESS'] = int(self.brightness_slider.get())
452454

453-
with open(MAIN_DIRECTORY + "config.yaml", "w", encoding='utf-8') as file:
455+
with open(MAIN_DIRECTORY / "config.yaml", "w", encoding='utf-8') as file:
454456
ruamel.yaml.YAML().dump(self.config, file)
455457

456458
def save_additional_config(self, ping: str, api_key: str, lat: str, long: str, unit: str, lang: str):
@@ -461,7 +463,7 @@ def save_additional_config(self, ping: str, api_key: str, lat: str, long: str, u
461463
self.config['config']['WEATHER_UNITS'] = unit
462464
self.config['config']['WEATHER_LANGUAGE'] = lang
463465

464-
with open(MAIN_DIRECTORY + "config.yaml", "w", encoding='utf-8') as file:
466+
with open(MAIN_DIRECTORY / "config.yaml", "w", encoding='utf-8') as file:
465467
ruamel.yaml.YAML().dump(self.config, file)
466468

467469
def on_theme_change(self, e=None):
@@ -471,25 +473,44 @@ def on_weatherping_click(self):
471473
self.more_config_window.show()
472474

473475
def on_open_theme_folder_click(self):
474-
path = f'"{MAIN_DIRECTORY}res/themes"'
476+
#path = f'"{MAIN_DIRECTORY}res/themes"'
477+
#if platform.system() == "Windows":
478+
# os.startfile(path)
479+
#elif platform.system() == "Darwin":
480+
# subprocess.Popen(["open", path])
481+
#else:
482+
# subprocess.Popen(["xdg-open", path])
483+
path = MAIN_DIRECTORY / "res/themes"
484+
475485
if platform.system() == "Windows":
476486
os.startfile(path)
477487
elif platform.system() == "Darwin":
478-
subprocess.Popen(["open", path])
488+
subprocess.Popen(["open", str(path)])
479489
else:
480-
subprocess.Popen(["xdg-open", path])
490+
subprocess.Popen(["xdg-open", str(path)])
491+
481492

482493
def on_theme_editor_click(self):
483-
subprocess.Popen(
484-
f'"{MAIN_DIRECTORY}{glob.glob("theme-editor.*", root_dir=MAIN_DIRECTORY)[0]}" "{self.theme_cb.get()}"',
485-
shell=True)
494+
theme_editor = next(MAIN_DIRECTORY.glob("theme-editor.*"))
495+
496+
if platform.system() == "Windows":
497+
subprocess.Popen([str(theme_editor), self.theme_cb.get()], shell=True)
498+
else:
499+
subprocess.Popen([str(theme_editor), self.theme_cb.get()])
500+
486501

487502
def on_save_click(self):
488503
self.save_config_values()
489504

490505
def on_saverun_click(self):
491506
self.save_config_values()
492-
subprocess.Popen(f'"{MAIN_DIRECTORY}{glob.glob("main.*", root_dir=MAIN_DIRECTORY)[0]}"', shell=True)
507+
main_file = next(MAIN_DIRECTORY.glob("main.*"))
508+
509+
if platform.system() == "Windows":
510+
subprocess.Popen([str(main_file)], shell=True)
511+
else:
512+
subprocess.Popen([str(main_file)])
513+
493514
self.window.destroy()
494515

495516
def on_brightness_change(self, e=None):

main.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
# If pystray cannot be loaded do not stop the program, just ignore it. The tray icon will not be displayed.
6969
pass
7070

71-
MAIN_DIRECTORY = str(Path(__file__).parent.resolve()) + "/"
71+
MAIN_DIRECTORY = Path(__file__).resolve().parent
7272

7373
if __name__ == "__main__":
7474

@@ -110,17 +110,20 @@ def clean_stop(tray_icon=None):
110110
except:
111111
os._exit(0)
112112

113-
114113
def on_signal_caught(signum, frame=None):
115114
logger.info("Caught signal %d, exiting" % signum)
116115
clean_stop()
117116

118-
119117
def on_configure_tray(tray_icon, item):
120118
logger.info("Configure from tray icon")
121-
subprocess.Popen(f'"{MAIN_DIRECTORY}{glob.glob("configure.*", root_dir=MAIN_DIRECTORY)[0]}"', shell=True)
122-
clean_stop(tray_icon)
123119

120+
configure_file = next(MAIN_DIRECTORY.glob("configure.*"))
121+
122+
if platform.system() == "Windows":
123+
subprocess.Popen([str(configure_file)], shell=True)
124+
else:
125+
subprocess.Popen([str(configure_file)])
126+
clean_stop(tray_icon)
124127

125128
def on_exit_tray(tray_icon, item):
126129
logger.info("Exit from tray icon")
@@ -165,7 +168,7 @@ def on_win32_wm_event(hWnd, msg, wParam, lParam):
165168
tray_icon = pystray.Icon(
166169
name='Turing System Monitor',
167170
title='Turing System Monitor',
168-
icon=Image.open(MAIN_DIRECTORY + "res/icons/monitor-icon-17865/64.png"),
171+
icon=Image.open(MAIN_DIRECTORY / "res/icons/monitor-icon-17865/64.png"),
169172
menu=pystray.Menu(
170173
pystray.MenuItem(
171174
text='Configure',

0 commit comments

Comments
 (0)