From 1106b939ce92a1280e1b8819b357c6294683248e Mon Sep 17 00:00:00 2001 From: Narayan Desai Date: Thu, 7 Oct 2004 03:22:28 +0000 Subject: switch to profile based metadata - change data structures - setup default profile support w/writeback (Logical change 1.76) git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@374 ce84e21b-d406-0410-9b95-82705330c041 --- src/lib/Server/Metadata.py | 49 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 12 deletions(-) (limited to 'src/lib/Server/Metadata.py') diff --git a/src/lib/Server/Metadata.py b/src/lib/Server/Metadata.py index 6b81e73c2..31769e40e 100644 --- a/src/lib/Server/Metadata.py +++ b/src/lib/Server/Metadata.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -from elementtree.ElementTree import XML +from elementtree.ElementTree import XML, tostring, SubElement from time import localtime, mktime from Generator import SingleXMLFileBacked @@ -82,19 +82,44 @@ class Metadata(object): return False return True +class Profile(object): + def __init__(self, xml): + self.classes = map(lambda x:x.attrib['name'], xml.findall("Class")) + self.attributes = map(lambda x:"%s.%s"%(x.attrib['scope'],x.attrib['name']), xml.findall("Attribute")) + class MetadataStore(SingleXMLFileBacked): def Index(self): + self.element = XML(self.data) + self.defaults = {} self.clients = {} + self.profiles = {} self.classes = {} - self.element = XML(self.data) + for p in self.element.findall("Profile"): + self.profiles[p.attrib['name']] = Profile(p) + for c in self.element.findall("Client"): + self.clients[c.attrib['name']] = (c.attrib['image'], c.attrib['profile']) for c in self.element.findall("Class"): - self.classes[c.attrib['name']] = map(lambda x:x.attrib['name'], c.findall(".//Bundle")) - for client in self.element.findall('Client'): - attributes = map(lambda x:"%s.%s"%(x.attrib['scope'],x.attrib['name']), - client.findall(".//Attribute")) - classes = map(lambda x:x.attrib['name'], client.findall(".//Class")) - bundles = reduce(lambda x,y:x+y, map(lambda z:self.classes.get(z,[]), classes),[]) - for b in bundles: - if bundles.count(b) > 1: bundles.remove(b) - self.clients[client.attrib['name']] = Metadata(False, client.attrib['image'], classes, - bundles, attributes, client.attrib['name']) + self.classes[c.attrib['name']] = map(lambda x:x.attrib['name'], c.findall("Bundle")) + for (k,v) in self.element.attrib.iteritems(): + if k[:8] == 'default_': + self.defaults[k[8:]] = v + + def FetchMetadata(self,client): + if self.clients.has_key(client): + (image,profile) = self.clients[client] + else: + # default profile stuff goes here + (image,profile) = (self.defaults['image'], self.defaults['profile']) + SubElement(self.element, "Client", name=client, profile=profile, image=image) + self.WriteBack() + p = self.profiles[profile] + # should we uniq here? V + bundles = reduce(lambda x,y:x+y, map(self.classes.get, p.classes)) + return Metadata(False, image, p.classes, bundles, p.attributes, client) + + def WriteBack(self): + # write changes to file back to fs + f = open(self.name, 'w') + f.write(tostring(self.element)) + f.close() + -- cgit v1.2.3-1-g7c22