-
Notifications
You must be signed in to change notification settings - Fork 0
/
name_space.py
42 lines (31 loc) · 1.23 KB
/
name_space.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
import struct
class NameSpace():
def __init__(self, binary_stream):
self.binary_stream = binary_stream
self.name_space = {}
# Skip start element
self.binary_stream(1)
self.string_block_size = self._indecator()
self.name_space['string_block_size'] = self.string_block_size
while bool(self.binary_stream):
indecator = self._indecator()
if indecator == 15:
index = self._index()
l = self._block_length()
text = self._text(l)
self.name_space[index] = text
else:
raise ValueError("Unknown Indecator!")
def __getitem__(self, index):
return self.name_space[index]
def _indecator(self):
return struct.unpack('B', self.binary_stream(1))[0]
def _index(self):
return struct.unpack('H', self.binary_stream(2))[0]
def _block_length(self):
return struct.unpack('H', self.binary_stream(2))[0]
def _text(self, l):
s = ''
for _ in range(l//self.string_block_size):
s += struct.unpack('s', self.binary_stream(self.string_block_size)[:1])[0].decode(encoding='utf-8')
return s