-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoggle_timer.py
34 lines (26 loc) · 1019 Bytes
/
toggle_timer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from auto_walls import State, get_config, notify
from psutil import Process, pid_exists
from subprocess import Popen
import os
def start_timer(interval: str, do_notify: bool):
script_dir = os.path.dirname(os.path.abspath(__file__))
process = Popen([f"{script_dir}/timer", str(interval)])
state.timer_pid = process.pid
if do_notify:
notify(f"Starting timer process with pid: {process.pid}")
def stop_timer(do_notify: bool, pid: int):
Process(pid).kill()
state.timer_pid = -1
if do_notify:
notify("Ending timer process ..")
if __name__ == '__main__':
state = State()
c = get_config()
try:
if (state.timer_pid and state.timer_pid != -1) and pid_exists(state.timer_pid) \
and os.path.basename(os.readlink(f'/proc/{state.timer_pid}/exe')) == 'timer':
stop_timer(c["notify"], state.timer_pid)
else:
start_timer(c["interval"], c["notify"])
except ValueError:
start_timer(c["interval"], c["notify"])