summaryrefslogtreecommitdiffstats
path: root/src/GeneratorUtils.py
diff options
context:
space:
mode:
authorNarayan Desai <desai@mcs.anl.gov>2004-01-06 05:30:25 +0000
committerNarayan Desai <desai@mcs.anl.gov>2004-01-06 05:30:25 +0000
commit90238504a2dc317e37615f4094729ec573b7939e (patch)
treede8d900fe122cdaf98c9e429de4882a748adcd73 /src/GeneratorUtils.py
parent435159b9bd8b1365b1972c89ce746d8180d1841b (diff)
downloadbcfg2-90238504a2dc317e37615f4094729ec573b7939e.tar.gz
bcfg2-90238504a2dc317e37615f4094729ec573b7939e.tar.bz2
bcfg2-90238504a2dc317e37615f4094729ec573b7939e.zip
(Logical change 1.10)
git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@38 ce84e21b-d406-0410-9b95-82705330c041
Diffstat (limited to 'src/GeneratorUtils.py')
-rw-r--r--src/GeneratorUtils.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/GeneratorUtils.py b/src/GeneratorUtils.py
index e69de29bb..69fe86e3f 100644
--- a/src/GeneratorUtils.py
+++ b/src/GeneratorUtils.py
@@ -0,0 +1,56 @@
+#!/usr/bin/env python
+# $Id$
+
+from os import listdir, stat
+from stat import ST_MTIME
+
+class FileBacked(object):
+ '''FileBacked is a class that will cache file data and automatically reload it as required from disk.
+ This class is currently READ-ONLY.'''
+
+ def __init__(self,filename):
+ '''Setup initial structures'''
+ self.filename = filename
+ self.mtime = stat(filename)[ST_MTIME]
+ self._data = file(filename).read()
+
+ def getdata(self):
+ mtime = stat(self.filename)[ST_MTIME]
+ if mtime != self.mtime:
+ self._data = file(self.filename).read()
+ self.mtime = mtime
+ return self._data
+
+ def setdata(self,val):
+ pass
+
+ data=property(getdata,setdata)
+
+class DirectoryBacked(object):
+ '''DirectoryBacked caches a complete directory (including proper negative caching)'''
+
+ def __init__(self,path):
+ self.path = path
+ self._entries = {}
+ self.mtime = stat(path)[ST_MTIME]
+ for entry in listdir(path):
+ self._entries[entry] = FileBacked("%s/%s"%(path,entry))
+
+ def GetEntries(self):
+ mtime = stat(self.path)[ST_MTIME]
+ if mtime != self.mtime:
+ current = self._entries.keys()
+ new = listdir(self.path)
+ for key in new:
+ if key not in current:
+ self._entries[key] = FileBacked("%s/%s"%(self.path,key))
+ for key in current:
+ if key not in new:
+ del self._entries[key]
+ return self._entries
+
+ def SetEntries(self,val):
+ pass
+
+ entries = property(GetEntries,SetEntries)
+