-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoefilter.py
282 lines (239 loc) · 9.5 KB
/
poefilter.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import copy
from collections import defaultdict
from textwrap import dedent, indent
from typing import Any, Callable, DefaultDict, Dict, Iterator, List, Optional, Union
from poepy_core import BaseTypeString
SHOW = 1
HIDE = 2
DISABLE = 3
def CustomSound(name, atype: str = "mp3"):
return f'"{name}.{atype}"'
class Style:
STYLEOPS = {
"SetFontSize",
"SetTextColor",
"SetBackgroundColor",
"SetBorderColor",
"MinimapIcon",
"PlayEffect",
"PlayAlertSound",
"CustomAlertSound",
}
def __init__(
self,
SetFontSize: int = 45,
SetTextColor: str = None,
SetBackgroundColor: str = None,
SetBorderColor: str = None,
MinimapIcon: str = None,
PlayEffect: str = None,
PlayAlertSound: str = None,
CustomAlertSound: str = None,
) -> None:
self.attributes: Dict[str, Any] = {}
self.attributes["SetFontSize"] = SetFontSize
self.attributes["SetTextColor"] = SetTextColor
self.attributes["SetBackgroundColor"] = SetBackgroundColor
self.attributes["SetBorderColor"] = SetBorderColor
self.attributes["MinimapIcon"] = MinimapIcon
self.attributes["PlayEffect"] = PlayEffect
self.attributes["PlayAlertSound"] = PlayAlertSound
self.attributes["CustomAlertSound"] = CustomAlertSound
def textify(self, key: str, to_textify: Any) -> str:
if type(to_textify) == list:
return f"{key} {BaseTypeString(to_textify)}"
elif type(to_textify) == bool:
return f"{key} {('True' if to_textify else 'False')}"
else:
return f"{key} {to_textify}"
def __str__(self) -> str:
ret = ""
for att, val in self.attributes.items():
ret += f"{self.textify(att, val)}\n" if val is not None else ""
return ret.strip()
def __repr__(self) -> str:
return self.__str__()
def changed_copy(self, **kwargs):
ret = copy.deepcopy(self)
for k, v in kwargs.items():
ret.attributes[k] = v
return ret
class Category:
# def __init__(self, comment="Untitled Category", show=True, classes=None, base_type=None, rarity=None, sockets=None, linked_sockets=None, socket_group=None, ):
def __init__(
self,
comment: str = "Untitled Category",
show: int = SHOW,
tags: List[str] = [],
**kwargs,
):
self.comment = comment
self.show = show
self.tags = tags
self.attributes: Dict[str, Any] = {}
for k, v in kwargs.items():
self.attributes[k] = v
def textify(self, key: str, to_textify: Any):
if type(to_textify) == list:
return f"{key} {BaseTypeString(to_textify)}"
elif type(to_textify) == bool:
return f"{key} {('True' if to_textify else 'False')}"
elif type(to_textify) == Style:
return to_textify
else:
return f"{key} {to_textify}"
def rema(self, to_remove):
if type(to_remove) == list:
for entry in to_remove:
del self[to_remove]
else:
del self[to_remove]
def __delitem__(self, key: str) -> None:
del self.attributes[key]
def __getitem__(self, key: str) -> Any:
return self.attributes.get(key)
def get_base_type(self) -> List[str]:
return self.attributes.get("BaseType", [])
def __setitem__(self, key: str, value: Any) -> None:
self.attributes[key] = value
def seta(self, args: List[tuple]) -> None:
for (k, v) in args:
self[k] = v
def geta(self, attrib: str) -> Any:
return self[attrib]
def __str__(self):
ret = f"{('Show' if self.show == SHOW else 'Hide')} # {self.comment}{''.join([f' ~ {tag}' for tag in self.tags])}\n"
for k, v in self.attributes.items():
if v is not None:
ret += indent(f"{self.textify(k,v)}\n", "\t")
return ret
def __repr__(self):
return self.__str__()
class Section:
def __init__(
self,
comment: str = "UNTITLED SECTION",
tags: List[str] = [],
*initial_categories,
) -> None:
self.comment = comment
self.tags = tags
self.categories: Dict[str, Category] = {}
for category in initial_categories:
self.append(category)
def append(self, category) -> None:
self.categories[category.comment] = category
def __len__(self) -> int:
return len(self.categories)
def __getitem__(self, key: str) -> Category:
if key in self.categories:
return self.categories[key]
else:
raise KeyError
def __setitem__(self, key: str, value: Category) -> None:
self.categories[key] = value
def __str__(self) -> str:
ret = ""
ret += f'#{"---"*36}\n'
ret += f"# Section:\t#\t{self.comment}{''.join([f' ~ {tag}' for tag in self.tags])}\n"
ret += f'#{"---"*36}\n'
for category in self.categories.values():
if category.show != DISABLE:
ret += indent(f"{category}", "\t")
return ret
def __repr__(self) -> str:
return self.__str__()
class FilterObj:
def __init__(self, comment: str = "Filter header comment") -> None:
self.sections: Dict[str, Section] = {}
self.styles: List[Style] = []
self.tags: DefaultDict[str, List[Union[Category, Section]]] = defaultdict(list)
self.comment = comment
@staticmethod
def from_file(source_file: str) -> "FilterObj":
current_filter: Optional[FilterObj] = None
with open(source_file, "r") as source_fp:
lines = [l.strip() for l in source_fp]
current_section: Optional[Section] = None
current_category: Optional[Category] = None
for line in lines:
if "# Filter:" in line:
current_filter = FilterObj(line.replace("# Filter:\t#\t", ""))
elif "# Section:" in line:
if all(c is not None for c in {current_category, current_section}):
current_section.append(current_category) # type: ignore
if current_section is not None:
current_filter.append(current_section) # type: ignore
current_section = None
current_category = None
line_args: List[str] = line.replace("# Section:\t#\t", "").split(
" ~ "
)
sec_name, sec_tags = line_args[0], line_args[1:]
current_section = Section(sec_name, tags=sec_tags)
for tag in sec_tags:
current_filter.tags[tag].append(current_section) # type: ignore
elif line.startswith("#"):
pass
else:
val: Union[str, List[str]]
com, val = line.split(" ", 1)
if any(c in com for c in {"Show", "Hide"}):
if all(
c is not None for c in {current_category, current_section}
):
current_section.append(current_category) # type: ignore
current_category = None
val = val.replace("# ", "").split(" ~ ") # type: ignore
cat_name, cat_tags = val[0], val[1:]
current_category = Category(
cat_name, (1 if "Show" in com else 2), tags=cat_tags
)
for tag in cat_tags:
current_filter.tags[tag].append( # type: ignore
current_category
)
else:
if '"' in val:
val = [x.strip('"') for x in val.split('" "')]
current_category.attributes[com] = val # type: ignore
current_section.append(current_category) # type: ignore
current_filter.append(current_section) # type: ignore
if current_filter is None:
current_filter = FilterObj()
return current_filter
def append(self, section: Section) -> None:
self.sections[section.comment] = section
def exception_from_section(self, section_name: str) -> List[Category]:
return [
cat
for cat in self[section_name].categories.values()
if "Exception" in cat.comment
]
def basetypes_from_tag(self, tag: str) -> List[str]:
ret: List[str] = []
for tagged in self.tags[tag]:
if isinstance(tagged, Category):
ret.extend(tagged.get_base_type())
return ret
def apply_to_tag(
self, tag: str, fun: Callable[[Union[Category, Section]], None]
) -> None:
for tagged in self.tags[tag]:
fun(tagged)
def __len__(self) -> int:
return len(self.sections)
def __getitem__(self, key: str) -> Section:
if key in self.sections:
return self.sections[key]
else:
raise KeyError
def __setitem__(self, key: str, value: Section) -> None:
self.sections[key] = value
def __str__(self) -> str:
ret = f"# Filter:\t#\t{self.comment}\n"
for section in self.sections.values():
ret += f"{section}"
return ret
def __repr__(self) -> str:
return self.__str__()