-
Notifications
You must be signed in to change notification settings - Fork 5
/
start.py
240 lines (188 loc) · 6.85 KB
/
start.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import json
import os
import sys
import threading
import traceback
import uuid
import nest_asyncio
import sentry_sdk
from prompt_toolkit.application import Application, DummyApplication, get_app
from prompt_toolkit.formatted_text import HTML
from prompt_toolkit.layout import Layout, DynamicContainer, FloatContainer, \
Float, FormattedTextControl
from prompt_toolkit.layout.containers import HSplit, VSplit, Window, WindowAlign
from prompt_toolkit.shortcuts import set_title
from prompt_toolkit.widgets import Button
from prompt_toolkit.widgets import Frame
import menus
from riitag import oauth2, user, watcher, presence, preferences
from riitag.util import get_cache
nest_asyncio.apply()
def on_error(exc_type, exc_value, exc_traceback):
app: RiiTagApplication = get_app()
if not isinstance(app, DummyApplication):
sys.__excepthook__(exc_type, exc_value, exc_traceback)
app.invalidate()
app.show_message(
'Whoops!',
'An unexpected error has occured.\n'
'The exception will be reported so the developers can look into it.\n\n'
'Need help? Contact us with this ID so we can help you out:\n' +
(get_user_id() or '<not found>') + '\n\n' +
'Reported exception:\n' +
f'{exc_type.__name__} - {exc_value or "<no except value>"}'
)
return
print()
print(
'+-------------------------------------------------------+\n'
'RiiTag-RPC failed to start :/ \n\n'
'Please contact us with this ID so we can help you out:\n' +
(get_user_id() or '<not found>') + '\n' +
'+-------------------------------------------------------+'
)
print()
print('** Original exception was: **')
traceback.print_exception(exc_value)
print()
print('** Press Enter to exit **')
input()
sys.exit(1)
def on_thread_error(args):
on_error(args.exc_type, args.exc_value, args.exc_traceback)
sys.excepthook = on_error
threading.excepthook = on_thread_error
try:
# prepare cache dir early so we can spot errors sooner
get_cache('')
except OSError:
print('ERROR: Could not create cache directory.')
print('Please check file permissions and try again.')
print()
print('Press enter to exit.')
input()
sys.exit(1)
def is_bundled():
return getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS')
# Get resource when frozen with PyInstaller
# noinspection PyProtectedMember,PyUnresolvedReferences
def resource_path(relative_path):
if is_bundled():
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
def get_user_id():
try:
with open(get_cache('_uid'), 'r') as f:
return f.read().strip()
except FileNotFoundError:
uid = str(uuid.uuid4())
try:
with open(get_cache('_uid'), 'w+') as f:
f.write(uid)
except:
return None
return uid
try:
with open(resource_path('config.json'), 'r') as file:
CONFIG: dict = json.load(file)
except FileNotFoundError:
print('[!] The config file seems to be missing.')
print('[!] Please re-download this program or create it manually.')
input()
sys.exit(1)
VERSION = CONFIG.get('version', '<unknown_version>')
sentry_sdk.init(
"https://[email protected]/5450405",
traces_sample_rate=1.0,
release=f'riitag-rpc@{VERSION}'
)
with sentry_sdk.configure_scope() as scope:
# noinspection PyDunderSlots,PyUnresolvedReferences
scope.user = {'id': get_user_id()}
scope.set_tag('bundled', is_bundled())
class RiiTagApplication(Application):
def __init__(self, *args, **kwargs):
self._current_menu: menus.Menu | None = None
self._float_message_layout = None
self.preferences = preferences.Preferences.load(get_cache('prefs.json'))
self.oauth_client = oauth2.OAuth2Client(CONFIG.get('oauth2'))
self.rpc_handler = presence.RPCHandler(
CONFIG.get('rpc', {}).get('client_id')
)
self.set_menu(menus.SplashScreen)
set_title(self.version_string)
super().__init__(*args, **kwargs,
layout=Layout(DynamicContainer(self._get_layout)),
full_screen=True)
self.token: oauth2.OAuth2Token | None = None
self.user: user.User | None = None
self.riitag_watcher: watcher.RiitagWatcher | None = None
self.oauth_client.start_server(CONFIG.get('port', 4000))
def _get_layout(self):
menu_layout = self._current_menu.get_layout()
if self._current_menu.is_framed:
menu_layout = Frame(menu_layout, title=self.header_string)
if self._float_message_layout:
menu_layout = FloatContainer(
content=menu_layout,
floats=[
Float(
content=self._float_message_layout
)
]
)
return menu_layout
######################
# Overridden Methods #
######################
@property
def key_bindings(self):
return self._current_menu.get_all_kb()
@key_bindings.setter
def key_bindings(self, value):
return
##################
# Custom Methods #
##################
@property
def version_string(self):
return f'RiiTag-RPC v{VERSION}'
@property
def header_string(self):
return f'RiiTag-RPC - {self._current_menu.name}'
def set_menu(self, menu):
if not issubclass(menu, menus.Menu):
raise ValueError('menu must be a subclass of menus.Menu')
if self._current_menu:
self._current_menu.on_exit()
self._current_menu = menu(self)
if hasattr(self, '_is_running'):
self.invalidate()
self._current_menu.on_start()
def show_message(self, title, message, callback=None):
cancel_button = Button('Cancel', handler=lambda: response_received(False))
ok_button = Button('OK', handler=lambda: response_received(True))
def response_received(is_ok):
if callback:
callback(is_ok)
self._float_message_layout = None
self.layout.focus_next()
self.invalidate()
message_frame = Frame(
HSplit([
Window(FormattedTextControl(HTML(message + '\n\n')), align=WindowAlign.CENTER),
VSplit([
cancel_button,
ok_button
], padding=3, align=WindowAlign.CENTER)
], padding=1),
title=title,
)
self._float_message_layout = message_frame
self.layout.focus(cancel_button)
self.invalidate()
def main():
application = RiiTagApplication()
application.run()
if __name__ == "__main__":
main()