-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.py
101 lines (82 loc) · 3.04 KB
/
scripts.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
import datetime
from collections import Counter
from events import Event, MouseEvent, TimeEvent
class ScriptManager:
templates = {'mouse': {'x': int, 'y': int},
'time': {'t': str, 'h': int, 'm': int, 's': int, 'u': int},
'test': {'x': str, 'y': int, 'z': str, 'ff': float}}
def __init__(self):
self.name = "unnamed_script"
self.eventList = []
def loadScript(self, script_path):
self.eventList.clear()
try:
with open(script_path, 'r') as file:
lines = file.readlines()
for line in lines:
if not self.addEvent(line):
return False
self.name = script_path
except FileNotFoundError:
return False
def saveScript(self, script_path):
try:
with open(script_path, 'x') as file:
file.writelines([f'{str(e)}\n' for e in self.eventList])
return True
except FileExistsError:
return False
def run(self, dommy=None):
print(f'[{datetime.datetime.now().time()}] Script {self.name} start..')
for e in self.eventList:
e.process()
print(f'[{datetime.datetime.now().time()}] Complete')
return True
def addEvent(self, event_string: str) -> bool:
if self._validateEventString(event_string):
self.eventList.append(self._createEvent(event_string))
return True
return False
@staticmethod
def _validateEventString(event_string: str) -> bool:
event_type, *args = event_string.split(';')
args = ";".join(args)
if event_type not in ScriptManager.templates.keys():
return False
template = ScriptManager.templates.get(event_type)
return ScriptManager._validateArgs(args, template)
@staticmethod
def _createEvent(event_string: str) -> Event:
event_type, *args = event_string.split(';')
param_dict = {}
for arg in args:
key, value = arg.split("=")
param_dict[key] = value
param_list = [ScriptManager.templates[event_type][p](param_dict[p])
for p in ScriptManager.templates[event_type]]
e = Event.createEvent({'mouse': MouseEvent,
'time': TimeEvent}[event_type], *param_list)
return e
@staticmethod
def _validateArgs(string: str, template: dict) -> bool:
try:
args = string.split(';')
except ValueError:
return False
keys = []
values = []
for arg in args:
try:
key, value = arg.split("=")
except ValueError:
return False
keys.append(key)
values.append(value)
if Counter(keys) != Counter(template.keys()):
return False
for idx, value in enumerate(values):
try:
template[keys[idx]](value)
except ValueError:
return False
return True