Skip to content

Commit 5698264

Browse files
Merge pull request #66 from Retro-I/fix-empty-settings-json
Fix empty settings json
2 parents a4a1acf + a84b699 commit 5698264

File tree

8 files changed

+277
-270
lines changed

8 files changed

+277
-270
lines changed

components/dialogs/SettingsLedDialog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(self):
1616
tight=True,
1717
controls=[
1818
ft.Switch(
19-
"LED-Streifen ausschalten",
19+
"LED-Streifen einschalten",
2020
label_style=ft.TextStyle(size=18),
2121
on_change=lambda e: strip.toggle_strip(),
2222
value=strip.is_strip_active(),

helper/Audio.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,20 @@ def play_sound_board(self, src):
108108

109109
def get_default_sound_settings(self) -> dict:
110110
with open(self.AUDIO_SETTINGS_PATH) as file:
111-
return json.load(file)
111+
data = json.load(file)
112+
return data
112113

113114
def get_default_volume(self) -> int:
114115
data = self.get_default_sound_settings()
115116
return int(data["defaultVolume"])
116117

117118
def set_default_volume(self, volume: int):
118-
data = self.get_default_sound_settings()
119-
data["defaultVolume"] = volume
120-
121-
with open(self.AUDIO_SETTINGS_PATH, "w") as file:
122-
file.write(json.dumps(data, sort_keys=True, indent=4, separators=(",", ": ")))
119+
with open(self.AUDIO_SETTINGS_PATH, "r+") as file:
120+
data = json.load(file)
121+
data["defaultVolume"] = volume
122+
file.seek(0)
123+
json.dump(data, file, indent=4)
124+
file.truncate()
123125

124126
def wait(self):
125127
time.sleep(0.5)

helper/Sounds.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def add_favorite_sound(self, item):
3131
file_data.append(item)
3232
file.seek(0)
3333
json.dump(file_data, file, indent=4)
34+
file.truncate()
3435

3536
return 0
3637

helper/Stations.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@ def add_station(self, station):
2525
station["color"] = color_helper.extract_color(station["logo"])
2626

2727
with open(self.STATIONS_SETTINGS_PATH, "r+") as file:
28-
file_data = json.load(file)
29-
file_data.append(station)
28+
data = json.load(file)
29+
data.append(station)
3030
file.seek(0)
31-
json.dump(file_data, file, indent=4)
31+
json.dump(data, file, indent=4)
3232

3333
def delete_station(self, index):
3434
with open(self.STATIONS_SETTINGS_PATH, "r+") as file:
35-
file_data = json.load(file)
36-
file_data.pop(index)
37-
38-
self._write_to_file(file_data, self.STATIONS_SETTINGS_PATH)
35+
data = json.load(file)
36+
data.pop(index)
37+
file.seek(0)
38+
json.dump(data, file)
39+
file.truncate()
3940

4041
def set_favorite_station(self, fav_station):
4142
stations = self.load_radio_stations()
@@ -48,7 +49,10 @@ def set_favorite_station(self, fav_station):
4849
station["favorite"] = True
4950
break
5051

51-
self._write_to_file(stations, self.STATIONS_SETTINGS_PATH)
52+
with open(self.STATIONS_SETTINGS_PATH, "r+") as file:
53+
file.seek(0)
54+
json.dump(stations, file, indent=4)
55+
file.truncate()
5256

5357
def get_favorite_station(self) -> object | None:
5458
stations = self.load_radio_stations()
@@ -64,11 +68,9 @@ def is_default_station_autoplay_enabled(self) -> bool:
6468
return file_data["enableAutoplay"]
6569

6670
def toggle_default_station_autoplay(self):
67-
with open(self.AUDIO_SETTINGS_PATH, "w") as file:
71+
with open(self.AUDIO_SETTINGS_PATH, "r+") as file:
6872
data = json.load(file)
6973
data["enableAutoplay"] = not self.is_default_station_autoplay_enabled()
70-
file.write(json.dumps(data, sort_keys=True, indent=4, separators=(",", ": ")))
71-
72-
def _write_to_file(self, data, path):
73-
with open(path, "w") as file:
74-
file.write(json.dumps(data, sort_keys=True, indent=4, separators=(",", ": ")))
74+
file.seek(0)
75+
json.dump(data, file, indent=4)
76+
file.truncate()

helper/Strip.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ def toggle_strip(self):
9797
if self.is_strip_active():
9898
self.animation.fill(BLACK)
9999
self.animation.freeze()
100-
self.update_settings(is_active=0)
100+
self.update_settings(is_active=True)
101101
else:
102102
self.animation.fill(self.curr_color)
103103
self.animation.resume()
104-
self.update_settings(is_active=1)
104+
self.update_settings(is_active=False)
105105
self.pixels.show()
106106

107107
def get_curr_brightness(self) -> float:
@@ -132,19 +132,21 @@ def disable(self):
132132
self.pixels.show()
133133

134134
def get_strip_settings(self):
135-
with open(self.STRIP_SETTINGS_PATH, newline="") as file:
135+
with open(self.STRIP_SETTINGS_PATH) as file:
136136
return json.load(file)
137137

138-
def update_settings(self, is_active=None, brightness=None, length=None):
138+
def update_settings(
139+
self, is_active: bool | None = None, brightness: float | None = None, length: int = None
140+
):
139141
_is_active = is_active if is_active is not None else self.is_strip_active()
140142
_brightness = brightness if brightness is not None else self.get_curr_brightness()
141143
_length = length if length is not None else self.get_led_length()
142144

143-
data = {
144-
"isStripEnabled": _is_active,
145-
"brightness": _brightness,
146-
"amountLeds": _length,
147-
}
148-
149-
with open(self.STRIP_SETTINGS_PATH, "w") as file:
150-
file.write(json.dumps(data, sort_keys=True, indent=4, separators=(",", ": ")))
145+
with open(self.STRIP_SETTINGS_PATH, "r+") as file:
146+
data = json.load(file)
147+
data["isStripEnabled"] = _is_active
148+
data["brightness"] = _brightness
149+
data["amountLeds"] = _length
150+
file.seek(0)
151+
json.dump(data, file, indent=4)
152+
file.truncate()

helper/SystemHelper.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,36 +58,36 @@ def startup_error(self) -> str | None:
5858

5959
return None
6060

61-
def write_startup_error(self, message):
62-
with open(self.STARTUP_ERROR_PATH, "w") as file:
63-
data = {
64-
"isStartupError": True,
65-
"startupErrorMessage": message,
66-
}
67-
68-
file.write(json.dumps(data, sort_keys=True, indent=4, separators=(",", ": ")))
61+
def write_startup_error(self, message: str = ""):
62+
with open(self.STARTUP_ERROR_PATH, "r+") as file:
63+
data = json.load(file)
64+
data["isStartupError"] = True
65+
data["startupErrorMessage"] = message
66+
file.seek(0)
67+
json.dump(data, file, indent=4)
68+
file.truncate()
6969

7070
def reset_startup_error(self):
71-
with open(self.STARTUP_ERROR_PATH, "w") as file:
72-
data = {
73-
"isStartupError": False,
74-
"startupErrorMessage": "",
75-
}
76-
77-
file.write(json.dumps(data, sort_keys=True, indent=4, separators=(",", ": ")))
71+
with open(self.STARTUP_ERROR_PATH, "r+") as file:
72+
data = json.load(file)
73+
data["isStartupError"] = False
74+
data["startupErrorMessage"] = ""
75+
file.seek(0)
76+
json.dump(data, file, indent=4)
77+
file.truncate()
7878

7979
def is_scrollbar_enabled(self) -> bool:
8080
with open(self.SCROLLBAR_SETTINGS_PATH) as file:
81-
file_data = json.load(file)
82-
return file_data["showScrollbar"]
81+
data = json.load(file)
82+
return bool(data["showScrollbar"])
8383

8484
def toggle_scrollbar_enabled(self):
85-
with open(self.SCROLLBAR_SETTINGS_PATH, "w") as file:
86-
data = {
87-
"showScrollbar": not self.is_scrollbar_enabled(),
88-
}
89-
90-
file.write(json.dumps(data, sort_keys=True, indent=4, separators=(",", ": ")))
85+
with open(self.SCROLLBAR_SETTINGS_PATH, "r+") as file:
86+
data = json.load(file)
87+
data["showScrollbar"] = not self.is_scrollbar_enabled()
88+
file.seek(0)
89+
json.dump(data, file, indent=4)
90+
file.truncate()
9191

9292
def change_revision(self, revision: str):
9393
self._update_process = subprocess.Popen(

settings/favorite-sounds.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[
2-
{
3-
"id": "hog-rider-extra-loud-6366",
4-
"title": "hog rider extra loud",
5-
"url": "https://www.myinstants.com/en/instant/hog-rider-extra-loud-6366",
6-
"mp3": "https://www.myinstants.com/media/sounds/hog-rider-extra-loud.mp3"
7-
}
2+
{
3+
"id": "hog-rider-extra-loud-6366",
4+
"title": "hog rider extra loud",
5+
"url": "https://www.myinstants.com/en/instant/hog-rider-extra-loud-6366",
6+
"mp3": "https://www.myinstants.com/media/sounds/hog-rider-extra-loud.mp3"
7+
}
88
]

0 commit comments

Comments
 (0)