From 90238504a2dc317e37615f4094729ec573b7939e Mon Sep 17 00:00:00 2001 From: Narayan Desai Date: Tue, 6 Jan 2004 05:30:25 +0000 Subject: (Logical change 1.10) git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@38 ce84e21b-d406-0410-9b95-82705330c041 --- src/GeneratorUtils.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'src/GeneratorUtils.py') 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) + -- cgit v1.2.3-1-g7c22