xmlio.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """
  2. xmlio.py
  3. Mixin class to support pygene objects in
  4. loading/saving as xml
  5. """
  6. import StringIO
  7. from xml.dom.minidom import getDOMImplementation, parse, parseString
  8. domimpl = getDOMImplementation()
  9. class PGXmlMixin(object):
  10. """
  11. mixin class to support pygene classes
  12. serialising themselves to/from xml
  13. """
  14. def xmlDump(self, fileobj):
  15. """
  16. Dumps out the population to an open file in XML format.
  17. To dump to a string, use .xmlDumps() instead
  18. """
  19. doc = domimpl.createDocument(None, "pygene", None)
  20. top = doc.documentElement
  21. top.appendChild(doc.createComment(
  22. "generated by pygene - http://www.freenet.org.nz/python/pygene"))
  23. self.xmlDumpSelf(doc, top)
  24. fileobj.write(doc.toxml())
  25. def xmlDumps(self):
  26. """
  27. dumps out to xml, returning a string of the raw
  28. generated xml
  29. """
  30. s = StringIO.StringIO()
  31. self.xmlDump(s)
  32. return s.getvalue()
  33. def xmlDumpSelf(self, doc, parent):
  34. """
  35. Writes out the contents of this population
  36. into the xml tree
  37. """
  38. raise Exception("class %s: xmlDumpSelf not implemented" % \
  39. self.__class__.__name__)
  40. def xmlDumpClass(self, tag):
  41. """
  42. dumps out class information
  43. """
  44. tag.setAttribute("class", self.__class__.__name__)
  45. tag.setAttribute("module", self.__class__.__module__)
  46. def xmlDumpAttribs(self, tag):
  47. """
  48. """