[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. |