def load(data, encoding=None): """ Load subtitle """ result = [] lines = data.splitlines() pattern = r"(?P<start>\d+:\d+:\d+,\d*) --> (?P<stop>\d+:\d+:\d+,\d*)" pattern = re.compile(pattern) state, text = 0, [] for line in lines: line = to_unicode(line, encoding) if state == 0: state = 1 elif state == 1: m = pattern.match(line) #if m is None: # raise SubtitlesFormatError d = m.groupdict() start = Time.parse(d['start']) stop = Time.parse(d['stop']) state = 2 elif state == 2: if line.strip() == "": result.append(Verse(start, stop, '\n'.join(text))) text = [] state = 0 else: text.append(line.strip()) return result, False
def goto_dlg(self): time = self.gui.do_input_dlg('Run command', 'Enter command', str(self.player.position)) try: self.player.position = Time.parse(time) except ValueError: pass
def testParse(self): self.assertEquals(Time(h=1, m=2, s=3, ms=456), Time.parse('1:2:3.456')) self.assertEquals(Time(h=0, m=2, s=3, ms=456), Time.parse(':2:3.456')) self.assertEquals(Time(h=0, m=2, s=3, ms=456), Time.parse('2:3.456')) self.assertEquals(Time(h=1, m=0, s=3, ms=456), Time.parse('1::3.456')) self.assertEquals(Time(h=0, m=0, s=3, ms=456), Time.parse(':3.456')) self.assertEquals(Time(h=0, m=0, s=3, ms=456), Time.parse('3.456')) self.assertEquals(Time(h=1, m=2, s=0, ms=456), Time.parse('1:2:.456')) self.assertEquals(Time(h=1, m=0, s=0, ms=456), Time.parse('1::.456')) self.assertEquals(Time(h=0, m=0, s=0, ms=456), Time.parse('0:0:.456')) self.assertEquals(Time(h=0, m=0, s=0, ms=456), Time.parse(':0:.456')) self.assertEquals(Time(h=0, m=0, s=0, ms=456), Time.parse('0::.456')) self.assertEquals(Time(h=0, m=0, s=0, ms=456), Time.parse('.456')) self.assertEquals(Time(h=0, m=0, s=0, ms=456), Time.parse(',456')) self.assertEquals(Time(h=0, m=0, s=1234, ms=456), Time.parse('1234,456')) self.assertEquals(Time(h=0, m=0, s=0, ms=0), Time.parse('')) self.assertEquals(Time(h=1, m=2, s=3), Time.parse('1:2:3,')) self.assertEquals(Time(h=1, m=2, s=3), Time.parse('1:2:3')) self.assertEquals(Time(h=1, m=2, s=3), Time.parse('3723')) self.assertEquals(Time(h=0, m=2, s=3), Time.parse('2:3')) self.assertEquals(Time(h=1, m=0, s=3), Time.parse('1::3')) self.assertEquals(Time(h=0, m=0, s=3), Time.parse('::3')) self.assertEquals(Time(h=0, m=0, s=3), Time.parse(':3')) self.assertEquals(Time(h=0, m=0, s=3), Time.parse('3')) self.assertRaises(ValueError, Time.parse, 'ala has a cat') self.assertRaises(ValueError, Time.parse, '1.3:2') self.assertRaises(ValueError, Time.parse, None)