-
Notifications
You must be signed in to change notification settings - Fork 0
/
menubar.py
102 lines (76 loc) · 2.43 KB
/
menubar.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import rumps
from subprocess import Popen
from src.setup_checks import CheckChatdbAccess, CheckConfigExists, CheckOpenaiKey, CheckShortcutExists
from src.runtime_environment import CONFIG_PATH, BOT_POPEN_CMD, INSTRUCTIONS_PATH, WORKING_DIR, DEBUG, POPUP_POPEN_CMD
from src.autoresponder.logger import logger
logger.debug('menubar loading')
class Menubar:
START = 'Start'
RESTART = 'Restart'
STOP = 'Stop'
EDIT_CONFIG = 'Edit Config'
QUIT = 'Quit'
REVEAL_FILES = 'Reveal Files'
rumps.debug_mode(DEBUG)
bot_process = None
# SETUP CHECK
checks = [
CheckConfigExists(config_path=CONFIG_PATH),
CheckOpenaiKey(config_path=CONFIG_PATH),
CheckChatdbAccess(),
CheckShortcutExists()
]
if not all(check.success for check in checks):
logger.debug('Some check(s?) failed... running error_popup')
Popen(POPUP_POPEN_CMD)
# PROGRAM START
def kill_and_null_proc():
global bot_process
if bot_process is not None:
bot_process.kill()
bot_process = None
def on_click_stop(_):
kill_and_null_proc()
start_button = app.menu[Menubar.START]
stop_button = app.menu[Menubar.STOP]
restart_button = app.menu[Menubar.RESTART]
restart_button.set_callback(None)
stop_button.set_callback(None)
start_button.set_callback(on_click_start)
@rumps.clicked(Menubar.EDIT_CONFIG)
def on_click_edit_config(_):
logger.debug(f'{Menubar.EDIT_CONFIG} clicked')
Popen(["open", "-e", CONFIG_PATH])
def on_click_restart(_):
kill_and_null_proc()
on_click_start(_)
@rumps.clicked(Menubar.QUIT)
def clean_up_before_quit(_):
logger.debug(f'{Menubar.QUIT} clicked')
kill_and_null_proc()
rumps.quit_application()
@rumps.clicked(Menubar.REVEAL_FILES)
def clean_reveal_files(_):
logger.debug(f'{Menubar.REVEAL_FILES} clicked')
Popen(["open", "-R", INSTRUCTIONS_PATH])
@rumps.clicked(Menubar.START)
def on_click_start(_):
global bot_process
logger.debug('start clicked')
start_button = app.menu[Menubar.START]
stop_button = app.menu[Menubar.STOP]
restart_button = app.menu[Menubar.RESTART]
restart_button.set_callback(on_click_restart)
stop_button.set_callback(on_click_stop)
start_button.set_callback(None)
bot_process = Popen(BOT_POPEN_CMD, cwd=WORKING_DIR)
app = rumps.App('💬', quit_button=None)
app.menu = [
Menubar.START,
Menubar.STOP,
Menubar.RESTART,
Menubar.EDIT_CONFIG,
Menubar.REVEAL_FILES,
Menubar.QUIT
]
app.run()