-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathini_parser.py
70 lines (57 loc) · 2.32 KB
/
ini_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
59
60
61
62
63
64
65
66
67
68
69
70
import typing
import messages.base as messages_base
class MessageIniParser:
messages: list[messages_base.Message]
protocol_name: str
hash_bytes_count: int
def __init__(self):
self.messages = []
self.protocol_name = None
self.hash_bytes_count = 8
def parse_meta_section(self, name, items):
key = items[0]
value = items[1]
doc = items[2]
if name.lower() == "protocol":
if key == "name":
print(f"** Define protocol {value}")
self.protocol_name = value
if key == "hash_bytes_count":
print(f"** Hash bytes count = {value}")
self.hash_bytes_count = int(value)
# todo: make exception and enum for hash bytes count
assert(self.hash_bytes_count in [1, 2, 4, 8])
def declare_message(self, name, items):
fields = [messages_base.MessageField(_name, _type, _doc) for _name, _type, _doc in items]
# todo: Make new class UserMessage ihnerited from Message
print(f"Declare {name}")
user_message = messages_base.Message(name, fields)
user_message.is_user_defined = True
self.messages.append(user_message)
def load(self, data: str):
current_section = ""
current_doc_comment = ""
config = {}
for line in data.split("\n"):
line = line.strip()
if not line:
continue
if line[0] == "[" and line[-1] == "]":
current_section = line[1:-1]
current_doc_comment = ""
else:
if line[0] == ";" or line[0] == "#":
current_doc_comment += line[1:] + "\n"
else:
items = list(map(lambda x: x.strip(), line.split("=")))
config.setdefault(current_section, []).append((items[0], items[1], current_doc_comment))
current_doc_comment = ""
for section, values in config.items():
if section[0] == '.':
for value in values:
self.parse_meta_section(section[1:], value)
else:
self.declare_message(section, values)
def load_file(self, filename: str):
with open(filename, "r") as file:
self.load(file.read())