From 7cbef35b32ee422d802dae7d61c8b4644d35f170 Mon Sep 17 00:00:00 2001 From: Narayan Desai Date: Tue, 6 Jan 2004 06:20:22 +0000 Subject: allow FileBacked to create files 2004/01/05 23:43:58-06:00 anl.gov!desai make FileBacked Read-Write (Logical change 1.12) git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@43 ce84e21b-d406-0410-9b95-82705330c041 --- src/GeneratorUtils.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/GeneratorUtils.py b/src/GeneratorUtils.py index 69fe86e3f..03895159f 100644 --- a/src/GeneratorUtils.py +++ b/src/GeneratorUtils.py @@ -5,14 +5,18 @@ 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.''' + '''FileBacked is a class that will cache file data and automatically reload it as required from disk.''' def __init__(self,filename): '''Setup initial structures''' self.filename = filename - self.mtime = stat(filename)[ST_MTIME] - self._data = file(filename).read() + try: + self.mtime = stat(filename)[ST_MTIME] + self._data = file(filename).read() + except OSError: + self.mtime = 0 + self._data = None + self.setdata('') def getdata(self): mtime = stat(self.filename)[ST_MTIME] @@ -22,12 +26,16 @@ class FileBacked(object): return self._data def setdata(self,val): - pass + if val != self._data: + self._data = val + file(self.filename,'w').write(val) + self.mtime = stat(self.filename)[ST_MTIME] data=property(getdata,setdata) class DirectoryBacked(object): - '''DirectoryBacked caches a complete directory (including proper negative caching)''' + '''DirectoryBacked caches a complete directory (including proper negative caching). + This class is READ-ONLY.''' def __init__(self,path): self.path = path -- cgit v1.2.3-1-g7c22