This repository has been archived by the owner on Oct 13, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
retroarcher.py
240 lines (191 loc) · 7.18 KB
/
retroarcher.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
#!/usr/bin/env python3
"""
..
retroarcher.py
Responsible for starting RetroArcher.
"""
# future imports
from __future__ import annotations
# standard imports
import argparse
import os
import sys
import time
from typing import Union
# local imports
import pyra
from pyra import config
from pyra import definitions
from pyra import helpers
from pyra import locales
from pyra import logger
from pyra import threads
py_name = 'pyra'
# locales
_ = locales.get_text()
# get logger
log = logger.get_logger(name=py_name)
class IntRange(object):
"""
Custom IntRange class for argparse.
Prevents printing out large list of possible choices for integer ranges.
Parameters
----------
stop : int
Range maximum value.
start : int, default = 0
Range minimum value.
Methods
-------
__call__:
Validate that value is within accepted range.
Examples
--------
>>> IntRange(0, 10)
<retroarcher.IntRange object at 0x...>
"""
def __init__(self, stop: int, start: int = 0,):
"""
Initialize the IntRange class object.
If stop is less than start, the values will be corrected automatically.
"""
if stop < start:
stop, start = start, stop
self.start, self.stop = start, stop
def __call__(self, value: Union[int, str]) -> int:
"""
Validate that value is within accepted range.
Validate the provided value is within the range of the `IntRange()` object.
Parameters
----------
value : Union[int, str]
The value to validate.
Returns
-------
int
The original value.
Raises
------
argparse.ArgumentTypeError
If provided value is outside the accepted range.
Examples
--------
>>> IntRange(0, 10).__call__(5)
5
>>> IntRange(0, 10).__call__(15)
Traceback (most recent call last):
...
argparse.ArgumentTypeError: Value outside of range: (0, 10)
"""
value = int(value)
if value < self.start or value >= self.stop:
raise argparse.ArgumentTypeError(f'Value outside of range: ({self.start}, {self.stop})')
return value
def main():
"""
Application entry point.
Parses arguments and initializes the application.
Examples
--------
>>> if __name__ == "__main__":
... main()
"""
# Fixed paths to RetroArcher
if definitions.Modes.FROZEN: # only when using the pyinstaller build
if definitions.Modes.SPLASH:
import pyi_splash # module cannot be installed outside of pyinstaller builds
pyi_splash.update_text("Attempting to start RetroArcher")
# Set up and gather command line arguments
# todo... fix translations for '--help' command
parser = argparse.ArgumentParser(description=_('RetroArcher is a Python based game streaming server.\n'
'Arguments supplied here are meant to be temporary.'))
parser.add_argument('--config', help=_('Specify a config file to use'))
parser.add_argument('--debug', action='store_true', help=_('Use debug logging level'))
parser.add_argument('--dev', action='store_true', help=_('Start RetroArcher in the development environment'))
parser.add_argument('--docker_healthcheck', action='store_true', help=_('Health check the container and exit'))
parser.add_argument('--nolaunch', action='store_true', help=_('Do not open RetroArcher in browser'))
parser.add_argument('-p', '--port', default=9696, type=IntRange(21, 65535),
help=_('Force RetroArcher to run on a specified port, default=9696')
)
parser.add_argument('-q', '--quiet', action='store_true', help=_('Turn off console logging'))
parser.add_argument('-v', '--version', action='store_true', help=_('Print the version details and exit'))
args = parser.parse_args()
if args.docker_healthcheck:
status = helpers.docker_healthcheck()
exit_code = int(not status)
sys.exit(exit_code)
if args.version:
print('version arg is not yet implemented')
sys.exit()
if args.config:
config_file = args.config
else:
config_file = os.path.join(definitions.Paths.DATA_DIR, definitions.Files.CONFIG)
if args.debug:
pyra.DEBUG = True
if args.dev:
pyra.DEV = True
if args.quiet:
pyra.QUIET = True
# initialize retroarcher
# logging should not occur until after initialize
# any submodules that require translations need to be imported after config is initialize
pyra.initialize(config_file=config_file)
if args.config:
log.info(msg=f"RetroArcher is using custom config file: {config_file}.")
if args.debug:
log.info(msg="RetroArcher will log debug messages.")
if args.dev:
log.info(msg="RetroArcher is running in the dev environment.")
if args.quiet:
log.info(msg="RetroArcher is running in quiet mode. Nothing will be printed to console.")
if args.port:
config.CONFIG['Network']['HTTP_PORT'] = args.port
config.CONFIG.write()
if config.CONFIG['General']['SYSTEM_TRAY']:
from pyra import tray_icon # submodule requires translations so importing after initialization
# also do not import if not required by config options
tray_icon.tray_run_threaded()
# start the webapp
if definitions.Modes.SPLASH: # pyinstaller build only, not darwin platforms
pyi_splash.update_text("Starting the webapp")
time.sleep(3) # show splash screen for a min of 3 seconds
pyi_splash.close() # close the splash screen
from pyra import webapp # import at use due to translations
threads.run_in_thread(target=webapp.start_webapp, name='Flask', daemon=True).start()
# this should be after starting flask app
if config.CONFIG['General']['LAUNCH_BROWSER'] and not args.nolaunch:
url = f"http://127.0.0.1:{config.CONFIG['Network']['HTTP_PORT']}"
helpers.open_url_in_browser(url=url)
wait() # wait for signal
def wait():
"""
Wait for signal.
Endlessly loop while `pyra.SIGNAL = None`.
If `pyra.SIGNAL` is changed to `shutdown` or `restart` `pyra.stop()` will be executed.
If KeyboardInterrupt signal is detected `pyra.stop()` will be executed.
Examples
--------
>>> wait()
"""
from pyra import hardware # submodule requires translations so importing after initialization
log.info("RetroArcher is ready!")
while True: # wait endlessly for a signal
if not pyra.SIGNAL:
hardware.update() # update dashboard resource values
try:
time.sleep(1)
except KeyboardInterrupt:
pyra.SIGNAL = 'shutdown'
else:
log.info(f'Received signal: {pyra.SIGNAL}')
if pyra.SIGNAL == 'shutdown':
pyra.stop()
elif pyra.SIGNAL == 'restart':
pyra.stop(restart=True)
else:
log.error('Unknown signal. Shutting down...')
pyra.stop()
break
if __name__ == "__main__":
main()