-
Notifications
You must be signed in to change notification settings - Fork 2
/
Parser.py
45 lines (41 loc) · 1.36 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
from Item import *
# Language keywords.
BACKGROUND = "background "
COMMENT = "#"
TEXT = "{0} ".format(ITEM_TYPE_TEXT)
VIDEO = "{0} ".format(ITEM_TYPE_VIDEO)
class Parser(object):
def __init__(self):
self.background = None
self.items = []
def parseBackground(self, ln):
bg = ln.replace(BACKGROUND, "")
bg = bg.strip()
self.background = bg
def parseLine(self, ln):
sln = ln.strip()
# Ignore comments.
if (sln.startswith(COMMENT)):
return
# Parse real data.
if (sln.startswith(BACKGROUND)):
self.parseBackground(sln)
elif (sln.startswith(TEXT)):
self.parseText(sln)
elif (sln.startswith(VIDEO)):
self.parseVideo(sln)
def parseLines(self, lines):
for ln in lines:
self.parseLine(ln)
def parseText(self, ln):
durationText = ln.replace(TEXT, "")
duration, text = durationText.split(" ", 1)
self.items.append(Item(ITEM_TYPE_TEXT, duration, text))
def parseVideo(self, ln):
durationVideo = ln.replace(VIDEO, "")
duration, video = durationVideo.split(" ", 1)
self.items.append(Item(ITEM_TYPE_VIDEO, duration, video))
def printTree(self):
print("Background: '{0}'".format(self.background))
for item in self.items:
print(item)