summaryrefslogtreecommitdiffstats
path: root/src/lib/Server
diff options
context:
space:
mode:
authorNarayan Desai <desai@mcs.anl.gov>2004-08-11 19:54:35 +0000
committerNarayan Desai <desai@mcs.anl.gov>2004-08-11 19:54:35 +0000
commit38910ac7557b4051b19ed33f47b535979b118eaa (patch)
tree5c15e43228551f8526bb535e338a5b36bc34ee0f /src/lib/Server
parentf2fb5194f70100598f98be25f1d30ffe2e6af08e (diff)
downloadbcfg2-38910ac7557b4051b19ed33f47b535979b118eaa.tar.gz
bcfg2-38910ac7557b4051b19ed33f47b535979b118eaa.tar.bz2
bcfg2-38910ac7557b4051b19ed33f47b535979b118eaa.zip
Rename: src/GeneratorUtils.py -> src/lib/Server/GeneratorUtils.py
(Logical change 1.33) git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@184 ce84e21b-d406-0410-9b95-82705330c041
Diffstat (limited to 'src/lib/Server')
-rw-r--r--src/lib/Server/GeneratorUtils.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/lib/Server/GeneratorUtils.py b/src/lib/Server/GeneratorUtils.py
index e69de29bb..13a9bedcf 100644
--- a/src/lib/Server/GeneratorUtils.py
+++ b/src/lib/Server/GeneratorUtils.py
@@ -0,0 +1,77 @@
+#!/usr/bin/env python
+# $Id: $
+
+from elementtree.ElementTree import XML
+
+class FileBacked(object):
+ '''This object caches file data in memory.
+ HandleEvent is called whenever fam registers an event.
+ Index can parse the data into member data as required.'''
+
+ def __init__(self, name):
+ self.name = name
+ self.HandleEvent()
+
+ def HandleEvent(self, event=None):
+ self.data = file(self.name).read()
+ self.Index()
+
+ def Index(self):
+ pass
+
+class DirectoryBacked(object):
+ __child__ = FileBacked
+
+ def __init__(self, name, fam):
+ self.name = name
+ self.fam = fam
+ self.entries = {}
+ self.inventory = False
+ fam.AddMonitor(name, self)
+
+ def __getitem__(self, key):
+ return self.entries[key]
+
+ def __iter__(self):
+ return self.entries.iteritems()
+
+ def AddEntry(self, name):
+ if self.entries.has_key(name):
+ print "got multiple adds"
+ else:
+ self.entries[name] = self.__child__('%s/%s'%(self.name, name))
+ self.entries[name].HandleEvent()
+
+ def HandleEvent(self, event):
+ action = event.code2str()
+ if action == 'exists':
+ if event.filename != self.name:
+ self.AddEntry(event.filename)
+ elif action == 'created':
+ self.AddEntry(event.filename)
+ elif action == 'changed':
+ self.entries[event.filename].HandleEvent(event)
+ elif action == 'deleted':
+ if self.entries.has_key(event.filename):
+ del self.entries[event.filename]
+ elif action in ['endExist']:
+ pass
+ else:
+ print "Got unknown event %s %s %s"%(event.requestID, event.code2str(), event.filename)
+
+class XMLFileBacked(FileBacked):
+ __identifier__ = 'name'
+
+ def Index(self):
+ a = XML(self.data)
+ self.label = a.attrib[self.__identifier__]
+ self.entries = a.getchildren()
+
+ def __iter__(self):
+ return iter(self.entries)
+
+class SingleXMLFileBacked(XMLFileBacked):
+ def __init__(self,filename,fam):
+ XMLFileBacked.__init__(self, filename)
+ fam.AddMonitor(filename, self)
+