Changeset d608a90


Ignore:
Timestamp:
Nov 11, 2010 7:31:00 PM (9 years ago)
Author:
Ted Faber <faber@…>
Branches:
version-3.02
Children:
1553d27
Parents:
891fcf1
Message:

Import the better XML for topdl

File:
1 edited

Legend:

Unmodified
Added
Removed
  • fedd/federation/topdl.py

    r8aaf8f8 rd608a90  
    33import re
    44import xml.parsers.expat
     5from xml.sax.saxutils import escape
     6from base64 import b64encode
     7from string import join
    58
    69from fedid import fedid as fedid_class
     
    7881    def to_dict(self):
    7982        return { 'attribute': self.attribute, 'value': self.value }
     83    def to_xml(self):
     84        return "<attribute>%s</attribute><value>%s</value>" % \
     85                (escape(self.attribute), escape(self.value))
    8086
    8187class Capacity(base):
     
    9096        return { 'rate': float(self.rate), 'kind': self.kind }
    9197
     98    def to_xml(self):
     99        return "<rate>%f</rate><kind>%s</kind>" % (self.rate, self.kind)
     100
    92101class Latency(base):
    93102    def __init__(self, time, kind):
     
    100109    def to_dict(self):
    101110        return { 'time': float(self.time), 'kind': self.kind }
     111
     112    def to_xml(self):
     113        return "<time>%f</time><kind>%s</kind>" % (self.time, self.kind)
    102114
    103115class Substrate(base):
     
    132144        return rv
    133145
     146    def to_xml(self):
     147        rv = "<name>%s</name>" % escape(self.name)
     148        if self.capacity is not None:
     149            rv += "<capacity>%s</capacity>" % self.capacity.to_xml()
     150        if self.latency is not None:
     151            rv += "<latency>%s</latency>" % self.latency.to_xml()
     152       
     153        if self.attribute:
     154            rv += join(["<attribute>%s</attribute>" % a.to_xml() \
     155                    for a in self.attribute], "")
     156        return rv
     157
    134158class CPU(base):
    135159    def __init__(self, type, attribute=[]):
     
    147171            rv['attribute'] = [ a.to_dict() for a in self.attribute ]
    148172        return rv
     173
     174    def to_xml(self):
     175        rv = "<type>%s</type>" % escape(self.type)
     176        if self.attribute:
     177            rv += join(["<attribute>%s</attribute>" % a.to_xml() \
     178                    for a in self.attribute], "")
     179        return rv
     180
    149181
    150182class Storage(base):
     
    164196            rv['attribute'] = [ a.to_dict() for a in self.attribute ]
    165197        return rv
     198
     199    def to_xml(self):
     200        rv = "<amount>%f</amount><persistence>%s</persistence>" % \
     201                (self.amount, escape(self.persistence))
     202        if self.attribute:
     203            rv += join(["<attribute>%s</attribute>" % a.to_xml() \
     204                    for a in self.attribute], "")
     205        return rv
     206
    166207
    167208class OperatingSystem(base):
     
    186227        if self.name: rv['name'] = self.name
    187228        if self.version: rv['version'] = self.version
    188         if self.distribution: rv['version'] = self.distribution
    189         if self.distributionversion: rv['version'] = self.distributionversion
     229        if self.distribution: rv['distribution'] = self.distribution
     230        if self.distributionversion:
     231            rv['distributionversion'] = self.distributionversion
    190232        if self.attribute:
    191233            rv['attribute'] = [ a.to_dict() for a in self.attribute ]
    192234        return rv
     235
     236    def to_xml(self):
     237        rv = ""
     238        if self.name: rv += "<name>%s</name>" % escape(self.name)
     239        if self.version: rv += "<version>%s</version>" % escape(self.version)
     240        if self.distribution:
     241            rv += "<distribution>%s</distribution>" % escape(self.distribution)
     242        if self.distributionversion:
     243            rv += "<distributionversion>%s</distributionversion>" % \
     244                    escape(self.distributionversion)
     245       
     246        if self.attribute:
     247            rv += join(["<attribute>%s</attribute>" % a.to_xml() \
     248                    for a in self.attribute], "")
     249        return rv
     250
    193251
    194252class Software(base):
     
    209267            rv['attribute'] = [ a.to_dict() for a in self.attribute ]
    210268        return rv
     269
     270    def to_xml(self):
     271        rv = "<location>%s</location>" % escape(self.location)
     272        if self.install: rv += "<install>%s</install>" % self.install
     273        if self.attribute:
     274            rv += join(["<attribute>%s</attribute>" % a.to_xml() \
     275                    for a in self.attribute], "")
     276        return rv
     277
    211278
    212279class Interface(base):
     
    243310            rv['attribute'] = [ a.to_dict() for a in self.attribute ]
    244311        return rv
     312
     313    def to_xml(self):
     314        rv = join(["<substrate>%s</substrate>" % escape(s) \
     315                for s in self.substrate], "")
     316        rv += "<name>%s</name>" % self.name
     317        if self.capacity:
     318            rv += "<capacity>%s</capacity>" % self.capacity.to_xml()
     319        if self.latency:
     320            rv += "<latency>%s</latency>" % self.latency.to_xml()
     321        if self.attribute:
     322            rv += join(["<attribute>%s</attribute>" % a.to_xml() \
     323                    for a in self.attribute], "")
     324        return rv
     325
    245326
    246327class ID(base):
     
    264345        if self.localname: rv['localname'] = self.localname
    265346        if self.kerberosUsername: rv['kerberosUsername'] = self.kerberosUsername
     347        return rv
     348
     349    def to_xml(self):
     350        if self.uuid: rv = "<uuid>%s</uuid>" % b64encode(self.uuid)
     351        elif self.fedid: rv = "<fedid>%s</fedid>" % b64encode(self.fedid)
     352        elif self.uri: rv = "<uri>%s</uri>" % escape(self.uri)
     353        elif self.localname:
     354            rv = "<localname>%s</localname>" % escape(self.localname)
     355        elif self.kerberosUsername:
     356            rv = "<kerberosUsername>%s</kerberosUsername>" % \
     357                    escape(self.kerberosUsername)
    266358        return rv
    267359
     
    314406        return { 'computer': rv }
    315407
     408    def to_xml(self):
     409        rv = "<name>%s</name>" % escape(self.name)
     410        if self.cpu:
     411            rv += join(["<cpu>%s</cpu>" % c.to_xml() for c in self.cpu], "")
     412        if self.os:
     413            rv += join(["<os>%s</os>" % o.to_xml() for o in self.os], "")
     414        if self.software:
     415            rv += join(["<software>%s</software>" % s.to_xml() \
     416                    for s in self.software], "")
     417        if self.storage:
     418            rv += join(["<stroage>%s</stroage>" % s.to_xml() \
     419                    for s in self.stroage], "")
     420        if self.interface:
     421            rv += join(["<interface>%s</interface>" % i.to_xml()
     422                for i in self.interface], "")
     423        if self.attribute:
     424            rv += join(["<attribute>%s</attribute>" % a.to_xml() \
     425                    for a in self.attribute], "")
     426        return "<computer>%s</computer>" % rv
     427
     428
    316429
    317430class Testbed(base):
     
    338451            rv['attribute'] = [ a.to_dict() for a in self.attribute]
    339452        return { 'testbed': rv }
     453
     454    def to_xml(self):
     455        rv = "<uri>%s</uri><type>%s</type>" % \
     456                (escape(self.uri), escape(self.type))
     457        if self.interface:
     458            rv += join(["<interface>%s</interface>" % i.to_xml()
     459                for i in self.interface], "")
     460        if self.attribute:
     461            rv += join(["<attribute>%s</attribute>" % a.to_xml() \
     462                    for a in self.attribute], "")
     463        return "<testbed>%s</testbed>" % rv
     464
     465       
    340466
    341467class Segment(base):
     
    365491        return { 'segment': rv }
    366492
     493    def to_xml(self):
     494        rv = "<id>%s</id><uri>%s</uri><type>%s</type>" % \
     495                (id.to_xml(), escape(self.uri), escape(self.type))
     496        if self.interface:
     497            rv += join(["<interface>%s</interface>" % i.to_xml()
     498                for i in self.interface], "")
     499        if self.attribute:
     500            rv += join(["<attribute>%s</attribute>" % a.to_xml() \
     501                    for a in self.attribute], "")
     502        return "<segment>%s</segment>" % rv
    367503
    368504class Other(base):
     
    385521        return {'other': rv }
    386522
     523    def to_xml(self):
     524        rv = ""
     525        if self.interface:
     526            rv += join(["<interface>%s</interface>" % i.to_xml()
     527                for i in self.interface], "")
     528        if self.attribute:
     529            rv += join(["<attribute>%s</attribute>" % a.to_xml() \
     530                    for a in self.attribute], "")
     531        return "<other>%s</other>" % rv
    387532
    388533class Topology(base):
     
    490635            rv['attribute'] = [ s.to_dict() for s in self.attribute]
    491636        return rv
     637
     638    def to_xml(self):
     639        rv = ""
     640        if self.substrates:
     641            rv += join(["<substrates>%s</substrates>" % s.to_xml() \
     642                    for s in self.substrates], "")
     643        if self.elements:
     644            rv += join(["<elements>%s</elements>" % e.to_xml() \
     645                    for e in self.elements], "")
     646        if self.attribute:
     647            rv += join(["<attribute>%s</attribute>" % a.to_xml() \
     648                    for a in self.attribute], "")
     649        return rv
     650
    492651
    493652def topology_from_xml(string=None, file=None, filename=None, top="topology"):
     
    576735def topology_to_xml(t, top=None):
    577736    """
    578     Print the topology as XML.  This is quick and dirty, but should work for
    579     many purposes.  Convert the topology to a dict and print it recursively.
     737    Print the topology as XML, recursively using the internal classes to_xml()
     738    methods.
    580739    """
    581     from xml.sax.saxutils import escape
    582 
    583     def dict_to_xml(e, top=None):
    584         if top: rv = "<%s>" % top
    585         else: rv = ""
    586 
    587         for k in e.keys():
    588             if isinstance(e[k], basestring):
    589                 rv += "<%s>%s</%s>" % (k, escape(e[k]), k)
    590             elif isinstance(e[k], (int, float, long)):
    591                 rv += "<%s>%d</%s>" % (k, e[k], k)
    592             elif isinstance(e[k], dict):
    593                 rv += "<%s>%s</%s>" % (k, dict_to_xml(e[k]), k)
    594             elif getattr(e[k], '__iter__', None):
    595                 for ee in e[k]:
    596                     if isinstance(ee, dict):
    597                         rv += "<%s>%s</%s>" % (k, dict_to_xml(ee), k)
    598                     else:
    599                         rv += "<%s>%s</%s>" % (k, escape(ee), k)
    600             else:
    601                 try:
    602                     rv += "<%s>%s</%s>" % (k, e[k], k)
    603                 except Exception:
    604                     raise ConsistencyError("What is this?? %s %s" % (k, e[k]))
    605         if top: rv += "</%s>" % top
    606         return rv
    607 
    608     return dict_to_xml(t.to_dict(), top)
    609 
     740
     741    if top: return "<%s>%s</%s>" % (top, t.to_xml(), top)
     742    else: return t.to_xml()
    610743
    611744def topology_to_vtopo(t):
Note: See TracChangeset for help on using the changeset viewer.