From d5616e3970b5fe3a32f565e26ff776f93a62ac54 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sat, 29 Sep 2007 22:05:03 +0200 Subject: Implemented script writing. Store original case keys for script info for writing. Retain original case in self.format in styles and events, using str.lower() when setting attributes. --- pykfx.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 14 deletions(-) (limited to 'pykfx.py') diff --git a/pykfx.py b/pykfx.py index 0f8d0a0..ded5269 100644 --- a/pykfx.py +++ b/pykfx.py @@ -6,6 +6,8 @@ class ScriptException(Exception): pass class ScriptInfo: def __init__(self, f = None): + # original case keys used for writing + self.ock = {} self.info = {} if f: self.read(f) @@ -21,21 +23,30 @@ class ScriptInfo: while s: if not s.startswith(';'): k, v = s.split(': ', 1) + self.ock[k.lower()] = k self.info[k.lower()] = v s = f.readline().strip() + def write(self, f): + f.write('[Script Info]\n') + f.writelines(['%s: %s\n' % (self.ock[k], self.info[k]) for k in self.info.keys()]) + class Style: - def __init__(self, format = None, s = None): - if format and s: - self.parse(format, s) + def __init__(self, format, s = None): + self.format = format + if s: + self.parse(s) + + def __str__(self): + return 'Style: %s' % ','.join([getattr(self, x.lower()) for x in self.format]) - def parse(self, format, s): + def parse(self, s): s = s.split(': ', 1) if s[0] != 'Style': raise StyleException('invalid style line') s = s[1].split(',') - for k, v in zip(format, s): - setattr(self, k, v) + for k, v in zip(self.format, s): + setattr(self, k.lower(), v) class Styles: def __init__(self, f = None): @@ -54,7 +65,7 @@ class Styles: s = s.split(': ', 1) if s[0] != 'Format': raise StyleException('format line not found') - self.format = [x.lower() for x in s[1].split(', ')] + self.format = s[1].split(', ') s = f.readline().strip() while s: @@ -62,6 +73,11 @@ class Styles: self.styles[s.name.lower()] = s s = f.readline().split() + def write(self, f): + f.write('[V4 Styles]\n') + f.write('Format: %s\n' % ', '.join(self.format)) + f.writelines(['%s\n' % str(s) for s in self.styles.values()]) + class Syllable: def __init__(self, start, end, text): self.start, self.end, self.dur, self.text = start, end, end - start, text @@ -70,10 +86,11 @@ class Syllable: return '{\\k%d}%s' % (self.dur / 10, self.text) class Event: - def __init__(self, format = None, s = None): + def __init__(self, format, s = None): self.karaoke = [] - if format and s: - self.parse(format, s) + self.format = format + if s: + self.parse(s) def __getitem__(self, key): if type(key) is int: @@ -81,12 +98,15 @@ class Event: else: raise TypeError('key must be int') - def parse(self, format, s): + def __str__(self): + return '%s: %s' % (self.kind.capitalize(), ','.join([getattr(self, x.lower()) for x in self.format])) + + def parse(self, s): s = s.split(': ', 1) self.kind = s[0].lower() s = s[1].split(',') - for k, v in zip(format, s): - setattr(self, k, v) + for k, v in zip(self.format, s): + setattr(self, k.lower(), v) if self.kind == 'dialogue' and self.text.find('\\k') > -1: self.parse_karaoke() @@ -138,13 +158,18 @@ class Events: s = s.split(': ', 1) if s[0] != 'Format': raise EventException('format line not found') - self.format = [x.lower() for x in s[1].split(', ')] + self.format = s[1].split(', ') s = f.readline().strip() while s: self.events.append(Event(self.format, s)) s = f.readline().strip() + def write(self, f): + f.write('[Events]\n') + f.write('Format: %s\n' % ', '.join(self.format)) + f.writelines(['%s\n' % str(e) for e in self.events]) + class Script: def __init__(self, f = None): if f: @@ -172,3 +197,12 @@ class Script: self.events = Events(f) else: raise ScriptException('script events not found') + + def write(self, f): + f = open(f, 'w') + self.info.write(f) + f.write('\n') + self.styles.write(f) + f.write('\n') + self.events.write(f) + f.write('\n') -- cgit v1.2.3