-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.py
58 lines (45 loc) · 1.79 KB
/
Parser.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
# This file contains code that adds info about every parser. This info is optional and
# must be edited manually, as it is not present in minecraft.
# This information is usefull when implementing the different parsers,
import json, os
from pathlib import Path
class ParserMapping:
def __init__(self,parsers:list):
self.parsers = parsers
if not os.path.exists(Path(__file__).parent / "./config/parsers.json"):
self.data = {}
else:
with open(Path(__file__).parent / "./config/parsers.json", "r") as fp:
self.data = json.load(fp)
# Add all new parsers to parsers.json
for parser in parsers:
id = str(parser["parser"])
if parser["modifier"]:
id = id + str(parser["modifier"])
if id not in self.data.keys():
self.data[id] = {
"examples" : [],
"regex" : ""
}
with open(Path(__file__).parent / "./config/parsers.json", "w") as fp:
json.dump(self.data,fp,sort_keys=True,indent=1)
def to_json(self):
arr = []
for parser in self.parsers:
id = str(parser["parser"])
if parser["modifier"]:
id = id + str(parser["modifier"])
if self.data[id]:
data = {
"parser" : parser["parser"],
"modifier" : parser["modifier"],
"examples" : self.data[id]["examples"],
}
else:
data = {
"parser" : parser["parser"],
"modifier" : parser["modifier"],
"examples" : [],
}
arr.append(data)
return arr