X-Git-Url: https://scm.cri.ensmp.fr/git/minwii.git/blobdiff_plain/6ac0f3c83c7f2b916038acfed564ba6c7fe8e9f4..00c7d2ee288d92de01920b31c968ada18651d2a4:/src/minwii/logfilereader.py diff --git a/src/minwii/logfilereader.py b/src/minwii/logfilereader.py index 648cdee..a5a2d35 100755 --- a/src/minwii/logfilereader.py +++ b/src/minwii/logfilereader.py @@ -6,6 +6,7 @@ $Id$ $URL$ """ +from types import StringTypes from widgets.playingscreen import PlayingScreenBase from eventutils import EventDispatcher from events import eventCodes @@ -30,12 +31,12 @@ class LogFileReader(object) : classe utilitaire pour l'accès aux données d'un fichier de log MinWii. """ - def __init__(self, logfile) : + def __init__(self, logfile, mode='r') : """ logfile : chemin d'accès au fichier de log MinWii. le format supporté est actuellement la version 1.0 uniquement. """ - if isinstance(logfile, str) : - self.logfile = open(logfile, 'r') + if isinstance(logfile, StringTypes) : + self.logfile = open(logfile, mode) else : self.logfile = logfile @@ -113,12 +114,14 @@ class LogFileReader(object) : for l in self.getBackwardLineIterator() : if l.startswith('EVT ') : break + else : + return None lastTicks = int(l.split(None, 2)[1]) return lastTicks def __del__(self) : - self.logfile.close() + self.close() def __iter__(self) : return self @@ -157,6 +160,39 @@ class LogFileReader(object) : while line : yield line.strip() line = br.readline() + + @inplaceread + def getMetadata(self) : + metadata = {} + self.next() # skip identification line. + line = self.next() + while line.startswith('METADATA ') : + line = line.split(None, 1)[1] + name, value = [v.strip() for v in line.split(':', 1)] + metadata[name] = value + line = self.next() + return metadata + + def setMetadata(self, metadata) : + f = self.logfile + f.seek(0) + before = f.readline() + line = f.readline() + while line.startswith('METADATA ') : + line = f.readline() + after = line + f.read() + + lines = [] + for name, value in metadata : + lines.append('METADATA %s : %s' % (name, value.encode('utf-8'))) + metadata = '\n'.join(lines) + f.seek(0) + f.write(before) + f.write(metadata) + f.write(after) + + def close(self) : + self.logfile.close() @@ -229,5 +265,4 @@ class LogFilePlayer(PlayingScreenBase) : previousTicks = ticks self.stop() - \ No newline at end of file