"""
xmlio.py

Mixin class to support pygene objects in
loading/saving as xml
"""

import io
from xml.dom.minidom import getDOMImplementation, parse, parseString

domimpl = getDOMImplementation()

class PGXmlMixin(object):
    """
    mixin class to support pygene classes
    serialising themselves to/from xml
    """
    def xmlDump(self, fileobj):
        """
        Dumps out the population to an open file in XML format.

        To dump to a string, use .xmlDumps() instead
        """
        doc = domimpl.createDocument(None, "pygene", None)

        top = doc.documentElement
        top.appendChild(doc.createComment(
            "generated by pygene - http://www.freenet.org.nz/python/pygene"))

        self.xmlDumpSelf(doc, top)

        fileobj.write(doc.toxml())




    def xmlDumps(self):
        """
        dumps out to xml, returning a string of the raw
        generated xml
        """
        s = io.StringIO()
        self.xmlDump(s)
        return s.getvalue()

    def xmlDumpSelf(self, doc, parent):
        """
        Writes out the contents of this population
        into the xml tree
        """
        raise Exception("class %s: xmlDumpSelf not implemented" % \
            self.__class__.__name__)

    def xmlDumpClass(self, tag):
        """
        dumps out class information
        """
        tag.setAttribute("class", self.__class__.__name__)
        tag.setAttribute("module", self.__class__.__module__)

    def xmlDumpAttribs(self, tag):
        """
        """
