forked from project-alice-assistant/ProjectAlice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
101 lines (75 loc) · 2.71 KB
/
main.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
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>
authors: Psycho <https://github.com/Psychokiller1888>
maxbachmann <https://github.com/maxbachmann>
retired or
inactive authors: Jierka <https://github.com/jr-k>
"""
import logging.handlers
import signal
import sys
import traceback
from datetime import datetime
from pathlib import Path
from core.Initializer import Initializer
formatter = logging.Formatter('%(asctime)s [%(threadName)s] - [%(levelname)s] - %(message)s')
_logger = logging.getLogger('ProjectAlice')
_logger.setLevel(logging.INFO)
date = int(datetime.now().strftime('%Y%m%d'))
logsMountpoint = Path(Path(__file__).resolve().parent, 'var', 'logs')
handler = logging.FileHandler(filename=f'{logsMountpoint}/logs.log', mode='w')
rotatingHandler = logging.handlers.RotatingFileHandler(filename=f'{logsMountpoint}/{date}-logs.log', mode='a', maxBytes=100000, backupCount=20)
streamHandler = logging.StreamHandler()
handler.setFormatter(formatter)
rotatingHandler.setFormatter(formatter)
_logger.addHandler(handler)
_logger.addHandler(rotatingHandler)
_logger.addHandler(streamHandler)
def exceptionListener(*exc_info):
global _logger
_logger.error('An unhandled exception occured')
text = ''.join(traceback.format_exception(*exc_info))
_logger.error(f'- Traceback: {text}')
sys.excepthook = exceptionListener
from core.ProjectAlice import ProjectAlice
import subprocess
import time
# noinspection PyUnusedLocal
def stopHandler(signum, frame):
global RUNNING
RUNNING = False
def restart():
global RUNNING
RUNNING = False
def main():
subprocess.run(['clear'])
global RUNNING
RUNNING = True
signal.signal(signal.SIGINT, stopHandler)
signal.signal(signal.SIGTERM, stopHandler)
Initializer().initProjectAlice()
projectAlice = ProjectAlice(restartHandler=restart)
try:
while RUNNING:
time.sleep(0.1)
except KeyboardInterrupt:
_logger.info('Interruption detected')
finally:
projectAlice.onStop()
_logger.info('Project Alice stopped, see you soon!')
if projectAlice.restart:
time.sleep(3)
sys.stdout.flush()
main()
RUNNING = False
if __name__ == '__main__':
main()