[Pymoul-svn] SF.net SVN: pymoul: [114] pymoul/trunk/src/moul/file
Status: Alpha
Brought to you by:
tiran
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. |