[Pymoul-svn] SF.net SVN: pymoul: [113] pymoul/trunk/src/moul
Status: Alpha
Brought to you by:
tiran
|
From: <ti...@us...> - 2007-02-01 03:07:31
|
Revision: 113
http://pymoul.svn.sourceforge.net/pymoul/?rev=113&view=rev
Author: tiran
Date: 2007-01-31 19:07:31 -0800 (Wed, 31 Jan 2007)
Log Message:
-----------
Moved stuff from config to *Directory classes
Modified Paths:
--------------
pymoul/trunk/src/moul/config/__init__.py
pymoul/trunk/src/moul/file/directory.py
Modified: pymoul/trunk/src/moul/config/__init__.py
===================================================================
--- pymoul/trunk/src/moul/config/__init__.py 2007-02-01 02:39:34 UTC (rev 112)
+++ pymoul/trunk/src/moul/config/__init__.py 2007-02-01 03:07:31 UTC (rev 113)
@@ -69,7 +69,7 @@
return value
elif default is _marker:
raise KeyError(section)
-
+
def setOption(self, section, name=None, value=_marker):
"""Set configuratin option
"""
@@ -82,15 +82,15 @@
del sec[name]
else:
sec[name] = value
-
+
def addSection(self, section):
"""Add a new configuration section
"""
return self.setdefault(section, {})
-
+
def listConfig(self):
"""List
-
+
>>> cfg = listConfig()
>>> len(cfg) > 0
True
@@ -115,35 +115,15 @@
## directories
class Directories(object):
-
- _dirmapping = {
- 'install' : "%(installdir)s",
- 'loc' : "%(installdir)s/dat",
- 'dat' : "%(installdir)s/dat",
- 'sound' : "%(installdir)s/sfx",
- 'soundwav' : "%(installdir)s/sfx/streamingCache",
- 'video' : "%(installdir)s/avi",
-
- 'userdata' : "%(datadir)s",
- 'log' : "%(datadir)s/Log",
- 'kiimages' : "%(datadir)s/KIimages",
- 'avatars' : "%(datadir)s/Avatars",
- 'chatlogs' : "%(datadir)s/chatlogs",
- 'ini' : "%(datadir)s/init",
-
- 'pymoul.ini' : "%(pymouldir)s/pymoul.ini",
- }
-
+ """TODO: to be replaced or removed
+ """
+
def __init__(self, config):
- self._cache = {}
self._config = config
-
- def clearCache(self):
- self._cache.clear()
def getMoulInstallDir(self):
return self._config.getOption('moul.installdir')
-
+
def getMoulUserDataDir(self):
return getMoulUserDataDir()
@@ -153,31 +133,11 @@
def lookupDir(self, name):
"""Lookup MOUL directory
"""
- cached = self._cache.get(name, None)
- if cached:
- return cached
- path = self._dirmapping.get(name)
- path = path.replace('/', os.sep)
- map = {'installdir' : self.getMoulInstallDir(),
- 'datadir' : self.getMoulUserDataDir(),
- 'pymouldir' : self.getPyMoulDataDir(),
+ map = {'install' : self.getMoulInstallDir,
+ 'userdata' : self.getMoulUserDataDir,
+ 'pymouldir' : self.getPyMoulDataDir,
}
- fullpath = path % map
- #self._cache[name] = fullpath
- return fullpath
-
- def listDirs(self):
- """List all MOUL dirs
-
- >>> dirs = listDirs()
- >>> len(dirs)
- 13
- >>> type(dirs)
- <type 'dict'>
- """
- return dict([(name, self.lookupDir(name)) for name in self._dirmapping])
+ return map[name]()
_directories = Directories(_configuration)
-clearCache = _directories.clearCache
lookupDir = _directories.lookupDir
-listDirs = _directories.listDirs
Modified: pymoul/trunk/src/moul/file/directory.py
===================================================================
--- pymoul/trunk/src/moul/file/directory.py 2007-02-01 02:39:34 UTC (rev 112)
+++ pymoul/trunk/src/moul/file/directory.py 2007-02-01 03:07:31 UTC (rev 113)
@@ -34,6 +34,25 @@
LOG = getLogger('moul.file.directory')
+class DirectoryCount(object):
+ """Counts how many files are inside a directory
+ """
+
+ def __init__(self, path):
+ 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
+
class AbstractUruDirectory(object):
"""Abstract class for an Uru directory
"""
@@ -66,6 +85,8 @@
"""
args = []
factory, argnames = self._factories[name]
+ if not self.exists(argnames[0]):
+ return None
for name in argnames:
args.append(self.get(name))
return factory(*args)
@@ -112,19 +133,27 @@
'graphics.ini' : (GraphicsIni, ('ini',)),
'audio.ini' : (AudioIni, ('ini',)),
'chatlogs' : (ChatlogDirectoryView, ('chatlogs',)),
- 'zipped' : None,
- 'fixed' : None,
+ 'zipped' : (DirectoryCount, ('zipped',)),
+ 'fixed' : (DirectoryCount, ('fixed',)),
}
+ def create(self, name):
+ """Create a directory
+ """
+ if not name or name == '.':
+ raise ValueError("Cannot create() basedir")
+ path = self.get(name)
+ os.makedirs(path)
+
def createTree(self):
"""Create the directory tree
"""
created = []
- if self.exists(''):
+ if not self.exists(''):
os.mkdir(self._basepath)
created.append(self._basepath)
for key in self._dirmapping:
if not self.exists(key):
- os.mkdir(path)
+ self.create(key)
created.append(path)
return created
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|