-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodels.py
49 lines (36 loc) · 1.09 KB
/
models.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
import io
import re
import sys
ITEM_RE = re.compile(br'(\d+):\s+(\S+)\s+(\S+)')
class ModelsFile:
def __init__(self, models):
self.models = models
def _strip(line):
start = line.find(b'#')
if start != -1:
line = line[:start]
return line.strip()
def read_from_file(f):
models = []
for line in f:
stripped_line = _strip(line)
if not stripped_line:
continue
match = ITEM_RE.match(stripped_line)
if match:
threedo = match.group(2)
name_comment_start = line.rfind(b'#')
if name_comment_start == -1:
name = "unknown"
else:
name = line[name_comment_start + 1:]
name = name.decode(errors='ignore')
name = name.strip().strip('#"').strip()
models.append((threedo, name))
return ModelsFile(models)
def read_from_bytes(b):
return read_from_file(io.BytesIO(b))
if __name__ == "__main__":
with open(sys.argv[1], 'rt') as f:
e = read_from_bytes(f.read().encode())
print(e.models)