Fixed various regular expression issues breaking functionality, as well as added better handling of newlines to the thread loop

This commit is contained in:
Jacob Henry 2016-11-29 23:51:34 +00:00
parent 5e4745ffa2
commit aa15cd33b0
2 changed files with 19 additions and 19 deletions

36
kong.py
View File

@ -7,8 +7,8 @@ DEFAULT_SONG = "dk_rap_classic"
#Patterns #Patterns
patterns = {} patterns = {}
patterns['start'] = re.compile(r"#STARTSONG\a+(.*?)") patterns['start'] = re.compile(r"#STARTSONG\s+(.*?)$")
patterns['time'] = re.compile(r"#TIMEPERLINE\a+(\d*)") patterns['time'] = re.compile(r"#TIMEPERLINE\s+(\d*)$")
patterns['rest'] = re.compile(r"#REST") patterns['rest'] = re.compile(r"#REST")
patterns['end'] = re.compile(r"#ENDSONG") patterns['end'] = re.compile(r"#ENDSONG")
@ -21,27 +21,27 @@ f.close()
class SongThread(threading.Thread): class SongThread(threading.Thread):
def __init__(this, replycallback, song_name): def __init__(self, callback, song_name):
this.slack = slack super().__init__()
this.song_name = song_name self.song_name = song_name
this.song_lines = song_lines self.callback = callback
this.delay = 1000 self.delay = 1000
this.daemon = True self.daemon = True
def run(self):
def run(this):
playing = False playing = False
for line in lyric_lines: for line in lyric_lines:
print(line) line = line.strip()
if line == "": if line == "":
continue continue
#Navigate to song start #Navigate to song start
if not playing: if not playing:
m = patterns['start'].match(line) m = patterns['start'].match(line)
print(line)
if m: if m:
if m.group(1) == this.song_name: if m.group(1) == self.song_name:
playing = True playing = True
continue continue
@ -50,13 +50,13 @@ class SongThread(threading.Thread):
#Config #Config
m = patterns['time'].match(line) m = patterns['time'].match(line)
if m: if m:
this.delay = int(m.group(1)) self.delay = int(m.group(1))
continue continue
#Rest line #Rest line
m = patterns['rest'].match(line) m = patterns['rest'].match(line)
if m: if m:
sleep(this.delay / 1000) sleep(self.delay / 1000)
continue continue
#End song #End song
@ -65,11 +65,11 @@ class SongThread(threading.Thread):
return return
#"sing" line #"sing" line
replycallback(line) self.callback(line)
sleep(this.delay / 1000) sleep(self.delay / 1000)
if not playing: if not playing:
replycallback("Could not find song") self.callback("Could not find song")
def getAllTitles(): def getAllTitles():
titles = [] titles = []
@ -80,7 +80,7 @@ def getAllTitles():
return titles return titles
request_pattern = re.compile("kong (.*?)$") request_pattern = re.compile(r"kong\s*(.*?)$")
def handleKongMsg(slack, msg): def handleKongMsg(slack, msg):
#Get text #Get text