Skip to content

Commit b1958f4

Browse files
committed
Bug fixes and reformating of the code:
- Deleted a lot of junk loops and try/except blocks
1 parent 19425f0 commit b1958f4

1 file changed

Lines changed: 34 additions & 38 deletions

File tree

main.py

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import winreg
88
import psutil
99
import shutil
10-
from typing import Union, Dict, SupportsInt, AnyStr
10+
from typing import Union, Dict, SupportsInt, AnyStr, Tuple
11+
import ctypes
1112

1213
from pathlib import Path
1314
from pynput import keyboard
@@ -16,7 +17,7 @@
1617

1718
Keys = keyboard.Key
1819
last_btn: Keys
19-
TIME_STEP = 2 # seconds
20+
TIME_STEP = 1 # seconds
2021
STARTUP_SWITCH = True
2122

2223
PATH_APPDATA_LOCAL = Path(os.getenv("LOCALAPPDATA")).resolve()
@@ -53,7 +54,7 @@ def on_release(key):
5354
if key == Keys.backspace and last_btn == Keys.shift_r:
5455
reschanger.set_display_defaults()
5556
write_logs(Exception("Program termination caused by user"))
56-
exit()
57+
os._exit(-1)
5758

5859

5960
def cur_power_state():
@@ -96,19 +97,27 @@ async def switch_rate(current_state, prss: ScreenSettings, psss: ScreenSettings)
9697
async def srr_loop(time_step, prss: ScreenSettings, psss: ScreenSettings):
9798
last_state = cur_power_state()
9899
while True:
99-
try:
100-
await asyncio.sleep(time_step)
101-
current_state = cur_power_state()
102-
if last_state == current_state:
103-
continue
104-
else:
105-
await switch_rate(current_state, prss, psss)
106-
last_state = current_state
107-
except KeyboardInterrupt as k:
108-
write_logs(k)
109-
exit()
110-
except Exception as e:
111-
write_logs(e)
100+
await asyncio.sleep(time_step)
101+
current_state = cur_power_state()
102+
if last_state == current_state:
103+
continue
104+
else:
105+
await switch_rate(current_state, prss, psss)
106+
last_state = current_state
107+
108+
109+
def load_config() -> Tuple[ScreenSettings, ScreenSettings]:
110+
with open(PATH_TO_PROGRAM / "config.json", "r") as config:
111+
stream = config.read()
112+
params = json.JSONDecoder().decode(stream)
113+
114+
powersave_dict = params["powersave-state"]
115+
powersave_state = ScreenSettings(**powersave_dict)
116+
117+
performance_dict = params["performance-state"]
118+
performance_state = ScreenSettings(**performance_dict)
119+
120+
return powersave_state, performance_state
112121

113122

114123
async def main():
@@ -130,29 +139,16 @@ async def main():
130139
params = cur_monitor_specs()
131140
json.dump(params, config, indent=4)
132141

133-
with open(PATH_TO_PROGRAM / "config.json", "r") as config:
134-
stream = config.read()
135-
params = json.JSONDecoder().decode(stream)
136-
137-
powersave_dict = params["powersave-state"]
138-
powersave_state = ScreenSettings(**powersave_dict)
142+
powersave_state, performance_state = load_config()
139143

140-
performance_dict = params["performance-state"]
141-
performance_state = ScreenSettings(**performance_dict)
142-
143-
fatalities = 0
144-
145-
while fatalities <= 5:
146-
try:
147-
with keyboard.Listener(on_press=on_press, on_release=on_release):
148-
await switch_rate(cur_power_state(), performance_state, powersave_state)
149-
await srr_loop(TIME_STEP, performance_state, powersave_state)
150-
except KeyboardInterrupt as k:
151-
write_logs(k)
152-
exit()
153-
except Exception as e:
154-
write_logs(e)
155-
fatalities += 1
144+
try:
145+
with keyboard.Listener(on_press=on_press, on_release=on_release):
146+
await switch_rate(cur_power_state(), performance_state, powersave_state)
147+
await srr_loop(TIME_STEP, performance_state, powersave_state)
148+
except Exception as e:
149+
ctypes.windll.user32.MessageBoxW(None, f"The SRR program terminated with the following error:\n{repr(e)}",
150+
"Error", 0)
151+
write_logs(e)
156152

157153

158154
if __name__ == '__main__':

0 commit comments

Comments
 (0)