forked from LazzaAU/PycharmSettings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
115 lines (85 loc) · 3.27 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
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>
philipp2310 <https://github.com/philipp2310>
retired or
inactive authors: Jierka <https://github.com/jr-k>
maxbachmann <https://github.com/maxbachmann>
"""
from core.Initializer import Initializer
Initializer().initProjectAlice()
import logging.handlers
import signal
import sys
import time
import traceback
from datetime import datetime
from pathlib import Path
from core.util.model import BashFormatting, FileFormatting, HtmlFormatting
from core.util.model.MqttLoggingHandler import MqttLoggingHandler
_logger = logging.getLogger('ProjectAlice')
_logger.setLevel(logging.INFO)
logFileFormatter = FileFormatting.Formatter()
bashFormatter = BashFormatting.Formatter()
htmlFormatter = HtmlFormatting.Formatter()
date = int(datetime.now().strftime('%Y%m%d'))
logsMountpoint = Path(Path(__file__).resolve().parent, 'var', 'logs')
logFileHandler = 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()
mqttHandler = MqttLoggingHandler()
logFileHandler.setFormatter(logFileFormatter)
rotatingHandler.setFormatter(logFileFormatter)
mqttHandler.setFormatter(htmlFormatter)
streamHandler.setFormatter(bashFormatter)
_logger.addHandler(logFileHandler)
_logger.addHandler(rotatingHandler)
_logger.addHandler(streamHandler)
_logger.addHandler(mqttHandler)
def exceptionListener(*exc_info): #NOSONAR
global _logger
_logger.error('[Project Alice] 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
# 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('[Project Alice] Interruption detected, preparing shutdown')
finally:
if projectAlice.isBooted:
projectAlice.onStop()
_logger.info('[Project Alice] Shutdown completed, see you soon!')
if projectAlice.restart:
time.sleep(3)
restart()
RUNNING = False
if __name__ == '__main__':
main()