-
Notifications
You must be signed in to change notification settings - Fork 4
/
episode.py
56 lines (42 loc) · 1.28 KB
/
episode.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
import io
import re
import sys
ITEM_RE = re.compile(
br'(\d+):\s+(\d+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(-?\d+)\s+(-?\d+)')
class EpisodeFile:
def __init__(self, title, levels):
if title and title[0] == '"' and title[-1] == '"':
title = title[1:-1]
self.title = title
self.levels = levels
def _strip(line):
start = line.find(b'#')
if start != -1:
line = line[:start]
return line.strip()
def read_from_file(f):
title = None
levels = []
for line in f:
line = _strip(line)
if not line:
continue
# first line is the title
if title is None:
title = line
continue
match = ITEM_RE.match(line)
if match:
key = int(match.group(1))
item_type = match.group(4)
if item_type == b"LEVEL":
filename = match.group(5)
levels.append(filename)
return EpisodeFile(title, levels)
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.title)
print(e.levels)