-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
155 lines (120 loc) · 5.03 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
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
"""
Mimic allows you to use your mobile device as a remote webcam.
Copyright 2021
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import logging
import multiprocessing
import os
from multiprocessing import Event, Pipe, Process
from os import environ, mkdir
from signal import SIGINT, SIGTERM, signal
from sys import stdout
from time import sleep
from types import FrameType
from win32api import GetLastError
from win32event import CreateMutex
from winerror import ERROR_ALREADY_EXISTS
from mimic.Constants import SLEEP_INTERVAL
from mimic.GUI.GUI import GUI
from mimic.Logging.AsyncLoggingHandler import AsyncRotatingFileHanlder
from mimic.Logging.Formatter import log_formatter
from mimic.Logging.TkinterLoggingHandler import TkinterTextHandler
from mimic.Pipeable import LogMessage, StringMessage
from mimic.TrayIcon import TrayIcon
from mimic.Utils.AppData import (initialize_local_app_data,
mkdir_local_app_data, resolve_local_app_data)
from mimic.Utils.Profiler import profile
from mimic.WebServer import webserver_thread_runner
stop_event = Event()
def stop_handler(signal_number: int, frame: FrameType) -> None:
"""
Handle stop signal events.
Args:
signal_number (int): Signal number that originated the signal
frame (FrameType): Current stack frame when signal was raised
"""
stop_event.set()
def main() -> None:
"""Mimic main entrypoint."""
signal(SIGINT, stop_handler)
signal(SIGTERM, stop_handler)
initialize_local_app_data()
tray_icon = TrayIcon(icon_image="assets/favicon.ico",
hover_text="Mimic", stop_event=stop_event)
tray_icon.run()
webserver_pipe, remote_webserver_pipe = Pipe()
server_process = Process(target=webserver_thread_runner, args=(
stop_event, remote_webserver_pipe))
server_process.start()
gui = GUI()
@gui.on('quit')
def on_gui_quit():
stop_event.set()
# Initialize web server logger
mkdir_local_app_data("logs")
webserver_logger = logging.getLogger('mimic.webserver')
_webserver_stdout_handler = logging.StreamHandler(stdout)
_webserver_stdout_handler.setFormatter(log_formatter)
_webserver_stdout_handler.setLevel(
logging.DEBUG
if "PY_ENV" in os.environ and os.environ["PY_ENV"] == "development"
else logging.INFO
)
webserver_logger.addHandler(_webserver_stdout_handler)
_webserver_file_handler = AsyncRotatingFileHanlder(resolve_local_app_data("logs", "webserver.log"))
_webserver_file_handler.setFormatter(log_formatter)
_webserver_file_handler.setLevel(logging.DEBUG)
webserver_logger.addHandler(_webserver_file_handler)
_webserver_tkinter_handler = TkinterTextHandler(gui.debug_log_window.debug_text)
_webserver_tkinter_handler.setFormatter(log_formatter)
_webserver_tkinter_handler.setLevel(
logging.DEBUG
if "PY_ENV" in os.environ and os.environ["PY_ENV"] == "development"
else logging.INFO
)
webserver_logger.addHandler(_webserver_tkinter_handler)
webserver_logger.setLevel(logging.DEBUG)
# Main loop
while True:
if stop_event.is_set():
break
# Get data from web server
if webserver_pipe.poll():
data = webserver_pipe.recv()
if data.isType(LogMessage):
webserver_logger.log(data.level, data.payload)
# Get data from tray icon
if tray_icon.pipe.poll():
message: StringMessage = tray_icon.pipe.recv()
if message == "show_debug_logs":
gui.debug_log_window.show()
if message == "show_qr_code":
gui.main_window.show()
# Update GUI
gui.update_idletasks()
gui.update()
sleep(SLEEP_INTERVAL)
server_process.join()
tray_icon.join()
if __name__ == "__main__":
multiprocessing.freeze_support()
handle = CreateMutex(None, 1, "Mimic Mutex")
if GetLastError() != ERROR_ALREADY_EXISTS:
if "PY_ENV" in environ and environ["PY_ENV"] == "development":
profile(main)
else:
main()