Changeset a914b1b for fedd/federation
- Timestamp:
- Nov 11, 2010 7:24:52 PM (14 years ago)
- Branches:
- axis_example, compt_changes, info-ops, master
- Children:
- 9ecda47
- Parents:
- 549830d
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fedd/federation/topdl.py
r549830d ra914b1b 3 3 import re 4 4 import xml.parsers.expat 5 from xml.sax.saxutils import escape 6 from base64 import b64encode 7 from string import join 5 8 6 9 from fedid import fedid as fedid_class … … 78 81 def to_dict(self): 79 82 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)) 80 86 81 87 class Capacity(base): … … 90 96 return { 'rate': float(self.rate), 'kind': self.kind } 91 97 98 def to_xml(self): 99 return "<rate>%f</rate><kind>%s</kind>" % (self.rate, self.kind) 100 92 101 class Latency(base): 93 102 def __init__(self, time, kind): … … 100 109 def to_dict(self): 101 110 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) 102 114 103 115 class Substrate(base): … … 132 144 return rv 133 145 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 134 158 class CPU(base): 135 159 def __init__(self, type, attribute=[]): … … 147 171 rv['attribute'] = [ a.to_dict() for a in self.attribute ] 148 172 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 149 181 150 182 class Storage(base): … … 164 196 rv['attribute'] = [ a.to_dict() for a in self.attribute ] 165 197 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 166 207 167 208 class OperatingSystem(base): … … 186 227 if self.name: rv['name'] = self.name 187 228 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 190 232 if self.attribute: 191 233 rv['attribute'] = [ a.to_dict() for a in self.attribute ] 192 234 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 193 251 194 252 class Software(base): … … 209 267 rv['attribute'] = [ a.to_dict() for a in self.attribute ] 210 268 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 211 278 212 279 class Interface(base): … … 243 310 rv['attribute'] = [ a.to_dict() for a in self.attribute ] 244 311 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 245 326 246 327 class ID(base): … … 264 345 if self.localname: rv['localname'] = self.localname 265 346 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) 266 358 return rv 267 359 … … 314 406 return { 'computer': rv } 315 407 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 316 429 317 430 class Testbed(base): … … 338 451 rv['attribute'] = [ a.to_dict() for a in self.attribute] 339 452 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 340 466 341 467 class Segment(base): … … 365 491 return { 'segment': rv } 366 492 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 367 503 368 504 class Other(base): … … 385 521 return {'other': rv } 386 522 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 387 532 388 533 class Topology(base): … … 490 635 rv['attribute'] = [ s.to_dict() for s in self.attribute] 491 636 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 492 651 493 652 def topology_from_xml(string=None, file=None, filename=None, top="topology"): … … 576 735 def topology_to_xml(t, top=None): 577 736 """ 578 Print the topology as XML . This is quick and dirty, but should work for579 m any 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. 580 739 """ 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() 610 743 611 744 def topology_to_vtopo(t):
Note: See TracChangeset
for help on using the changeset viewer.