def exportTopicTreeSpecXml(moduleName=None,
                           rootTopic=None,
                           bak='bak',
                           moduleDoc=None):
    '''
    If rootTopic is None, then pub.getDefaultTopicTreeRoot() is assumed.
    '''

    if rootTopic is None:
        from pubsub import pub
        rootTopic = pub.getDefaultTopicTreeRoot()
    elif isinstance(rootTopic, (str, unicode)):
        from pubsub import pub
        rootTopic = pub.getTopic(rootTopic)

    tree = ET.Element('topicdefntree')
    if moduleDoc:
        mod_desc = ET.SubElement(tree, 'description')
        mod_desc.text = ' '.join(moduleDoc.split())

    traverser = pub.TopicTreeTraverser(XmlVisitor(tree))
    traverser.traverse(rootTopic)

    indent(tree)

    if moduleName:

        filename = '%s.xml' % moduleName
        if bak:
            pub._backupIfExists(filename, bak)

        fulltree = ET.ElementTree(tree)
        fulltree.write(filename, "utf-8", True)

    return ET.tostring(tree)
def exportTopicTreeSpecXml(moduleName=None, rootTopic=None, bak='bak', moduleDoc=None):
    '''
    If rootTopic is None, then pub.getDefaultTopicTreeRoot() is assumed.
    '''

    if rootTopic is None:
        from pubsub import pub
        rootTopic = pub.getDefaultTopicTreeRoot()
    elif isinstance(rootTopic, (str, unicode)):
        from pubsub import pub
        rootTopic = pub.getTopic(rootTopic)

    tree = ET.Element('topicdefntree')
    if moduleDoc:
        mod_desc = ET.SubElement(tree, 'description')
        mod_desc.text = ' '.join(moduleDoc.split())

    traverser = pub.TopicTreeTraverser(XmlVisitor(tree))
    traverser.traverse(rootTopic)

    indent(tree)

    if moduleName:

        filename = '%s.xml' % moduleName
        if bak:
            pub._backupIfExists(filename, bak)

        fulltree= ET.ElementTree(tree)
        fulltree.write(filename, "utf-8", True)

    return ET.tostring(tree)
def exportTopicTreeSpec(moduleName=None, rootTopic=None, bak="bak", moduleDoc=None):
    """Export the topic tree rooted at rootTopic to module (.py) file. This file 
    will contain a nested class hierarchy representing the topic tree. Returns a
    string representing the contents of the file. Parameters:

        - If moduleName is given, the topic tree is written to moduleName.py in
          os.getcwd() (the file is overwritten). If bak is None, the module file
          is not backed up.
        - If rootTopic is specified, the export only traverses tree from 
          corresponding topic. Otherwise, complete tree, using 
          pub.getDefaultTopicTreeRoot() as starting  point.
        - The moduleDoc is the doc string for the module ie topic tree.
    """

    if rootTopic is None:
        from pubsub import pub

        rootTopic = pub.getDefaultTopicTreeRoot()
    elif isinstance(rootTopic, (str, unicode)):
        from pubsub import pub

        rootTopic = pub.getTopic(rootTopic)

    # create exporter
    if moduleName is None:
        from StringIO import StringIO

        capture = StringIO()
        TopicTreeSpecPrinter(rootTopic, fileObj=capture, treeDoc=moduleDoc)
        return capture.getvalue()

    else:
        filename = "%s.py" % moduleName
        if bak:
            _backupIfExists(filename, bak)
        moduleFile = file(filename, "w")
        try:
            TopicTreeSpecPrinter(rootTopic, fileObj=moduleFile, treeDoc=moduleDoc)
        finally:
            moduleFile.close()
def exportTopicTreeSpec(moduleName = None, rootTopic=None, bak='bak', moduleDoc=None):
    '''Export the topic tree rooted at rootTopic to module (.py) file. This file 
    will contain a nested class hierarchy representing the topic tree. Returns a
    string representing the contents of the file. Parameters:

        - If moduleName is given, the topic tree is written to moduleName.py in
          os.getcwd() (the file is overwritten). If bak is None, the module file
          is not backed up.
        - If rootTopic is specified, the export only traverses tree from 
          corresponding topic. Otherwise, complete tree, using 
          pub.getDefaultTopicTreeRoot() as starting  point.
        - The moduleDoc is the doc string for the module ie topic tree.
    '''

    if rootTopic is None:
        from pubsub import pub
        rootTopic = pub.getDefaultTopicTreeRoot()
    elif isinstance(rootTopic, (str, unicode)):
        from pubsub import pub
        rootTopic = pub.getTopic(rootTopic)

    # create exporter
    if moduleName is None:
        from StringIO import StringIO
        capture = StringIO()
        TopicTreeSpecPrinter(rootTopic, fileObj=capture, treeDoc=moduleDoc)
        return capture.getvalue()

    else:
        filename = '%s.py' % moduleName
        if bak:
            _backupIfExists(filename, bak)
        moduleFile = file(filename, 'w')
        try:
            TopicTreeSpecPrinter(rootTopic, fileObj=moduleFile, treeDoc=moduleDoc)
        finally:
            moduleFile.close()