forked from taojy123/KeymouseGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeymouseGo.py
148 lines (126 loc) · 4.88 KB
/
KeymouseGo.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
# cython: language_level=3
# !/usr/bin/env python
# Boa:App:BoaApp
import os
import sys
import math
from PySide2.QtWidgets import QApplication, QWidget, QSpinBox
from PySide2.QtCore import Slot, QRect
from PySide2 import QtCore
import UIFunc
import argparse
from Event import ScriptEvent, ScreenWidth as SW, ScreenHeight as SH
from loguru import logger
from assets.plugins.ProcessException import *
def add_lib_path(libpaths):
for libpath in libpaths:
if os.path.exists(libpath) and (libpath not in sys.path):
sys.path.append(libpath)
def to_abs_path(*args):
return os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), *args)
add_lib_path([os.path.join(to_abs_path('plugins'))])
def resize_layout(ui, ratio_w, ratio_h):
ui.resize(ui.width() * ratio_w, ui.height() * ratio_h)
for q_widget in ui.findChildren(QWidget):
q_widget.setGeometry(QRect(q_widget.x() * ratio_w,
q_widget.y() * ratio_h,
q_widget.width() * ratio_w,
q_widget.height() * ratio_h))
q_widget.setStyleSheet('font-size: ' + str(math.ceil(9 * min(ratio_h, ratio_w))) + 'px')
if isinstance(q_widget, QSpinBox):
q_widget.setStyleSheet('padding-left: 7px')
def main():
# 适应高DPI
if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):
QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
app = QApplication(sys.argv)
ui = UIFunc.UIFunc(app)
# 不同分辨率下调节字体大小和窗口大小
ratio_w = SW / 1920
ratio_h = SH / 1080
if ratio_w > 1 and ratio_h > 1:
resize_layout(ui, ratio_w, ratio_h)
ui.setFixedSize(ui.width(), ui.height())
ui.show()
sys.exit(app.exec_())
@logger.catch
def single_run(script_path, run_times=1, speed=100, module_name='Extension'):
@Slot(ScriptEvent)
def on_keyboard_event(event):
key_name = event.action[1].lower()
stop_name = 'f9'
if key_name == stop_name:
logger.debug('break exit!')
os._exit(0)
return True
UIFunc.Recorder.setuphook(commandline=True)
UIFunc.Recorder.set_callback(on_keyboard_event)
try:
for path in script_path:
logger.info('Script path:%s' % path)
events, smodule_name, _ = UIFunc.RunScriptClass.parsescript(path, speed=speed)
extension = UIFunc.RunScriptClass.getextension(
smodule_name if smodule_name is not None else module_name,
runtimes=run_times,
speed=speed)
j = 0
while j < extension.runtimes or extension.runtimes == 0:
logger.info('=========== %d ===========' % j)
try:
if extension.onbeforeeachloop(j):
UIFunc.RunScriptClass.run_script_once(events, extension)
extension.onaftereachloop(j)
j += 1
except BreakProcess:
logger.debug('Break')
j += 1
continue
except EndProcess:
logger.debug('End')
break
extension.onendp()
logger.info('%s run finish' % path)
logger.info('Scripts run finish!')
except Exception as e:
logger.error(e)
raise e
finally:
os._exit(0)
if __name__ == '__main__':
logger.debug(sys.argv)
if len(sys.argv) > 1:
parser = argparse.ArgumentParser()
parser.add_argument('scripts',
help='Path for the scripts',
type=str,
nargs='+'
)
parser.add_argument('-rt', '--runtimes',
help='Run times for the script',
type=int,
default=1
)
parser.add_argument('-sp', '--speed',
help='Run speed for the script, input in percentage form',
type=int,
default=100
)
parser.add_argument('-m', '--module',
help='Extension for the program',
type=str,
default='Extension'
)
args = vars(parser.parse_args())
logger.debug(args)
if args['speed'] <= 0:
logger.warning('Unsupported speed')
else:
single_run(args['scripts'],
run_times=args['runtimes'],
speed=args['speed'],
module_name=args['module']
)
else:
main()