Author: dmeyer
Date: Mon Feb 26 21:12:46 2007
New Revision: 9286

Modified:
   trunk/core/src/xmlconfig.py

Log:
port to minidom

Modified: trunk/core/src/xmlconfig.py
==============================================================================
--- trunk/core/src/xmlconfig.py (original)
+++ trunk/core/src/xmlconfig.py Mon Feb 26 21:12:46 2007
@@ -35,28 +35,13 @@
 import os
 import stat
 import md5
+from xml.dom import minidom
 
 # kaa imports
 import kaa.distribution.xmlconfig
-import kaa.xml
 
-def xmlconfig(configfile, sources):
 
-    def get_parent(tree, name, position=''):
-        for child in tree:
-            if child.name == 'group':
-                # print 'have', child.getattr('name')
-                if position + child.name == name:
-                    raise RuntimeError('bad tree')
-                if name.startswith(position + child.getattr('name')):
-                    # print 'deeper'
-                    return get_parent(child, name, position + 
child.getattr('name') + '.')
-        for name in name[len(position):].strip(' .').split('.'):
-            # print 'create', name
-            node = kaa.xml.Node('group', name=name)
-            tree.add_child(node)
-            tree = node
-        return tree
+def xmlconfig(configfile, sources):
 
     hashkey = [ f+str(os.stat(f)[stat.ST_MTIME]) for f in sources ]
     hashkey = md5.new(''.join(hashkey)).hexdigest()
@@ -69,34 +54,31 @@
             return True
         fd.close()
 
+    doc = None
     modules = []
     for cfg in sources:
         # load cxml file
-        m = kaa.xml.Document(cfg, 'config')
-        if m.getattr('name'):
-            # FIXME: ugly hack
-            # No idea why this is needed, m.unlink() should do the
-            # trick. One day I will understand the memory management
-            # from libxml2.
-            m._doc._doc = True
-            m.unlink()
-        if not m.getattr('name').endswith('plugin'):
+        m = minidom.parse(cfg).firstChild
+        if m.nodeName != 'config':
+            raise RuntimeError('%s is no valid cxml file' % cfg)
+        if not m.getAttribute('name'):
+            doc = m.parentNode
+            continue
+        if not m.getAttribute('name').endswith('plugin'):
             modules.append(m)
             continue
         # list of plugins
-        for plugin in m.children:
-            if plugin.name == 'group':
-                # unlink node to add later to a new doc
-                plugin.unlink()
-                name = '%s.%s' % (m.getattr('name'), plugin.getattr('name'))
-                plugin.setattr('name', name)
+        for plugin in m.childNodes:
+            if plugin.nodeName == 'group':
+                name = '%s.%s' % (m.getAttribute('name'), 
plugin.getAttribute('name'))
+                plugin.setAttribute('name', name)
                 modules.append(plugin)
 
     def valfunc(node):
-        name = node.getattr('name')
+        name = node.getAttribute('name')
         if name.startswith('plugin'):
             return '2%s' % name
-        if node.hasattr('plugin') or '.plugin.' in name:
+        if node.hasAttribute('plugin') or '.plugin.' in name:
             return '1%s' % name
         if 'gui' in name:
             return '3%s' % name
@@ -105,51 +87,66 @@
     # sort modules somehow
     modules.sort(lambda x,y: cmp(valfunc(x), valfunc(y)))
 
-    if modules[0].getattr('name') == '':
-        tree = modules.pop(0)
-    else:
+    if doc is None:
         doc = '<?xml version="1.0"?><config name=""></config>'
-        tree = kaa.xml.Document(doc, 'config')
+        doc = minidom.parseString(doc)
+
+    def get_parent(parent, name, position=''):
+        for child in parent.childNodes:
+            if child.nodeName == 'group':
+                # print 'have', child.getattr('name')
+                if position + child.nodeName == name:
+                    raise RuntimeError('bad tree')
+                if name.startswith(position + child.getAttribute('name')):
+                    # print 'deeper'
+                    position = position + child.getAttribute('name') + '.'
+                    return get_parent(child, name, position)
+        for name in name[len(position):].strip(' .').split('.'):
+            node = doc.createElement('group')
+            node.setAttribute('name', name)
+            parent.appendChild(node)
+            parent = node
+        return parent
 
     for m in modules:
-        parent = get_parent(tree, m.getattr('name'))
-        if m.hasattr('plugin'):
-            node = kaa.xml.Node('var')
-            node.setattr('name', 'activate')
-            node.setattr('default', m.getattr('plugin'))
-            parent.add_child(node)
-            parent.setattr('is_plugin', 'yes')
-        for child in m:
-            child.unlink()
-            parent.add_child(child)
+        parent = get_parent(doc.firstChild, m.getAttribute('name'))
+        if m.hasAttribute('plugin'):
+            node = doc.createElement('var')
+            node.setAttribute('name', 'activate')
+            node.setAttribute('default', m.getAttribute('plugin'))
+            parent.appendChild(node)
+            parent.setAttribute('is_plugin', 'yes')
+        for child in m.childNodes[:]:
+            parent.appendChild(child)
 
     if configfile.endswith('.cxml'):
-        return tree.save(configfile)
+        return open(configfile, 'w').write(doc.toxml())
+    
     out = open(configfile, 'w')
     out.write('# -*- coding: iso-8859-1 -*-\n')
     out.write('# -*- hash: %s\n' % hashkey)
     out.write('# auto generated file\n\n')
     out.write('from kaa.config import Var, Group, Dict, List, Config\n\n')
     out.write('config = ')
-    kaa.distribution.xmlconfig.Parser().parse(tree, out)
+    kaa.distribution.xmlconfig.Parser().parse(doc.firstChild, out)
 
     def find_plugins(node, position=''):
-        for child in node:
-            if child.hasattr('is_plugin'):
-                name = "%s%s" % (position, child.getattr('name'))
+        for child in node.childNodes:
+            if not child.nodeName == 'group':
+                continue
+            if child.hasAttribute('is_plugin'):
+                name = "%s%s" % (position, child.getAttribute('name'))
                 out.write('\'%s\', ' % name.strip('. ,'))
-            if child.name == 'group':
-                find_plugins(child, position + child.getattr('name') + '.')
-
+            find_plugins(child, position + child.getAttribute('name') + '.')
     out.write('\nplugins = [')
-    find_plugins(tree)
+    find_plugins(doc.firstChild)
     out.write(']\n')
 
     def find_code(node):
-        for child in node:
-            if child.name == 'code':
+        for child in node.childNodes:
+            if child.nodeName == 'code':
                 out.write(kaa.distribution.xmlconfig.format_content(child) + 
'\n\n')
-            if child.name == 'group':
+            if child.nodeName == 'group':
                 find_code(child)
-    find_code(tree)
+    find_code(doc.firstChild)
     out.close()

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to