-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmtp.py
More file actions
170 lines (133 loc) · 4.14 KB
/
mtp.py
File metadata and controls
170 lines (133 loc) · 4.14 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from typing import Any, Callable
from bs4 import BeautifulSoup, Tag
def deleteWord(word: str, text: str):
return text.replace(word, "")
class TextReplacer:
"""
A class used to replace placeholders in a text with actual values.
Attributes:
----------
model : dict[str, Any]
A dictionary containing the placeholders and their corresponding values.
Methods:
-------
__setitem__(key, value)
Sets a new placeholder and its value in the model.
__call__(text: str)
Replaces the placeholders in the text with their actual values.
"""
def __init__(self, model: dict[str, Any]) -> None:
self.model = model
def __setitem__(self, key, value):
self.model[key] = value
def __call__(self, text: str):
for key in self.model:
text = text.replace(f"<-{key}->", str(self.model[key]))
return text
def Condition(text: str):
"""
A function used to evaluate conditional statements in a text.
Parameters:
----------
text : str
The text containing the conditional statements.
Returns:
-------
str
The text with the conditional statements evaluated.
"""
mk = BeautifulSoup(text, "html.parser")
nodes: list[Tag] = mk.find_all("if")
for node in nodes:
if "a" not in node.attrs:
raise ValueError(f"expected an attribute 'a' in node {node!r}")
a = node.attrs["a"]
if "b" not in node.attrs:
raise ValueError(f"expected an attribute 'b' in node {node!r}")
b = node.attrs["b"]
if a == b:
node.replace_with(node.text)
else:
node.decompose()
return str(mk)
def Repetition(text: str):
"""
A function used to repeat a text a specified number of times.
Parameters:
----------
text : str
The text to be repeated.
Returns:
-------
str
The repeated text.
"""
mk = BeautifulSoup(text, "html.parser")
nodes: list[Tag] = mk.find_all("rep")
for node in nodes:
if "amount" not in node.attrs:
raise ValueError(f"expected an attribute 'amount' in node {node!r}")
amount = int(node.attrs["amount"])
node.replace_with(f'{(node.text+"\n")*amount}')
return str(mk)
def CreateElements(text: str, name: str, elements: list[dict[str, Any]]):
"""
A function used to create elements in a text based on a template.
Parameters:
----------
text : str
The text containing the template.
elements : list[dict[str, Any]]
A list of dictionaries containing the data to be used in the template.
Returns:
-------
str
The text with the elements created.
"""
mk = BeautifulSoup(text, "html.parser")
node = mk.find(name)
if node is None:
raise SyntaxError(f"could't find the '{name.upper()}' tag in the html")
result = ""
for element in elements:
result += TextReplacer(element)(node.text)
result += "\n"
node.replace_with(result)
return str(mk)
def useTagFunction(text, funcs: dict[str, Callable]):
"""
A function used to apply functions to tags in a text.
Parameters:
----------
text : str
The text containing the tags.
funcs : dict[str, Callable]
A dictionary containing the functions to be applied to the tags.
Returns:
-------
str
The text with the functions applied to the tags.
"""
mk = BeautifulSoup(text, "html.parser")
nodes: list[Tag] = mk.find_all()
for tag in nodes:
if tag.name in funcs:
tag.replace_with(funcs[tag.name](tag.name, tag.text, tag.attrs))
return str(mk)
def callFunctions(text: str, funcs: dict[str, Callable]):
"""
A function used to call functions in a text.
Parameters:
----------
text : str
The text containing the function calls.
funcs : dict[str, Callable]
A dictionary containing the functions to be called.
Returns:
-------
str
The text with the functions called.
"""
for name in funcs:
text = text.replace(f"{name}()", funcs[name]())
return text