-
Notifications
You must be signed in to change notification settings - Fork 4
/
classes.py
80 lines (74 loc) · 2.64 KB
/
classes.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
from wireutils import format, color_print
import re
class Command:
def __init__(self, cmd, block="chain_command_block", init=False, conditional=False):
self.cmd = cmd
self.cond = conditional
self.init = init
self.block = block
def __str__(self):
return self.cmd
def prettystr(self):
return format("{cmd}{init}{cond}{repeat}",
cmd = self.cmd,
init = "\n - Initialization" if self.init else "",
cond = "\n - Conditional" if self.cond else "",
repeat="\n - Repeating" if self.block == "repeating_command_block" else "")
class FakeCommand:
hasdata_regex = re.compile(r":\d{1,2}$")
def __init__(self, blockname, init):
self.cond = False
self.init = init
if self.hasdata_regex.search(blockname):
datastr = self.hasdata_regex.findall(blockname)
self.data = int(datastr[0][1:])
else: self.data = 0
self.block = self.hasdata_regex.sub("", blockname)
def __str__(self):
return format("{block} {data}",
block=self.block,
data=self.data)
def prettystr(self):
return format("{darkgray}{block}{init}",
block = self,
init = "\n - Initialization" if self.init else "")
class CmdVariable:
def __init__(self, name, replacewith):
self.name = name
self.replacewith = replacewith
self.regex = re.compile(r"\|\$"+name.lower()+r"\||\$"+name.lower()+r"\b", re.IGNORECASE)
def sub(self, string):
return self.regex.sub(self.replacewith, string)
def macrofunc(string, params, args):
for i in range(min(len(args), len(params))):
string = re.sub(r"\|" + params[i] + r"\|", args[i], string)
return string
class CmdMacro:
param = r"\((?:(?:(?:-?\d+(?:\.\d+)?|\"(?:[^\"\\]*(?:\\.)*)*\"),\s*)*(?:-?\d+(?:\.\d+)?|\"(?:[^\"\\]*(?:\\.)*)*\"))?\)"
param_regex = re.compile(param)
def __init__(self, name, params, replacewith, function=macrofunc):
self.name = name
self.replacewith = replacewith
self.params = params
self.regex = re.compile(r"\$"+name.lower()+self.param, re.IGNORECASE)
self.function = function
def sub(self, string):
while self.regex.search(string):
found = self.regex.finditer(string)
for find in found:
params = self.param_regex.search(find.group()).group()[1:-1]
params = re.sub(r",\s", ",", params)
paraml = params.split(",")
parsedparams = []
for i in paraml:
if i:
if i[0] == '"':
i = i[1:-1].replace('\\"', '"').replace('\\\\', '\\')
parsedparams.append(i)
try:
output = self.function(self.replacewith, self.params, parsedparams)
except:
color_print("{params} is not a valid argument list for ${funcname}.", color=ansi_colors.RED, params=params, funcname=self.name)
output = ""
string = string.replace(find.group(), output)
return string