Thread: [Pymoul-svn] SF.net SVN: pymoul: [44] pymoul/trunk/src/moul/file
Status: Alpha
Brought to you by:
tiran
From: <ti...@us...> - 2007-01-18 17:00:18
|
Revision: 44 http://pymoul.svn.sourceforge.net/pymoul/?rev=44&view=rev Author: tiran Date: 2007-01-18 09:00:13 -0800 (Thu, 18 Jan 2007) Log Message: ----------- WDYS ini parser (working) parses and writes graphics and audio ini data Modified Paths: -------------- pymoul/trunk/src/moul/file/tests/test_wdysini.py pymoul/trunk/src/moul/file/wdysini.py Modified: pymoul/trunk/src/moul/file/tests/test_wdysini.py =================================================================== --- pymoul/trunk/src/moul/file/tests/test_wdysini.py 2007-01-17 22:28:07 UTC (rev 43) +++ pymoul/trunk/src/moul/file/tests/test_wdysini.py 2007-01-18 17:00:13 UTC (rev 44) @@ -21,13 +21,59 @@ __version__ = "$Id$" __revision__ = "$Revision$" +import os import unittest from doctest import DocTestSuite -import moul.file.wdysini +from moul.file.wdysini import AudioIni +from moul.file.wdysini import GraphicsIni +base = os.path.dirname(__file__) +gra_enc = os.path.join(base, 'graphics.ini') +gra_dec = os.path.join(base, 'graphics.txt') +aud_enc = os.path.join(base, 'audio.ini') +aud_dec = os.path.join(base, 'audio.txt') + +class GenericIniTest(unittest.TestCase): + enc = None + dec = None + parserClass = None + + def setUp(self): + self.enc_fd = open(self.enc, 'rb') + self.dec_fd = open(self.dec, 'r') + self.parser = self.parserClass() + + def tearDown(self): + self.enc_fd.close() + self.dec_fd.close() + + def test_parseString(self): + p = self.parser + data = self.dec_fd.read() + p.parseString(data) + self.failUnless(p._filedata) + self.failIf(p.isChanged()) + newdata = p.writeString() + olines = data.split('\n') + nlines = newdata.split('\n') + for i, oline in enumerate(olines): + self.failUnlessEqual(oline, nlines[i]) + +class AudioIniTest(GenericIniTest): + enc = aud_enc + dec = aud_dec + parserClass = AudioIni + +class GraphicsIniTest(GenericIniTest): + enc = gra_enc + dec = gra_dec + parserClass = GraphicsIni + def test_suite(): return unittest.TestSuite(( + unittest.makeSuite(AudioIniTest), + unittest.makeSuite(GraphicsIniTest), DocTestSuite('moul.file.wdysini') )) Modified: pymoul/trunk/src/moul/file/wdysini.py =================================================================== --- pymoul/trunk/src/moul/file/wdysini.py 2007-01-17 22:28:07 UTC (rev 43) +++ pymoul/trunk/src/moul/file/wdysini.py 2007-01-18 17:00:13 UTC (rev 44) @@ -24,6 +24,89 @@ from moul.crypt.whatdoyousee import decryptWDYS from moul.crypt.whatdoyousee import encryptWDYS +class BoolString(object): + """Boolean string + + >>> BoolString(True) + true + >>> BoolString(1) + true + >>> bool(BoolString(1)) + True + >>> int(BoolString(1)) + 1 + >>> BoolString('yes'), BoolString('true'), BoolString('1') + (true, true, true) + + >>> BoolString() + false + >>> BoolString(False) + false + >>> BoolString(0) + false + >>> bool(BoolString(0)) + False + >>> int(BoolString(0)) + 0 + >>> BoolString('no'), BoolString('false'), BoolString('0') + (false, false, false) + + >>> BoolString('OK') + Traceback (most recent call last): + ... + ValueError: OK + """ + + def __init__(self, value=False): + if isinstance(value, basestring): + if value.lower() in ('yes', 'true', '1'): + value = True + elif value.lower() in ('no', 'false', '0'): + value = False + else: + raise ValueError(value) + self._true = bool(value) + + def __nonzero__(self): + return self._true + + def __int__(self): + return int(self._true) + + def __str__(self): + if self._true: + return 'true' + else: + return 'false' + + def __repr__(self): + return str(self) + + def __cmp__(self, other): + return cmp(bool(self), bool(other)) + +class FloatString(float): + """Float with a slightly different representation + + >>> FloatString(1.0) + 1 + >>> str(FloatString(1.0)) + '1' + >>> FloatString(0.0) + 0.0 + >>> str(FloatString(0.0)) + '0.0' + """ + def __str__(self): + if self == 0.0: + return '0.0' + if self == 1.0: + return '1' + return "%0.12f" % self + + def __repr__(self): + return str(self) + VIDEO_MODES = ( # width, height, w ratio, h ratio (800, 600, 4, 3), @@ -41,20 +124,69 @@ class ConfFile(object): _options = {} + + def __init__(self): + self._filedata = {} + self._newdata = {} + self._order = [] + + def parseString(self, s): + """Parse string with file contents + + @param s newline seperated file (string) + """ + lines = s.split('\n') + for line in lines: + if not line.strip(): + continue + found = False + for key, convert in self._options.items(): + # not elegant but safe + if line.startswith(key): + found = True + newkey = key.replace(' ', '_') + self._order.append(newkey) + data = line[len(key)+1:].strip() + self._filedata[newkey] = convert(data) + break + if not found: + raise ValueError(line) + + self._newdata = self._filedata.copy() + + def isChanged(self): + """Check if the data was changed + """ + for key, value in self._filedata.items(): + if value != self._newdata[key]: + return True + return False + + def writeString(self): + """Create a string with new file contents + """ + out = [] + for newkey in self._order: + value = self._newdata[newkey] + key = newkey.replace('_', ' ') + assert key in self._options + out.append("%s %s" % (key, value)) + out.append('') # new line at EOF + return '\n'.join(out) class AudioIni(ConfFile): _options = { - 'Audio.Initialize' : bool, - 'Audio.UseEAX' : bool, + 'Audio.Initialize' : BoolString, + 'Audio.UseEAX' : BoolString, 'Audio.SetPriorityCutoff' : int, 'Audio.MuteAll' : int, - 'Audio.SetChannelVolume SoundFX' : float, - 'Audio.SetChannelVolume BgndMusic' : float, - 'Audio.SetChannelVolume Ambience' : float, - 'Audio.SetChannelVolume NPCVoice' : float, + 'Audio.SetChannelVolume SoundFX' : FloatString, + 'Audio.SetChannelVolume BgndMusic' : FloatString, + 'Audio.SetChannelVolume Ambience' : FloatString, + 'Audio.SetChannelVolume NPCVoice' : FloatString, 'Audio.EnableVoiceRecording' : int, 'Audio.SetDeviceName' : str, - 'Audio.SetChannelVolume GUI' : float, + 'Audio.SetChannelVolume GUI' : FloatString, } class GraphicsIni(ConfFile): @@ -62,15 +194,15 @@ 'Graphics.Width' : int, 'Graphics.Height' : int, 'Graphics.ColorDepth' : int, - 'Graphics.Windowed' : bool, + 'Graphics.Windowed' : BoolString, 'Graphics.AntiAliasAmount' : int, 'Graphics.AnisotropicLevel' : int, 'Graphics.TextureQuality' : int, 'Quality.Level' : int, 'Graphics.Shadow.Enable' : int, 'Graphics.EnablePlanarReflections' : int, - 'Graphics.EnableVSync' : bool, - 'Graphics.Shadow.VisibleDistance' : float, + 'Graphics.EnableVSync' : BoolString, + 'Graphics.Shadow.VisibleDistance' : FloatString, } # width, height, w ratio, h ratio @@ -84,7 +216,6 @@ def getVidModeHuman(self, idx): """Human readable vidoe mode by index """ - #import pdb; pdb.set_trace() return "%ix%i (%i:%i)" % self.getVidModeByIdx(idx) def getVidIdxByMode(self, w, h): This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ti...@us...> - 2007-01-20 20:23:20
|
Revision: 55 http://pymoul.svn.sourceforge.net/pymoul/?rev=55&view=rev Author: tiran Date: 2007-01-20 12:23:17 -0800 (Sat, 20 Jan 2007) Log Message: ----------- loc parser tests Modified Paths: -------------- pymoul/trunk/src/moul/file/localization.py pymoul/trunk/src/moul/file/tests/test_localization.py Added Paths: ----------- pymoul/trunk/src/moul/file/tests/test.loc Modified: pymoul/trunk/src/moul/file/localization.py =================================================================== --- pymoul/trunk/src/moul/file/localization.py 2007-01-20 20:00:57 UTC (rev 54) +++ pymoul/trunk/src/moul/file/localization.py 2007-01-20 20:23:17 UTC (rev 55) @@ -70,6 +70,13 @@ assert key == translation.key() dict.__setitem__(self, key, translation) + def clear(self): + dict.clear(self) + self._bylanguage.clear() + self._byage.clear() + self._byset.clear() + self._byelement.clear() + translationRegistry = TranslationRegistry() registerTranslation = translationRegistry.register @@ -101,6 +108,12 @@ @property def age(self): return self._age + @property + def set(self): return self._set + + @property + def element(self): return self._element + def __str__(self): return self.content @@ -193,7 +206,7 @@ LOG.debug("parseLogDirectory: unable to read file %s" % loc) continue with open(loc, 'r') as fd: - parseLoc(fd, fname=fname, debug=debug) + parseLoc(fd, fname=fname) files.append(fname) return {'files' : files, 'errors' : errors } Added: pymoul/trunk/src/moul/file/tests/test.loc =================================================================== --- pymoul/trunk/src/moul/file/tests/test.loc (rev 0) +++ pymoul/trunk/src/moul/file/tests/test.loc 2007-01-20 20:23:17 UTC (rev 55) @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8"?> +<localizations> + <age name="testage"> + <set name="testset"> + <element name="testelement"> +<translation language="english"> +text +data +foo +</translation> + </element> + </set> + </age> +</localizations> + Modified: pymoul/trunk/src/moul/file/tests/test_localization.py =================================================================== --- pymoul/trunk/src/moul/file/tests/test_localization.py 2007-01-20 20:00:57 UTC (rev 54) +++ pymoul/trunk/src/moul/file/tests/test_localization.py 2007-01-20 20:23:17 UTC (rev 55) @@ -21,13 +21,39 @@ __version__ = "$Id$" __revision__ = "$Revision$" +import os import unittest from doctest import DocTestSuite -import moul.file.localization +from moul.file.localization import parseLocDirectory +from moul.file.localization import parseLoc +from moul.file.localization import translationRegistry +base = os.path.dirname(__file__) + +class LocalizationTest(unittest.TestCase): + def setUp(self): + self._old = translationRegistry.copy() + translationRegistry.clear() + + def tearDown(self): + pass + + def test_parseLocDir(self): + parseLocDirectory(base) + key = (u'testage', u'testset', u'testelement', u'english') + self.failUnless(key in translationRegistry, translationRegistry.keys()) + self.failUnlessEqual(len(translationRegistry), 1) + + def test_parseLoc(self): + parseLoc(os.path.join(base, 'test.loc')) + key = (u'testage', u'testset', u'testelement', u'english') + self.failUnless(key in translationRegistry, translationRegistry.keys()) + self.failUnlessEqual(len(translationRegistry), 1) + def test_suite(): return unittest.TestSuite(( + unittest.makeSuite(LocalizationTest), DocTestSuite('moul.file.localization') )) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ti...@us...> - 2007-01-30 01:20:57
|
Revision: 100 http://pymoul.svn.sourceforge.net/pymoul/?rev=100&view=rev Author: tiran Date: 2007-01-29 17:20:56 -0800 (Mon, 29 Jan 2007) Log Message: ----------- Fixed misc bugs in plasmalog and chatlog Added KIImageFixer class with tests Modified Paths: -------------- pymoul/trunk/src/moul/file/chatlog.py pymoul/trunk/src/moul/file/kiimage.py pymoul/trunk/src/moul/file/plasmalog.py pymoul/trunk/src/moul/file/tests/test_kiimage.py pymoul/trunk/src/moul/file/tests/test_plasmalog.py Modified: pymoul/trunk/src/moul/file/chatlog.py =================================================================== --- pymoul/trunk/src/moul/file/chatlog.py 2007-01-29 22:07:42 UTC (rev 99) +++ pymoul/trunk/src/moul/file/chatlog.py 2007-01-30 01:20:56 UTC (rev 100) @@ -22,7 +22,7 @@ (MM/DD hh:mm:ss) Chat.log started... (MM/DD hh:mm:ss) ...Chat.log stopped. -(MM/DD hh:mm:ss) From USER in LOCATION: msg +(MM/DD hh:mm:ss) From USER in LOCATION: MSG (MM/DD hh:mm:ss) Error: ERRORMSG (MM/DD hh:mm:ss) USER: TEXT (note the two spaces!) (MM/DD hh:mm:ss) To USER: TEXT @@ -64,7 +64,6 @@ # User: message ) - LOG = getLogger('moul.chat') def modtime(pathname): Modified: pymoul/trunk/src/moul/file/kiimage.py =================================================================== --- pymoul/trunk/src/moul/file/kiimage.py 2007-01-29 22:07:42 UTC (rev 99) +++ pymoul/trunk/src/moul/file/kiimage.py 2007-01-30 01:20:56 UTC (rev 100) @@ -24,13 +24,38 @@ import os import tempfile import struct +from stat import ST_SIZE +from stat import ST_MTIME +from fnmatch import fnmatch +from moul.log import getLogger + JPEG_HEADER = "\377\330\377" -class KiImageError(ValueError): +LOG = getLogger("moul.kiimage") + +class KIImageError(ValueError): pass -class KiImage(object): +def fixedNewer(ki, fixed): + """Check file size and mod date to see if the fixed image is newer + """ + kistat = os.stat(ki) + fixedstat = os.stat(fixed) + if kistat[ST_SIZE] +4 == fixedstat[ST_SIZE]: + # ok, KI is 4 bytes larger + if fixed[ST_MTIME] > ki[ST_MTIME]: + # ok, fixed is newer + return True + else: + return False + else: + return None + +MOUL_IMAGE = 1 +JPEG_IMAGE = 2 + +class KIImage(object): """Ki image handler MOUL's KI images have four leading bytes of junk that encode the file @@ -44,17 +69,26 @@ else: name = getattr(fd_name, 'name', '<UNKNOWN>') fd = fd_name - + self._filename = name self._fd = fd self._size = None + self._filetype = None def close(self): + """Close file handler + """ if self._fd: self._fd.close() self._fd = None - def getSize(self): + def getFileSize(self): + """Get size of file + + Uses the standard seek(0,2)+tell() syntax to get the size of the file. + The file size is cached in self._size. The function resets the + position of the file pointer at the end of the call. + """ if self._size is not None: return self._size fd = self._fd @@ -69,6 +103,12 @@ return size def moulHeaderToSize(self, header=None): + """Convert a MOUL header to file size in int + + NOTE: A MOUL file is jpeg size + 4 bytes header size + + If header is not given the header of the current file is used. + """ # XXX use struct if header is None: fd = self._fd @@ -80,9 +120,13 @@ return size def sizeToMoulHeader(self, size=None): + """Converts a file size in int to a MOUL header string + + NOTE: A MOUL file is jpeg size + 4 bytes header size + """ # XXX use struct if size is None: - size = self.getSize() + size = self.getFileSize() leading = 4* [None] for i in (3,2,1,0): l = size >> 8*i @@ -92,31 +136,54 @@ def verifyMoulHeader(self, header=None): header_size = self.moulHeaderToSize(header) - file_size = self.getSize() + file_size = self.getFileSize() if (header_size + 4) == file_size: return True return False def isJpeg(self): + """Check if file is a JPEG image w/o MOUL header + + The method only checks the header. It doesn't verify the whole + file. + """ + if self._filetype is not None: + return self._filetype == JPEG_IMAGE fd = self._fd fd.seek(0) data = fd.read(3) if data == JPEG_HEADER and not self.isMoulImage(): + self._filetype = JPEG_IMAGE return True - return False + else: + return False def isMoulImage(self): + """Check if file is a MOUL file (JPEG file with MOUL header) + + The method only checks the header. It doesn't verify the whole + file but it verifies the MOUL header. + """ + if self._filetype is not None: + return self._filetype == MOUL_IMAGE fd = self._fd opos = fd.tell() fd.seek(4) data = fd.read(3) if data == JPEG_HEADER and self.verifyMoulHeader(): + self._filetype = MOUL_IMAGE return True - return False + else: + return False + + def removeMoulHeader(self): + """Remove MOUL header from an image - def removeMoulHeader(self): + The method doesn't chance the current file. It returns a file + descriptor to a temporary file. + """ if not self.isMoulImage(): - raise KiImageError('Image has no MOUL header') + raise KIImageError('Image has no MOUL header') out = tempfile.TemporaryFile() fd = self._fd fd.seek(4) @@ -125,10 +192,15 @@ return out def addMoulHeader(self): + """Add MOUL header to an image + + The method doesn't chance the current file. It returns a file + descriptor to a temporary file. + """ if self.isMoulImage(): - raise KiImageError('Image has already a MOUL header') + raise KIImageError('Image has already a MOUL header') if not self.isJpeg(): - raise KiImageError('File is not a JPEG') + raise KIImageError('File is not a JPEG') out = tempfile.TemporaryFile() header = self.sizeToMoulHeader() fd = self._fd @@ -138,3 +210,62 @@ out.write(fd.read()) out.seek(0) return out + +class KIImageFixer(object): + """Create fixed images in a new directory + """ + _pat = ("*.jpg",) + + def __init__(self, srcdir, destdir): + self._srcdir = srcdir + self._destdir = destdir + self._found = [] # all found files + self._tocheck = [] # found files to check + self._fixed = [] # fixed files + + if not os.path.isdir(srcdir): + LOG.critical("%s is not a directory" % srcdir) + return False + if not os.path.isdir(destdir): + LOG.info("Creating chatlog directory %s" % destdir) + os.mkdir(destdir) + + def findFiles(self): + """Find filess + """ + for root, dirs, files in os.walk(self._srcdir): + for name in files: + matches = [pat for pat in self._pat + if fnmatch(name, pat)] + if matches: + ki = os.path.join(root, name) + fixed = os.path.join(self._destdir, name) + self._found.append(ki) + if os.path.isfile(fixed): + if fixedNewer(ki, fixed): + LOG.debug("File %s exists but was changed." % name) + self._tocheck.append((ki, fixed)) + else: + LOG.debug("File %s exists and is fixed." % name) + else: + self._tocheck.append((ki, fixed)) + def checkAndCopyFiles(self): + """Check files if they are KI images + + If the file is a KI image than copy the file to the new location + """ + for kiname, fixedname in self._tocheck: + ki = KIImage(kiname) + if ki.isMoulImage(): + tmp = ki.removeMoulHeader() + fixed = open(fixedname, 'wb') + while True: + buf = tmp.read(4096) + if not buf: + break + fixed.write(buf) + LOG.info("Created fixed image %s" % fixedname) + self._fixed.append((kiname, fixedname)) + fixed.close() + tmp.close() + ki.close() Modified: pymoul/trunk/src/moul/file/plasmalog.py =================================================================== --- pymoul/trunk/src/moul/file/plasmalog.py 2007-01-29 22:07:42 UTC (rev 99) +++ pymoul/trunk/src/moul/file/plasmalog.py 2007-01-30 01:20:56 UTC (rev 100) @@ -106,20 +106,30 @@ The removeLogs function removes only files considered as safe """ + rmdir = [] for root, dirs, files in os.walk(self._srcdir, topdown=False): for name in files: matches = [pat for pat in self._save_patterns if fnmatch(name, pat)] - fpath = os.path.join(root, name) if matches: - os.remove(fpath) + os.remove(os.path.join(root, name)) else: - self._not_removed.append(fpath) + self._not_removed.append((root, name)) LOG.warning("Won't remove %s from %s" % (name, root)) for name in dirs: - os.rmdir(os.path.join(root, name)) + rmdir.append(os.path.join(root, name)) - os.rmdir(self._srcdir) + rmdir.append(self._srcdir) + for d in rmdir: + try: + os.rmdir(d) + except OSError, err: + LOG.exception("Could not delete directory") + self._not_removed.append(d) + if self._not_removed: + return False + else: + return True def __call__(self): self.zipLogDir() Modified: pymoul/trunk/src/moul/file/tests/test_kiimage.py =================================================================== --- pymoul/trunk/src/moul/file/tests/test_kiimage.py 2007-01-29 22:07:42 UTC (rev 99) +++ pymoul/trunk/src/moul/file/tests/test_kiimage.py 2007-01-30 01:20:56 UTC (rev 100) @@ -26,15 +26,15 @@ import unittest from doctest import DocTestSuite -from moul.file.kiimage import KiImage -from moul.file.kiimage import KiImageError +from moul.file.kiimage import KIImage +from moul.file.kiimage import KIImageError from moul.file.kiimage import JPEG_HEADER base = os.path.dirname(__file__) kiimg = os.path.join(base, 'avatar.jpg') kiclean = os.path.join(base, 'avatar_clean.jpg') -class KiImageTest(unittest.TestCase): +class KIImageTest(unittest.TestCase): def setUp(self): self._ki = open(kiimg, 'rb') @@ -45,29 +45,29 @@ self._clean.close() def test_openname(self): - k = KiImage(kiimg) + k = KIImage(kiimg) self.failUnless(k.verifyMoulHeader()) self.failUnless(k.isMoulImage()) self.failIf(k.isJpeg()) - k = KiImage(kiclean) + k = KIImage(kiclean) self.failIf(k.verifyMoulHeader()) self.failIf(k.isMoulImage()) self.failUnless(k.isJpeg()) def test_openfd(self): - k = KiImage(self._ki) + k = KIImage(self._ki) self.failUnless(k.verifyMoulHeader()) self.failUnless(k.isMoulImage()) self.failIf(k.isJpeg()) - k = KiImage(self._clean) + k = KIImage(self._clean) self.failIf(k.verifyMoulHeader()) self.failIf(k.isMoulImage()) self.failUnless(k.isJpeg()) def test_removeheader(self): - k = KiImage(self._ki) + k = KIImage(self._ki) fd = k.removeMoulHeader() data = fd.read() cleandata = self._clean.read() @@ -76,7 +76,7 @@ "file mismatch %r:%r" % (data[:8], cleandata[:8])) def test_addheader(self): - k = KiImage(self._clean) + k = KIImage(self._clean) fd = k.addMoulHeader() data = fd.read() kidata = self._ki.read() @@ -86,7 +86,7 @@ def test_suite(): return unittest.TestSuite(( - unittest.makeSuite(KiImageTest), + unittest.makeSuite(KIImageTest), DocTestSuite('moul.file.kiimage') )) Modified: pymoul/trunk/src/moul/file/tests/test_plasmalog.py =================================================================== --- pymoul/trunk/src/moul/file/tests/test_plasmalog.py 2007-01-29 22:07:42 UTC (rev 99) +++ pymoul/trunk/src/moul/file/tests/test_plasmalog.py 2007-01-30 01:20:56 UTC (rev 100) @@ -30,6 +30,8 @@ from moul.file.plasmalog import PlasmalogZipper from moul.file.chatlog import ChatlogMover +from moul.file.kiimage import KIImageFixer +from moul.file.kiimage import KIImage FILE_LIST = ['audio.0.elf', 'audiocaps.0.elf', 'audioTimes.0.elf', 'Avatar.0.elf', 'impacts.0.elf', 'LocalizationDataMgr.0.elf', @@ -70,6 +72,8 @@ _fakeLogdir(os.path.join(path, 'Logs', 'faketest')) shutil.copyfile(os.path.join(base, 'avatar.jpg'), os.path.join(path, 'Avatars', '001.jpg')) + shutil.copyfile(os.path.join(base, 'avatar.jpg'), + os.path.join(path, 'KIimages', 'KIimage001.jpg')) shutil.copyfile(os.path.join(base, 'graphics.ini'), os.path.join(path, 'init', 'graphics.ini')) shutil.copyfile(os.path.join(base, 'audio.ini'), @@ -78,19 +82,24 @@ class PlasmaChatTest(unittest.TestCase): def setUp(self): self._tmpdir = mkdtemp() + self._logdir = os.path.join(self._tmpdir, 'Logs') + self._kidir = os.path.join(self._tmpdir, 'KIimages') + self._avatardir = os.path.join(self._tmpdir, 'Avatars') + self._chatdest = os.path.join(self._tmpdir, 'Chatlogs') self._logdest = os.path.join(self._tmpdir, 'ZippedLogs') - self._logdir = os.path.join(self._tmpdir, 'Logs') + + self._kidest = os.path.join(self._tmpdir, 'FixedImages') _fakeUruLiveDir(self._tmpdir) - self._clm = ChatlogMover(self._logdir, self._chatdest) - self._plz = PlasmalogZipper(self._logdir, self._logdest) - def test_createDir(self): + def test_000dirs(self): + for d in (self._tmpdir, self._logdir, self._kidir, self._avatardir): + self.failUnless(os.path.isdir(d), d) + + def test_plasmaLogZipper(self): + plz = PlasmalogZipper(self._logdir, self._logdest) self.failUnless(os.path.isdir(self._logdest), self._logdest) - self.failUnless(os.path.isdir(self._chatdest), self._chatdest) - def test_plasmaLogZipper(self): - plz = self._plz self.failUnless(plz.isPlasmaLogDir()) plz.zipLogDir() content = os.listdir(self._logdest) @@ -103,7 +112,9 @@ self.failIf(os.path.isdir(self._logdir)) def test_chatLogMover(self): - clm = self._clm + clm = ChatlogMover(self._logdir, self._chatdest) + self.failUnless(os.path.isdir(self._chatdest), self._chatdest) + clm.findLogs() clm.moveChatlogs() content = os.listdir(self._chatdest) @@ -114,6 +125,22 @@ self.failUnless(c.endswith(".txt")) self.failUnlessEqual(len(c), length, c) + def test_kiImageMover(self): + kif = KIImageFixer(self._kidir, self._kidest) + self.failUnless(os.path.isdir(self._kidest), self._kidest) + + kif.findFiles() + kif.checkAndCopyFiles() + content = os.listdir(self._kidest) + self.failUnlessEqual(len(content), 1, content) + self.failUnlessEqual(content, ["KIimage001.jpg"]) + + cleandata = open(os.path.join(base, 'avatar_clean.jpg'), 'rb').read() + newdata = open(os.path.join(self._kidest, "KIimage001.jpg"), 'rb').read() + + self.failUnlessEqual(len(cleandata), len(newdata)) + self.failUnlessEqual(cleandata, newdata) + def tearDown(self): shutil.rmtree(self._tmpdir) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ti...@us...> - 2007-02-01 02:39:34
|
Revision: 112 http://pymoul.svn.sourceforge.net/pymoul/?rev=112&view=rev Author: tiran Date: 2007-01-31 18:39:34 -0800 (Wed, 31 Jan 2007) Log Message: ----------- Added directory handler Modified Paths: -------------- pymoul/trunk/src/moul/file/localization.py Added Paths: ----------- pymoul/trunk/src/moul/file/directory.py pymoul/trunk/src/moul/file/tests/test_directory.py Removed Paths: ------------- pymoul/trunk/src/moul/file/directories.py Deleted: pymoul/trunk/src/moul/file/directories.py =================================================================== --- pymoul/trunk/src/moul/file/directories.py 2007-02-01 00:05:40 UTC (rev 111) +++ pymoul/trunk/src/moul/file/directories.py 2007-02-01 02:39:34 UTC (rev 112) @@ -1,78 +0,0 @@ -# pyMoul - Python interface to Myst Online URU Live -# Copyright (C) 2007 Christian Heimes <christian (at) cheimes (dot) de> - -# This program is free software; you can redistribute it and/or modify it -# under the terms of the GNU General Public License as published by the -# Free Software Foundation; either version 2 of the License, or (at your -# option) any later version. -# -# This program is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for -# more details. -# -# You should have received a copy of the GNU General Public License along -# with this program; if not, write to the Free Software Foundation, Inc., 59 -# Temple Place, Suite 330, Boston, MA 02111-1307 USA -# -"""Directories: Uru game directory and Uru personal data directory -""" -__author__ = "Christian Heimes" -__version__ = "$Id$" -__revision__ = "$Revision$" - -import os - -from moul.log import getLogger -from moul.file.chatlog import ChatlogMover -from moul.file.chatlog import ChatlogDirectoryView -from moul.file.plasmalog import PlasmalogZipper -from moul.file.kiimage import KIImageFixer - -LOG = getLogger('moul.file.directory') - -class AbstractUruDirectory(object): - """Abstract class for an Uru directory - """ - _dirmapping = {} - - def __init__(self, basedir): - self._basedir = basedir - - def exists(self): - return os.path.isdir(self._basedir) - - def get(self, name): - path = self._dirmapping(name) - return os.path.join(self._basedir, path) - -class UruGameDataDirectory(AbstractUruDirectory): - """Uru game directory handler - - An uru game directory contains the launcher, explorer, age files, sound - files, localizations and other stuff. - """ - _dirmapping = { - 'loc' : 'dat', - 'dat' : 'dat', - 'sound' : 'sfx' - 'soundwav' : 'sfx' + os.sep + 'streamingCache', - 'video' : 'avi', - } - -class UruPersonalDataDirectory(AbstractUruDirectory): - """Uru personal data handler - - An uru personal data directory contains per user data like audio and - graphics settings, KI shots, avatar images, logs and pyMoul specific - data. - """ - _dirmapping = { - 'log' : 'Log', - 'kiimages' : 'KIimages', - 'avatars' : 'Avatars', - 'ini' : 'init', - 'chatlogs' : 'Chatlogs', - 'zipped' : 'ZippedLogs', - } - Copied: pymoul/trunk/src/moul/file/directory.py (from rev 111, pymoul/trunk/src/moul/file/directories.py) =================================================================== --- pymoul/trunk/src/moul/file/directory.py (rev 0) +++ pymoul/trunk/src/moul/file/directory.py 2007-02-01 02:39:34 UTC (rev 112) @@ -0,0 +1,130 @@ +# pyMoul - Python interface to Myst Online URU Live +# Copyright (C) 2007 Christian Heimes <christian (at) cheimes (dot) de> + +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., 59 +# Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +"""Directories: Uru game directory and Uru personal data directory +""" +__author__ = "Christian Heimes" +__version__ = "$Id$" +__revision__ = "$Revision$" + +import os + +from moul.log import getLogger +from moul.file.chatlog import ChatlogMover +from moul.file.chatlog import ChatlogDirectoryView +from moul.file.plasmalog import PlasmalogZipper +from moul.file.kiimage import KIImageFixer +from moul.file.wdysini import GraphicsIni +from moul.file.wdysini import AudioIni +from moul.file.localization import parseLocDirectory + +LOG = getLogger('moul.file.directory') + +class AbstractUruDirectory(object): + """Abstract class for an Uru directory + """ + # name -> subpath + _dirmapping = {} + # name -> (callable, nametuple) + _factories = {} + + def __init__(self, basepath): + self._basepath = basepath + + def exists(self, name=None): + """Check if directory with name exists. + """ + os.path.isdir(self.get(name)) + + def get(self, name=None): + """Get complete path for name + + None or '.' means basedir. + """ + if name and name != '.': + path = self._dirmapping(name) + else: + path = '' + return os.path.join(self._basepath, path) + + def factory(self, name): + """Factory - creates instances to handle subdirectories and files + """ + args = [] + factory, argnames = self._factories[name] + for name in argnames: + args.append(self.get(name)) + return factory(*args) + +class UruGameDataDirectory(AbstractUruDirectory): + """Uru game directory handler + + An uru game directory contains the launcher, explorer, age files, sound + files, localizations and other stuff. + """ + _dirmapping = { + 'loc' : 'dat', + 'dat' : 'dat', + 'sound' : 'sfx', + 'soundwav' : 'sfx' + os.sep + 'streamingCache', + 'video' : 'avi', + } + _factory = { + 'loc' : (parseLocDirectory, ('loc',)), + } + +class UruPersonalDataDirectory(AbstractUruDirectory): + """Uru personal data handler + + An uru personal data directory contains per user data like audio and + graphics settings, KI shots, avatar images, logs and pyMoul specific + data. + """ + _dirmapping = { + 'log' : 'Log', + 'kiimages' : 'KIimages', + 'avatars' : 'Avatars', + 'ini' : 'init', + 'chatlogs' : 'Chatlogs', + 'zipped' : 'ZippedLogs', + 'fixed' : 'FixedImages', + } + + _factories = { + 'log-chat' : (ChatlogMover, ('log', 'chatlogs')), + 'log-zip' : (PlasmalogZipper, ('log', 'zipped')), + 'kiimages' : (KIImageFixer, ('kiimages', 'fixed')), + 'avatars' : (KIImageFixer, ('avatars', 'fixed')), + 'graphics.ini' : (GraphicsIni, ('ini',)), + 'audio.ini' : (AudioIni, ('ini',)), + 'chatlogs' : (ChatlogDirectoryView, ('chatlogs',)), + 'zipped' : None, + 'fixed' : None, + } + + def createTree(self): + """Create the directory tree + """ + created = [] + if self.exists(''): + os.mkdir(self._basepath) + created.append(self._basepath) + for key in self._dirmapping: + if not self.exists(key): + os.mkdir(path) + created.append(path) + return created Property changes on: pymoul/trunk/src/moul/file/directory.py ___________________________________________________________________ Name: svn:keywords + Id Revision Name: svn:eol-style + native Modified: pymoul/trunk/src/moul/file/localization.py =================================================================== --- pymoul/trunk/src/moul/file/localization.py 2007-02-01 00:05:40 UTC (rev 111) +++ pymoul/trunk/src/moul/file/localization.py 2007-02-01 02:39:34 UTC (rev 112) @@ -36,21 +36,14 @@ from moul.log import getLogger LOG = getLogger('moul loc') -def _addList(d, key, value): - lst = d.setdefault(key, []) - lst.append(value) +IGNORE_AGES = [u'OptionsMenu', u'ACA', u'Global', u'KI', u'Heek'] +IGNORE_SETS = [] +IGNORE_ELEMENTS = [] -def _addTree(d, lang, age, setname, element, key): - dlang = d.setdefault(lang, {}) - dage = dlang.setdefault(age, {}) - dset = dage.setdefault(setname, {}) - dset[element] = key - class TranslationRegistry(dict): """Registry for Translation objects""" - __slot__ = ('_bylanguage', '_byage', '_byset', '_byelement', '_tree', - '_ignore_age') + __slot__ = ('_bylanguage', '_byage', '_byset', '_byelement', '_tree') def __new__(cls, *args, **kwargs): self = dict.__new__(cls, *args, **kwargs) @@ -59,7 +52,6 @@ self._byset = {} self._byelement = {} self._tree = {} - self._ignore_age = [u'OptionsMenu', u'ACA', u'Global', u'KI'] return self def register(self, translation): @@ -69,8 +61,12 @@ (translation.fname, str(key))) LOG.error("TranslationRegistry: %s" % msg) raise KeyError(msg) - if translation.age in self._ignore_age: + if translation.age in IGNORE_AGES: return + if translation.set in IGNORE_SETS: + return + if translation.element in IGNORE_ELEMENTS: + return _addList(self._bylanguage, translation.language, key) _addList(self._byage, translation.age, key) _addList(self._byset, translation.set, key) @@ -112,6 +108,21 @@ translationRegistry = TranslationRegistry() registerTranslation = translationRegistry.register +def _addList(d, key, value): + """Utility for dict.setdefault(key, [].append()) + """ + lst = d.setdefault(key, []) + lst.append(value) + +def _addTree(d, lang, age, setname, element, key): + """Utility to create the lang - age - set - element tree + """ + dlang = d.setdefault(lang, {}) + dage = dlang.setdefault(age, {}) + dset = dage.setdefault(setname, {}) + dset[element] = key + + class Translation(unicode): """Translation object Added: pymoul/trunk/src/moul/file/tests/test_directory.py =================================================================== --- pymoul/trunk/src/moul/file/tests/test_directory.py (rev 0) +++ pymoul/trunk/src/moul/file/tests/test_directory.py 2007-02-01 02:39:34 UTC (rev 112) @@ -0,0 +1,36 @@ +# pyMoul - Python interface to Myst Online URU Live +# Copyright (C) 2007 Christian Heimes <christian (at) cheimes (dot) de> + +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., 59 +# Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +"""moul.file.directory unit tests +""" +__author__ = "Christian Heimes" +__version__ = "$Id$" +__revision__ = "$Revision$" + +import unittest +from doctest import DocTestSuite + +import moul.file.directory + +def test_suite(): + return unittest.TestSuite(( + DocTestSuite('moul.file.directory') + )) + +if __name__ == '__main__': + unittest.main(defaultTest="test_suite") + Property changes on: pymoul/trunk/src/moul/file/tests/test_directory.py ___________________________________________________________________ Name: svn:keywords + Id Revision Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ti...@us...> - 2007-02-01 13:47:37
|
Revision: 114 http://pymoul.svn.sourceforge.net/pymoul/?rev=114&view=rev Author: tiran Date: 2007-02-01 05:47:36 -0800 (Thu, 01 Feb 2007) Log Message: ----------- Moved some functions to tests.utils Added directory tests Doc strings Modified Paths: -------------- pymoul/trunk/src/moul/file/directory.py pymoul/trunk/src/moul/file/tests/test_directory.py pymoul/trunk/src/moul/file/tests/test_plasmalog.py Added Paths: ----------- pymoul/trunk/src/moul/file/tests/utils.py Modified: pymoul/trunk/src/moul/file/directory.py =================================================================== --- pymoul/trunk/src/moul/file/directory.py 2007-02-01 03:07:31 UTC (rev 113) +++ pymoul/trunk/src/moul/file/directory.py 2007-02-01 13:47:36 UTC (rev 114) @@ -36,25 +36,36 @@ class DirectoryCount(object): """Counts how many files are inside a directory + """ + def __init__(self, path): + """Constructor - def __init__(self, path): + @param path: absolute path to folder + @type path: str + """ self._basepath = path - self._count = 0 - if not os.path.isdir(path): - self._count = None - return - # TODO: fnmatch - for name in os.listdir(path): - if os.path.isfile(os.path.join(path, name)): - self._count+=1 @property def count(self): - return self._count + """Count number of files in basepath + returns int or None if path doesn't exist + """ + count = 0 + if not os.path.isdir(self._basepath): + return + # TODO: fnmatch + return len([name for name in os.listdir(path) + if os.path.isfile(os.path.join(path, name))]) + class AbstractUruDirectory(object): """Abstract class for an Uru directory + + @cvar _dirmapping: A mapping of name S{->} subpath + @type _dirmapping: dict + @cvar _factories: A list of factories name S{->} (callable, name tuples) C{_dirmapping} + @type _factories: dict """ # name -> subpath _dirmapping = {} @@ -62,17 +73,30 @@ _factories = {} def __init__(self, basepath): + """Constructor + + @param basepath: absolute path to folder + @type basepath: str + """ + self._basepath = basepath def exists(self, name=None): """Check if directory with name exists. + + @param name: Name of the subdirectory or None for basedir + @type name: str or None + @rtype: bool """ - os.path.isdir(self.get(name)) + return os.path.isdir(self.get(name)) def get(self, name=None): """Get complete path for name - - None or '.' means basedir. + + @param name: Name of the subdirectory or None for basedir + @type name: str or None + @return: absolute path + @rtype: str """ if name and name != '.': path = self._dirmapping(name) @@ -82,6 +106,10 @@ def factory(self, name): """Factory - creates instances to handle subdirectories and files + + @param name: Factory name + @type name: str + @return: Factory instance """ args = [] factory, argnames = self._factories[name] @@ -126,12 +154,12 @@ } _factories = { - 'log-chat' : (ChatlogMover, ('log', 'chatlogs')), - 'log-zip' : (PlasmalogZipper, ('log', 'zipped')), + 'logchat' : (ChatlogMover, ('log', 'chatlogs')), + 'logzip' : (PlasmalogZipper, ('log', 'zipped')), 'kiimages' : (KIImageFixer, ('kiimages', 'fixed')), 'avatars' : (KIImageFixer, ('avatars', 'fixed')), - 'graphics.ini' : (GraphicsIni, ('ini',)), - 'audio.ini' : (AudioIni, ('ini',)), + 'graphicsini' : (GraphicsIni, ('ini',)), + 'audioini' : (AudioIni, ('ini',)), 'chatlogs' : (ChatlogDirectoryView, ('chatlogs',)), 'zipped' : (DirectoryCount, ('zipped',)), 'fixed' : (DirectoryCount, ('fixed',)), @@ -139,6 +167,9 @@ def create(self, name): """Create a directory + + @param name: Name of the subdirectory + @type name: str """ if not name or name == '.': raise ValueError("Cannot create() basedir") @@ -147,6 +178,9 @@ def createTree(self): """Create the directory tree + + @return: List of created directories as absolute path + @rtype: list of str """ created = [] if not self.exists(''): Modified: pymoul/trunk/src/moul/file/tests/test_directory.py =================================================================== --- pymoul/trunk/src/moul/file/tests/test_directory.py 2007-02-01 03:07:31 UTC (rev 113) +++ pymoul/trunk/src/moul/file/tests/test_directory.py 2007-02-01 13:47:36 UTC (rev 114) @@ -24,10 +24,41 @@ import unittest from doctest import DocTestSuite -import moul.file.directory +from moul.file.directory import UruGameDataDirectory +from moul.file.directory import UruPersonalDataDirectory +from moul.file.tests.utils import fakeUruPersonalDataDir +from moul.file.tests.utils import fakeUruGameDir +from moul.file.tests.utils import mkTempdir +from moul.file.tests.utils import rmTempdir +from moul.file.tests.utils import base + +class AbstractTest(unittest.TestCase): + def tearDown(self): + rmTempdir(self._tmpdir) + + def test_exists(self): + self.failUnless(self.urudir.exists) + +class UruPersonalDirTest(AbstractTest): + def setUp(self): + self._tmpdir = mkTempdir() + fakeUruPersonalDataDir(self._tmpdir) + self.urudir = UruPersonalDataDirectory(self._tmpdir) + +class UruGameDirTest(AbstractTest): + def setUp(self): + self._tmpdir = mkTempdir() + fakeUruGameDir(self._tmpdir) + self.urudir = UruGameDataDirectory(self._tmpdir) + + def test_foo(self): + pass + def test_suite(): return unittest.TestSuite(( + unittest.makeSuite(UruPersonalDirTest), + unittest.makeSuite(UruGameDirTest), DocTestSuite('moul.file.directory') )) Modified: pymoul/trunk/src/moul/file/tests/test_plasmalog.py =================================================================== --- pymoul/trunk/src/moul/file/tests/test_plasmalog.py 2007-02-01 03:07:31 UTC (rev 113) +++ pymoul/trunk/src/moul/file/tests/test_plasmalog.py 2007-02-01 13:47:36 UTC (rev 114) @@ -35,55 +35,15 @@ from moul.file.kiimage import KIImageFixer from moul.file.kiimage import KIImage -FILE_LIST = ['audio.0.elf', 'audiocaps.0.elf', 'audioTimes.0.elf', - 'Avatar.0.elf', 'impacts.0.elf', 'LocalizationDataMgr.0.elf', - 'Marker.0.elf', 'NetScreener.0.elf', 'patcher.0.elf', 'pipeline.0.elf', - 'plasmalog.txt', 'Python.0.elf', 'Simulation.0.elf', 'voice.0.elf', - 'Chat.0.log'] +from moul.file.tests.utils import fakeUruPersonalDataDir +from moul.file.tests.utils import fakeUruGameDir +from moul.file.tests.utils import mkTempdir +from moul.file.tests.utils import rmTempdir +from moul.file.tests.utils import base -CHATLOG = """(01/02 03:04:05) Chat.log started... -(01/02 03:04:06) Testuser: foo -(01/02 03:04:07) ...Chat.log stopped. -""" - -base = os.path.dirname(__file__) - -def _fakeLogdir(path): - """Create a fake logdir - """ - if not os.path.isdir(path): - os.mkdir(path) - for fname in FILE_LIST: - fd = open(os.path.join(path, fname), 'w') - fd.write('dummy') - fd.close() - - fd = open(os.path.join(path, 'Chat.0.log'), 'w') - fd.write(CHATLOG) - fd.close() - fd = open(os.path.join(path, 'plasmalog.txt'), 'w') - fd.write('') - fd.close() - -def _fakeUruLiveDir(path): - """Create a fake Uru Live directory - """ - for dname in ('Avatars', 'init', 'KIimages'): - os.mkdir(os.path.join(path, dname)) - _fakeLogdir(os.path.join(path, 'Logs')) - _fakeLogdir(os.path.join(path, 'Logs', 'faketest')) - shutil.copyfile(os.path.join(base, 'avatar.jpg'), - os.path.join(path, 'Avatars', '001.jpg')) - shutil.copyfile(os.path.join(base, 'avatar.jpg'), - os.path.join(path, 'KIimages', 'KIimage001.jpg')) - shutil.copyfile(os.path.join(base, 'graphics.ini'), - os.path.join(path, 'init', 'graphics.ini')) - shutil.copyfile(os.path.join(base, 'audio.ini'), - os.path.join(path, 'init', 'audio.ini')) - class PlasmaChatTest(unittest.TestCase): def setUp(self): - self._tmpdir = mkdtemp() + self._tmpdir = mkTempdir() self._logdir = os.path.join(self._tmpdir, 'Logs') self._kidir = os.path.join(self._tmpdir, 'KIimages') self._avatardir = os.path.join(self._tmpdir, 'Avatars') @@ -92,8 +52,11 @@ self._logdest = os.path.join(self._tmpdir, 'ZippedLogs') self._kidest = os.path.join(self._tmpdir, 'FixedImages') - _fakeUruLiveDir(self._tmpdir) + fakeUruPersonalDataDir(self._tmpdir) + def tearDown(self): + rmTempdir(self._tmpdir) + def test_000dirs(self): for d in (self._tmpdir, self._logdir, self._kidir, self._avatardir): self.failUnless(os.path.isdir(d), d) @@ -153,9 +116,6 @@ self.failUnlessEqual(len(cleandata), len(newdata)) self.failUnlessEqual(cleandata, newdata) - def tearDown(self): - shutil.rmtree(self._tmpdir) - def test_suite(): return unittest.TestSuite(( unittest.makeSuite(PlasmaChatTest), Copied: pymoul/trunk/src/moul/file/tests/utils.py (from rev 113, pymoul/trunk/src/moul/file/tests/test_plasmalog.py) =================================================================== --- pymoul/trunk/src/moul/file/tests/utils.py (rev 0) +++ pymoul/trunk/src/moul/file/tests/utils.py 2007-02-01 13:47:36 UTC (rev 114) @@ -0,0 +1,94 @@ +# pyMoul - Python interface to Myst Online URU Live +# Copyright (C) 2007 Christian Heimes <christian (at) cheimes (dot) de> + +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., 59 +# Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +"""test helpers +""" +__author__ = "Christian Heimes" +__version__ = "$Id$" +__revision__ = "$Revision$" + +import os +import shutil +from tempfile import mkdtemp + +FILE_LIST = ['audio.0.elf', 'audiocaps.0.elf', 'audioTimes.0.elf', + 'Avatar.0.elf', 'impacts.0.elf', 'LocalizationDataMgr.0.elf', + 'Marker.0.elf', 'NetScreener.0.elf', 'patcher.0.elf', 'pipeline.0.elf', + 'plasmalog.txt', 'Python.0.elf', 'Simulation.0.elf', 'voice.0.elf', + 'Chat.0.log'] + +CHATLOG = """(01/02 03:04:05) Chat.log started... +(01/02 03:04:06) Testuser: foo +(01/02 03:04:07) ...Chat.log stopped. +""" + +base = os.path.dirname(__file__) + +def fakeLogdir(path): + """Create a fake logdir + """ + if not os.path.isdir(path): + os.mkdir(path) + for fname in FILE_LIST: + fd = open(os.path.join(path, fname), 'w') + fd.write('dummy') + fd.close() + + fd = open(os.path.join(path, 'Chat.0.log'), 'w') + fd.write(CHATLOG) + fd.close() + fd = open(os.path.join(path, 'plasmalog.txt'), 'w') + fd.write('') + fd.close() + +def fakeUruPersonalDataDir(path): + """Create a fake Uru Live personal data directory + """ + for dname in ('Avatars', 'init', 'KIimages'): + os.mkdir(os.path.join(path, dname)) + fakeLogdir(os.path.join(path, 'Logs')) + fakeLogdir(os.path.join(path, 'Logs', 'faketest')) + shutil.copyfile(os.path.join(base, 'avatar.jpg'), + os.path.join(path, 'Avatars', '001.jpg')) + shutil.copyfile(os.path.join(base, 'avatar.jpg'), + os.path.join(path, 'KIimages', 'KIimage001.jpg')) + shutil.copyfile(os.path.join(base, 'graphics.ini'), + os.path.join(path, 'init', 'graphics.ini')) + shutil.copyfile(os.path.join(base, 'audio.ini'), + os.path.join(path, 'init', 'audio.ini')) + +def fakeUruGameDir(path): + """Create a fake Uru Live game directory + """ + for dname in ('dat', 'sfx', 'avi'): + os.mkdir(os.path.join(path, dname)) + for fname in ('UruLauncher.exe', 'UruExplorer.exe'): # XXX: win32 + fd = open(os.path.join(path, fname), 'wb') + fd.write('dummy') + fd.close() + shutil.copyfile(os.path.join(base, 'test.loc'), + os.path.join(path, 'dat', 'test.loc')) + +def mkTempdir(): + """Make a temporary directory + """ + return mkdtemp() + +def rmTempdir(d): + """Remove a temporary directory + """ + shutil.rmtree(d) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ti...@us...> - 2007-02-04 15:41:45
|
Revision: 133 http://pymoul.svn.sourceforge.net/pymoul/?rev=133&view=rev Author: tiran Date: 2007-02-04 07:41:40 -0800 (Sun, 04 Feb 2007) Log Message: ----------- Changed fixed KI image name Enhanced chatlog and plasmalog code for UI integration Added utils module Modified Paths: -------------- pymoul/trunk/src/moul/file/chatlog.py pymoul/trunk/src/moul/file/kiimage.py pymoul/trunk/src/moul/file/plasmalog.py Added Paths: ----------- pymoul/trunk/src/moul/file/utils.py Modified: pymoul/trunk/src/moul/file/chatlog.py =================================================================== --- pymoul/trunk/src/moul/file/chatlog.py 2007-02-03 21:56:56 UTC (rev 132) +++ pymoul/trunk/src/moul/file/chatlog.py 2007-02-04 15:41:40 UTC (rev 133) @@ -36,9 +36,9 @@ import re from fnmatch import fnmatch from shutil import move -from stat import ST_MTIME from time import localtime +from moul.file.utils import fileModTime from moul.log import getLogger @@ -79,10 +79,6 @@ LOG = getLogger('moul.chat') -def modtime(pathname): - """Modification time - """ - return os.stat(pathname)[ST_MTIME] class ChatlogMover(object): """ @@ -116,38 +112,37 @@ continue LOG.debug("Found chatlog %s" % fpath) fd = open(fpath, 'r') - created = self._findCreated(fd) - if not created: - LOG.warning("Failed to parse chatlog %s" % fpath) - mtime = localtime(modtime(fpath)) + created = self._findCreated(fpath) + mtime = fileModTime(fpath) newname = self._makeFilename(created, mtime) - details = { 'fpath' : fpath, 'filename' : file, 'modtime' : mtime, 'created' : created, 'newname' : newname, - 'newpath' : os.path.join(self._dest, newname), + 'v' : os.path.join(self._dest, newname), } self._logs.append(details) - def _findCreated(self, fd): + def _findCreated(self, fpath): """Parse chatlog to find "started" date """ + fd = open(fpath, 'r') + line = fd.readline() + mo = CHAT_RE.match(line) created = None - while True: - line = fd.readline() - if not line: - break - mo = CHAT_RE.match(line) - if mo: - data = mo.groupdict() - if data['msg'].startswith(CHATLOG_STARTED): - d = mo.groupdict() - created = (None, int(d['M']), int(d['D']), int(d['h']), - int(d['m']), int(d['s']), None, None, None) + if mo: + data = mo.groupdict() + if data['msg'].startswith(CHATLOG_STARTED): + d = mo.groupdict() + created = (None, int(d['M']), int(d['D']), int(d['h']), + int(d['m']), int(d['s']), None, None, None) + if created is None: + LOG.warning("Failed to parse chatlog %s" % fpath) + created = (None, 0, 0, 0, + 0, 0, None, None, None) return created def _makeFilename(self, created, stopped): @@ -162,21 +157,37 @@ } return self._filename % mapping + def moveChatlog(self, details): + """Move a chatlogs from old to new location + """ + old = details['fpath'] + new = details['newpath'] + if os.path.isfile(new): + LOG.warning("%s exists - overwriting" % new) + LOG.info("Move chatlog %s to %s" % (old, new)) + move(old, new) + def moveChatlogs(self): - """Move chatlogs from old to new location and name + """Move all chatlogs """ for details in self._logs: - old = details['fpath'] - new = details['newpath'] - if os.path.isfile(new): - LOG.warning("%s exists - overwriting" % new) - LOG.info("Move chatlog %s to %s" % (old, new)) - move(old, new) + self.moveChatlog(details) - def __call__(self): - self.findLogs() - self.moveChatlogs() + def __len__(self): + """len() suppor + """ + return len(self._logs) + def __iter__(self): + """Iterate over the files to move + + Calls moveChatlog() for each element. This method is used to + update a progress bar in the UI. + """ + for details in self._logs: + yield details['newname'] + self.moveChatlog(details) + class ChatlogDirectoryView(object): """A view of a chat log directory Modified: pymoul/trunk/src/moul/file/kiimage.py =================================================================== --- pymoul/trunk/src/moul/file/kiimage.py 2007-02-03 21:56:56 UTC (rev 132) +++ pymoul/trunk/src/moul/file/kiimage.py 2007-02-04 15:41:40 UTC (rev 133) @@ -22,15 +22,16 @@ __revision__ = "$Revision$" import os +import re import struct import tempfile from fnmatch import fnmatch -from stat import ST_MTIME -from stat import ST_SIZE +from moul.file.utils import fileModTime +from moul.file.utils import fileSize from moul.log import getLogger - +KINUMBER_RE = re.compile("(\d+)\.jpg$", re.IGNORECASE) JPEG_HEADER = "\377\330\377" LOG = getLogger("moul.kiimage") @@ -41,11 +42,9 @@ def fixedNewer(ki, fixed): """Check file size and mod date to see if the fixed image is newer """ - kistat = os.stat(ki) - fixedstat = os.stat(fixed) - if kistat[ST_SIZE] +4 == fixedstat[ST_SIZE]: + if fileSize(ki) +4 == fileSize(fixed): # ok, KI is 4 bytes larger - if fixed[ST_MTIME] > ki[ST_MTIME]: + if fileModTime(fixed) > fileModTime(ki): # ok, fixed is newer return True else: @@ -230,7 +229,27 @@ if not os.path.isdir(destdir): LOG.info("Creating fixed images directory %s" % destdir) os.mkdir(destdir) - + + @staticmethod + def mkFixedName(path, name, dest): + """Make new new based on the name of the KI image + + @param path: directory where the file lives + @type path: str + @param name: name of the image w/o path + @type name: str + @return: KIimage_moddate_number.ext + """ + fpath = os.path.join(path, name) + date = fileModTime(fpath, format="%Y%m%d_%H%M%S") + _, ext = os.path.splitext(name) # "kiimage0001" , ".jpg" + mo = KINUMBER_RE.search(name.lower()) + if mo is not None: + number = int(mo.group(1)) + else: + number = 0 + return os.path.join(dest, "KIimage_%s_%04i.%s" % (date, number, ext[1:])) + def findFiles(self): """Find files """ @@ -243,8 +262,9 @@ if fnmatch(name, pat)] if matches: ki = os.path.join(root, name) - fixed = os.path.join(self._destdir, name) + fixed = self.mkFixedName(root, name, self._destdir) self._found.append(ki) + # XXX: move checks to copy method! if os.path.isfile(fixed): if fixedNewer(ki, fixed): LOG.debug("File %s exists but was changed." % name) @@ -285,9 +305,7 @@ Calls checkAndCopyFiles() for each element. This method is used to update a progress bar in the UI. - - Yields a NULL seperated string containing "kiname\x00fixedname" """ for kiname, fixedname in self._tocheck: - yield fixedname # "%s"x00%s" % (kiname, fixedname) + yield kiname self.checkAndCopyFile(kiname, fixedname) Modified: pymoul/trunk/src/moul/file/plasmalog.py =================================================================== --- pymoul/trunk/src/moul/file/plasmalog.py 2007-02-03 21:56:56 UTC (rev 132) +++ pymoul/trunk/src/moul/file/plasmalog.py 2007-02-04 15:41:40 UTC (rev 133) @@ -28,6 +28,7 @@ from stat import ST_MTIME from moul.crypt.elf import decryptElf +from moul.file.utils import fileModTime from moul.log import getLogger @@ -44,6 +45,9 @@ def __init__(self, srcdir, destdir): self._srcdir = srcdir self._destdir = destdir + self._doremove = True + self._dozip = True + LOG.debug("PlasmalogZipper: %s -> %s" % (srcdir, destdir)) self._zipped_dirs = {} # dirname -> zipfile self._not_removed = [] # files with full path @@ -59,13 +63,16 @@ LOG.info("Creating chatlog directory %s" % destdir) os.mkdir(destdir) - @staticmethod - def getTimeStamp(path): - """Get time stamp yyyymmdd_hhmm based in the modification time + def setZip(self, boolean): + """Set ZIP flag """ - sec = os.stat(path)[ST_MTIME] - return time.strftime("%Y%m%d_%H%M", time.localtime(sec)) + self._dozip = bool(boolean) + def setRemoe(self, boolean): + """Set remoe flag + """ + self._doremove = bool(boolean) + def isPlasmaLogDir(self, path=None): """Check if a path is a valid plasma log directory @@ -77,7 +84,7 @@ pl = os.path.join(path, PLASMA_LOG) if not os.path.isfile(pl): return False - return self.getTimeStamp(pl) + return fileModTime(pl, format="%Y%m%d_%H%M") def zipLogDir(self): """Zip all log files @@ -133,6 +140,17 @@ else: return True - def __call__(self): - self.zipLogDir() - self.removeLogs() + def __len__(self): + """TODO: implementation + """ + return 1 + + def __iter__(self): + """TODO: implementation + """ + for logdir in ("dummy",): + yield logdir + if self._dozip: + self.zipLogDir() + if self._doremove: + self.removeLogs() Added: pymoul/trunk/src/moul/file/utils.py =================================================================== --- pymoul/trunk/src/moul/file/utils.py (rev 0) +++ pymoul/trunk/src/moul/file/utils.py 2007-02-04 15:41:40 UTC (rev 133) @@ -0,0 +1,52 @@ +# pyMoul - Python interface to Myst Online URU Live +# Copyright (C) 2007 Christian Heimes <christian (at) cheimes (dot) de> + +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., 59 +# Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +"""misc utils functions +""" +from __future__ import absolute_import + +__author__ = "Christian Heimes" +__version__ = "$Id: kiimage.py 129 2007-02-03 18:41:23Z tiran $" +__revision__ = "$Revision: 129 $" + +import os +import time +from stat import ST_MTIME +from stat import ST_SIZE + +def fileModTime(path, format=None): + """Get modification time of file + + @param path: path to file or directory + @type path: string + @param format: strftime string ("%Y%m%d_%H%M") + @type format: str or None for time struct + @return: str or time struct + """ + sec = os.stat(path)[ST_MTIME] + if not format: + return time.localtime(sec) + else: + return time.strftime(format, time.localtime(sec)) + +def fileSize(path): + """Get size of file + + @param path: path to file + @type path: string + """ + return os.stat(path)[ST_SIZE] This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ti...@us...> - 2007-02-26 15:05:36
|
Revision: 192 http://pymoul.svn.sourceforge.net/pymoul/?rev=192&view=rev Author: tiran Date: 2007-02-26 07:05:28 -0800 (Mon, 26 Feb 2007) Log Message: ----------- Added more screen res and fixed unit tests Modified Paths: -------------- pymoul/trunk/src/moul/file/tests/test_wdysini.py pymoul/trunk/src/moul/file/wdysini.py Modified: pymoul/trunk/src/moul/file/tests/test_wdysini.py =================================================================== --- pymoul/trunk/src/moul/file/tests/test_wdysini.py 2007-02-25 23:21:47 UTC (rev 191) +++ pymoul/trunk/src/moul/file/tests/test_wdysini.py 2007-02-26 15:05:28 UTC (rev 192) @@ -140,22 +140,22 @@ def test_property_edit(self): p = self.parserClass(self.tmpdir) eq = self.failUnlessEqual - + p.read() self.failIf(p.isChanged()) - + p.vsync = True eq(p._get('Graphics.EnableVSync'), True) self.failUnless(p.isChanged()) p.vsync = False eq(p._get('Graphics.EnableVSync'), False) #XXX self.failIf(p.isChanged()) - + p.screenres = 0 eq(p._get('Graphics.Width'), 800) eq(p._get('Graphics.Height'), 600) - - p.screenres = 10 + + p.screenres = 11 eq(p._get('Graphics.Width'), 1920) eq(p._get('Graphics.Height'), 1200) self.failUnless(p.isChanged()) Modified: pymoul/trunk/src/moul/file/wdysini.py =================================================================== --- pymoul/trunk/src/moul/file/wdysini.py 2007-02-25 23:21:47 UTC (rev 191) +++ pymoul/trunk/src/moul/file/wdysini.py 2007-02-26 15:05:28 UTC (rev 192) @@ -217,10 +217,10 @@ Traceback (most recent call last): ... ConstrainError: name: -1 < min 0 - >>> v(11, debug="name") + >>> v(13, debug="name") Traceback (most recent call last): ... - ConstrainError: name: 11 > max 10 + ConstrainError: name: 13 > max 12 """ def __init__(self): self._min = 0 @@ -252,16 +252,17 @@ """ # width, height, w ratio, h ratio _videomodes = ( - (800, 600, 4, 3), - (1024, 768, 4, 3), - (1152, 864, 4, 3), - (1280, 720, 16, 9), - (1280, 768, 5, 3), - (1280, 800, 16, 10), - (1280, 960, 4, 3), - (1280, 1024, 5, 4), - (1600, 900, 16, 9), - (1600, 1200, 4, 3), + ( 800, 600, 4, 3), + (1024, 768, 4, 3), + (1152, 864, 4, 3), + (1280, 720, 16, 9), + (1280, 768, 5, 3), + (1280, 800, 16, 10), + (1280, 960, 4, 3), + (1280, 1024, 5, 4), + (1600, 900, 16, 9), + (1680, 1050, 10, 10), + (1600, 1200, 4, 3), (1920, 1200, 16, 10), (2560, 1600, 16, 10), ) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ti...@us...> - 2007-03-20 17:28:39
|
Revision: 269 http://pymoul.svn.sourceforge.net/pymoul/?rev=269&view=rev Author: tiran Date: 2007-03-20 10:19:45 -0700 (Tue, 20 Mar 2007) Log Message: ----------- Changed mkdir/makedirs in moul.file and added more logging. The factories mustn't create the directories when its parent directories are missing. Modified Paths: -------------- pymoul/trunk/src/moul/file/chatlog.py pymoul/trunk/src/moul/file/directory.py pymoul/trunk/src/moul/file/kiimage.py Modified: pymoul/trunk/src/moul/file/chatlog.py =================================================================== --- pymoul/trunk/src/moul/file/chatlog.py 2007-03-19 21:08:20 UTC (rev 268) +++ pymoul/trunk/src/moul/file/chatlog.py 2007-03-20 17:19:45 UTC (rev 269) @@ -91,7 +91,7 @@ def clear(self): """Clear results and check the existence of directories - + @return: state of directories, True if everything is ok @rtype: bool """ @@ -99,10 +99,11 @@ if not os.path.isdir(self._srcdir): LOG.warning("%s is not a directory" % self._srcdir) return False - if not os.path.isdir(self._destdir): - LOG.info("Creating chatlog directory %s" % self._destdir) - os.mkdir(self._destdir) - return False + else: + if not os.path.isdir(self._destdir): + LOG.info("Creating chatlog directory %s" % self._destdir) + os.mkdir(self._destdir) + return False # XXX ??? return True def findLogs(self): Modified: pymoul/trunk/src/moul/file/directory.py =================================================================== --- pymoul/trunk/src/moul/file/directory.py 2007-03-19 21:08:20 UTC (rev 268) +++ pymoul/trunk/src/moul/file/directory.py 2007-03-20 17:19:45 UTC (rev 269) @@ -62,7 +62,7 @@ class AbstractUruDirectory(object): """Abstract class for an Uru directory - + @cvar _dirmapping: A mapping of name S{->} subpath @type _dirmapping: dict @cvar _factories: A list of factories name S{->} (callable, name tuples) C{_dirmapping} @@ -80,6 +80,12 @@ @type basepath: str """ self._basepath = basepath + if not self.exists(): + LOG.warning("Basepath %s does not exist for %s!" % + (self._basepath, self.__class__.__name__)) + else: + LOG.debug("Using basepath %s for %s" % + (self._basepath, self.__class__.__name__)) for key in self._factories: if key in self.__class__.__dict__: raise AttributeError("Name %s is reserved and can't be used " @@ -103,10 +109,9 @@ @rtype: str """ if name and name != '.': - path = self._dirmapping[name] + return os.path.join(self._basepath, self._dirmapping[name]) else: - path = '' - return os.path.join(self._basepath, path) + return self._basepath def factory(self, name): """Factory - creates instances to handle subdirectories and files @@ -135,7 +140,7 @@ class UruGameDataDirectory(AbstractUruDirectory): """Uru game directory handler - + An uru game directory contains the launcher, explorer, age files, sound files, localizations and other stuff. """ @@ -152,7 +157,7 @@ class UruPersonalDataDirectory(AbstractUruDirectory): """Uru personal data handler - + An uru personal data directory contains per user data like audio and graphics settings, KI shots, avatar images, logs and pyMoul specific data. @@ -193,7 +198,8 @@ if not name or name == '.': raise ValueError("Cannot create() basedir") path = self.get(name) - os.makedirs(path) + os.mkdir(path) + #os.makedirs(path) # XXX return path def createTree(self): @@ -203,7 +209,8 @@ @rtype: list of str """ created = [] - if not self.exists(''): + if not self.exists(): + raise OSError("Basepath doesn't exist") # XXX os.mkdir(self._basepath) created.append(self._basepath) for key in self._dirmapping: Modified: pymoul/trunk/src/moul/file/kiimage.py =================================================================== --- pymoul/trunk/src/moul/file/kiimage.py 2007-03-19 21:08:20 UTC (rev 268) +++ pymoul/trunk/src/moul/file/kiimage.py 2007-03-20 17:19:45 UTC (rev 269) @@ -226,14 +226,15 @@ if not os.path.isdir(srcdir): LOG.warning("%s is not a directory" % srcdir) - if not os.path.isdir(destdir): - LOG.info("Creating fixed images directory %s" % destdir) - os.mkdir(destdir) + else: + if not os.path.isdir(destdir): + LOG.info("Creating fixed images directory %s" % destdir) + os.mkdir(destdir) @staticmethod def mkFixedName(path, name, dest): """Make new new based on the name of the KI image - + @param path: directory where the file lives @type path: str @param name: name of the image w/o path This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ti...@us...> - 2007-03-20 17:45:30
|
Revision: 271 http://pymoul.svn.sourceforge.net/pymoul/?rev=271&view=rev Author: tiran Date: 2007-03-20 10:42:38 -0700 (Tue, 20 Mar 2007) Log Message: ----------- Workaround for new semantic of createTree Modified Paths: -------------- pymoul/trunk/src/moul/file/directory.py pymoul/trunk/src/moul/file/tests/test_directory.py Modified: pymoul/trunk/src/moul/file/directory.py =================================================================== --- pymoul/trunk/src/moul/file/directory.py 2007-03-20 17:37:52 UTC (rev 270) +++ pymoul/trunk/src/moul/file/directory.py 2007-03-20 17:42:38 UTC (rev 271) @@ -210,7 +210,7 @@ """ created = [] if not self.exists(): - raise OSError("Basepath doesn't exist") # XXX + raise OSError("Basepath %s doesn't exist" % self._basepath) # XXX os.mkdir(self._basepath) created.append(self._basepath) for key in self._dirmapping: Modified: pymoul/trunk/src/moul/file/tests/test_directory.py =================================================================== --- pymoul/trunk/src/moul/file/tests/test_directory.py 2007-03-20 17:37:52 UTC (rev 270) +++ pymoul/trunk/src/moul/file/tests/test_directory.py 2007-03-20 17:42:38 UTC (rev 271) @@ -69,6 +69,7 @@ self.failIf(self.urudir.factory('audioini').exists()) self.failIf(self.urudir.factory('graphicsini').exists()) + os.mkdir(tmpdir) # XXX self.urudir.createTree() self.failUnless(self.urudir.exists()) self.urudir.initializeFactories() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ti...@us...> - 2007-04-19 00:33:30
|
Revision: 286 http://pymoul.svn.sourceforge.net/pymoul/?rev=286&view=rev Author: tiran Date: 2007-04-18 17:33:11 -0700 (Wed, 18 Apr 2007) Log Message: ----------- Fixes to SDL and Stream class Modified Paths: -------------- crypttea/trunk/src/crypttea/_common.py pymoul/trunk/src/moul/crypt/stream.py pymoul/trunk/src/moul/file/sdl.py Modified: crypttea/trunk/src/crypttea/_common.py =================================================================== --- crypttea/trunk/src/crypttea/_common.py 2007-04-18 23:46:47 UTC (rev 285) +++ crypttea/trunk/src/crypttea/_common.py 2007-04-19 00:33:11 UTC (rev 286) @@ -208,7 +208,7 @@ fmt = self._endian+"2L" result = [] if l % bs != 0: - raise ValueError + raise ValueError(l) for i in range(l/bs): v = unpack(fmt, block[i*bs:(i+1)*bs]) w = func(v, key, rounds) Modified: pymoul/trunk/src/moul/crypt/stream.py =================================================================== --- pymoul/trunk/src/moul/crypt/stream.py 2007-04-18 23:46:47 UTC (rev 285) +++ pymoul/trunk/src/moul/crypt/stream.py 2007-04-19 00:33:11 UTC (rev 286) @@ -32,11 +32,10 @@ """Encrypted stream """ magic = 12 * '\x00' - cipherClass = None blockSize = 1024 - def __init__(self, fdname, key, **kwargs): - if isinstane(fdname, basestring): + def __init__(self, fdname, cipher): + if isinstance(fdname, basestring): fd = BinaryFile(fdname) elif isinstance(fdname, BinaryFile): fd = fdname @@ -51,22 +50,23 @@ fd.seek(16) if fsize-16 < self._size: raise ValueError("Size mismatch %i/%i" % (fsize, self._size)) - self._cipher = self.cipherClass(key, **kwargs) + self._cipher = cipher self._fd = fd def read(self): + fd = self._fd fd.seek(16) remain = self._size bs = self.blockSize - fd = self._fd decrypt = self._cipher.decrypt buf = [] while True: - if remain == 0: + if remain <= 0: break + data = fd.read(bs) + data = decrypt(data) if remain < bs: - bs = remain - data = fd.read(bs) - buf.append(decrypt(data)) + data = data[:remain] + buf.append(data) + remain -= bs return ''.join(buf) - Modified: pymoul/trunk/src/moul/file/sdl.py =================================================================== --- pymoul/trunk/src/moul/file/sdl.py 2007-04-18 23:46:47 UTC (rev 285) +++ pymoul/trunk/src/moul/file/sdl.py 2007-04-19 00:33:11 UTC (rev 286) @@ -25,30 +25,30 @@ import re -class SDLReader(object): - """Read Python pak +class SDLParser(object): + """Read SDL Files - >>> mo = SDLReader.comment.match('abc # def # efg') + >>> mo = SDLParser.comment.match('abc # def # efg') >>> mo.group('content') 'abc ' - >>> mo = SDLReader.comment.match('abc') + >>> mo = SDLParser.comment.match('abc') >>> mo.group('content') 'abc' - >>> mo = SDLReader.comment.match('# abc') + >>> mo = SDLParser.comment.match('# abc') >>> mo.group('content') '' - >>> mo = SDLReader.statedesc.match('STATEDESC testage') + >>> mo = SDLParser.statedesc.match('STATEDESC testage') >>> mo.group('name') 'testage' - >>> mo = SDLReader.version.match(' VERSION 5') + >>> mo = SDLParser.version.match(' VERSION 5') >>> mo.group('version') '5' - >>> mo = SDLReader.var.search('VAR BOOL testvar[1] DEFAULT=1') + >>> mo = SDLParser.var.search('VAR BOOL testvar[1] DEFAULT=1') >>> mo.group('type') 'BOOL' >>> mo.group('name') @@ -60,9 +60,11 @@ statedesc = re.compile("^STATEDESC[ ]+(?P<name>\w+)") version = re.compile("[ \t]*VERSION[ ]+(?P<version>\d+)") var = re.compile("VAR[ ]+(?P<type>\w+)[ ]+(?P<name>[$\w]+)\[1\](?P<tail>.*)") - def __init__(self, fname): - self._fname = fname - self._fd = open(fname, 'r') + def __init__(self, fdname): + if isinstance(fdname, basestring): + self._fd = open(fdname, 'r') + else: + self._fd = fdname self.states = [] self._parseFile() @@ -71,7 +73,7 @@ #line = self.comment.match(line).group('content') mo = self.statedesc.match(line) if mo: - self.states.append(mo.group('name') + self.states.append(mo.group('name')) class SDLStates(object): """SDL state object This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |