use cElementTree instead of minidom for xml parsing (thx to Moritz Venn)
authorghost <andreas.monzner@multimedia-labs.de>
Wed, 3 Dec 2008 15:08:07 +0000 (16:08 +0100)
committerghost <andreas.monzner@multimedia-labs.de>
Wed, 3 Dec 2008 15:08:07 +0000 (16:08 +0100)
lib/python/Screens/InfoBarGenerics.py
lib/python/Screens/Menu.py
lib/python/Screens/Setup.py
mytest.py
tests/test_timer.py

index b923340cfcca576d710c1359abc467a9cc6a3924..af29ed4e670899d5bfffc9e99cf277930cdc7cc3 100644 (file)
@@ -348,14 +348,14 @@ class InfoBarMenu:
 
        def mainMenu(self):
                print "loading mainmenu XML..."
 
        def mainMenu(self):
                print "loading mainmenu XML..."
-               menu = mdom.childNodes[0]
-               assert menu.tagName == "menu", "root element in menu must be 'menu'!"
+               menu = mdom.getroot()
+               assert menu.tag == "menu", "root element in menu must be 'menu'!"
 
                self.session.infobar = self
                # so we can access the currently active infobar from screens opened from within the mainmenu
                # at the moment used from the SubserviceSelection
 
 
                self.session.infobar = self
                # so we can access the currently active infobar from screens opened from within the mainmenu
                # at the moment used from the SubserviceSelection
 
-               self.session.openWithCallback(self.mainMenuClosed, MainMenu, menu, menu.childNodes)
+               self.session.openWithCallback(self.mainMenuClosed, MainMenu, menu)
 
        def mainMenuClosed(self, *val):
                self.session.infobar = None
 
        def mainMenuClosed(self, *val):
                self.session.infobar = None
index 92039b429fa4679d860137f8760dbbde14cc2bf8..93f23dfb620e91b1484e7e6bac9c32a47625fb5c 100644 (file)
@@ -9,12 +9,10 @@ from Components.SystemInfo import SystemInfo
 
 from Tools.Directories import resolveFilename, SCOPE_SKIN
 
 
 from Tools.Directories import resolveFilename, SCOPE_SKIN
 
-import xml.dom.minidom
+import xml.etree.cElementTree
 
 from Screens.Setup import Setup, getSetupTitle
 
 
 from Screens.Setup import Setup, getSetupTitle
 
-from Tools import XMLTools
-
 #              <item text="TV-Mode">self.setModeTV()</item>
 #              <item text="Radio-Mode">self.setModeRadio()</item>
 #              <item text="File-Mode">self.setModeFile()</item>
 #              <item text="TV-Mode">self.setModeTV()</item>
 #              <item text="Radio-Mode">self.setModeRadio()</item>
 #              <item text="File-Mode">self.setModeFile()</item>
@@ -22,9 +20,7 @@ from Tools import XMLTools
 
 
 # read the menu
 
 
 # read the menu
-menufile = file(resolveFilename(SCOPE_SKIN, 'menu.xml'), 'r')
-mdom = xml.dom.minidom.parseString(menufile.read())
-menufile.close()
+mdom = xml.etree.cElementTree.parse(resolveFilename(SCOPE_SKIN, 'menu.xml'))
 
 class boundFunction:
        def __init__(self, fnc, *args):
 
 class boundFunction:
        def __init__(self, fnc, *args):
@@ -103,17 +99,17 @@ class Menu(Screen):
                self.session.openWithCallback(self.menuClosed, Setup, dialog)
 
        def addMenu(self, destList, node):
                self.session.openWithCallback(self.menuClosed, Setup, dialog)
 
        def addMenu(self, destList, node):
-               requires = node.getAttribute("requires")
+               requires = node.get("requires")
                if requires and not SystemInfo.get(requires, False):
                        return
                if requires and not SystemInfo.get(requires, False):
                        return
-               MenuTitle = _(node.getAttribute("text").encode("UTF-8") or "??")
-               entryID = node.getAttribute("entryID") or "undefined"
-               weight = node.getAttribute("weight") or 50
-               x = node.getAttribute("flushConfigOnClose")
+               MenuTitle = _(node.get("text", "??").encode("UTF-8"))
+               entryID = node.get("entryID", "undefined")
+               weight = node.get("weight", 50)
+               x = node.get("flushConfigOnClose")
                if x:
                if x:
-                       a = boundFunction(self.session.openWithCallback, self.menuClosedWithConfigFlush, Menu, node, node.childNodes)
+                       a = boundFunction(self.session.openWithCallback, self.menuClosedWithConfigFlush, Menu, node)
                else:
                else:
-                       a = boundFunction(self.session.openWithCallback, self.menuClosed, Menu, node, node.childNodes)
+                       a = boundFunction(self.session.openWithCallback, self.menuClosed, Menu, node)
                #TODO add check if !empty(node.childNodes)
                destList.append((MenuTitle, a, entryID, weight))
 
                #TODO add check if !empty(node.childNodes)
                destList.append((MenuTitle, a, entryID, weight))
 
@@ -126,18 +122,16 @@ class Menu(Screen):
                        self.close(True)
 
        def addItem(self, destList, node):
                        self.close(True)
 
        def addItem(self, destList, node):
-               requires = node.getAttribute("requires")
+               requires = node.get("requires")
                if requires and not SystemInfo.get(requires, False):
                        return
                if requires and not SystemInfo.get(requires, False):
                        return
-               item_text = node.getAttribute("text").encode("UTF-8")
-               entryID = node.getAttribute("entryID") or "undefined"
-               weight = node.getAttribute("weight") or 50
-               for x in node.childNodes:
-                       if x.nodeType != xml.dom.minidom.Element.nodeType:
-                               continue
-                       elif x.tagName == 'screen':
-                               module = x.getAttribute("module") or None
-                               screen = x.getAttribute("screen") or None
+               item_text = node.get("text", "").encode("UTF-8")
+               entryID = node.get("entryID", "undefined")
+               weight = node.get("weight", 50)
+               for x in node:
+                       if x.tag == 'screen':
+                               module = x.get("module")
+                               screen = x.get("screen")
 
                                if screen is None:
                                        screen = module
 
                                if screen is None:
                                        screen = module
@@ -150,16 +144,16 @@ class Menu(Screen):
 
                                # check for arguments. they will be appended to the
                                # openDialog call
 
                                # check for arguments. they will be appended to the
                                # openDialog call
-                               args = XMLTools.mergeText(x.childNodes)
+                               args = x.text or ""
                                screen += ", " + args
 
                                destList.append((_(item_text or "??"), boundFunction(self.runScreen, (module, screen)), entryID, weight))
                                return
                                screen += ", " + args
 
                                destList.append((_(item_text or "??"), boundFunction(self.runScreen, (module, screen)), entryID, weight))
                                return
-                       elif x.tagName == 'code':
-                               destList.append((_(item_text or "??"), boundFunction(self.execText, XMLTools.mergeText(x.childNodes)), entryID, weight))
+                       elif x.tag == 'code':
+                               destList.append((_(item_text or "??"), boundFunction(self.execText, x.text), entryID, weight))
                                return
                                return
-                       elif x.tagName == 'setup':
-                               id = x.getAttribute("id")
+                       elif x.tag == 'setup':
+                               id = x.get("id")
                                if item_text == "":
                                        item_text = _(getSetupTitle(id)) + "..."
                                else:
                                if item_text == "":
                                        item_text = _(getSetupTitle(id)) + "..."
                                else:
@@ -169,26 +163,23 @@ class Menu(Screen):
                destList.append((item_text, self.nothing, entryID, weight))
 
 
                destList.append((item_text, self.nothing, entryID, weight))
 
 
-       def __init__(self, session, parent, childNode):
+       def __init__(self, session, parent):
                Screen.__init__(self, session)
                
                list = []
                
                menuID = None
                Screen.__init__(self, session)
                
                list = []
                
                menuID = None
-               for x in childNode:                                             #walk through the actual nodelist
-                       if x.nodeType != xml.dom.minidom.Element.nodeType:
-                           continue
-                       elif x.tagName == 'item':
-                               item_level = int(x.getAttribute("level") or "0")
-                               
+               for x in parent:                                                #walk through the actual nodelist
+                       if x.tag == 'item':
+                               item_level = int(x.get("level", 0))
                                if item_level <= config.usage.setup_level.index:
                                        self.addItem(list, x)
                                        count += 1
                                if item_level <= config.usage.setup_level.index:
                                        self.addItem(list, x)
                                        count += 1
-                       elif x.tagName == 'menu':
+                       elif x.tag == 'menu':
                                self.addMenu(list, x)
                                count += 1
                                self.addMenu(list, x)
                                count += 1
-                       elif x.tagName == "id":
-                               menuID = x.getAttribute("val")
+                       elif x.tag == "id":
+                               menuID = x.get("val")
                                count = 0
 
                        if menuID is not None:
                                count = 0
 
                        if menuID is not None:
@@ -237,10 +228,10 @@ class Menu(Screen):
                                "9": self.keyNumberGlobal
                        })
 
                                "9": self.keyNumberGlobal
                        })
 
-               a = parent.getAttribute("title").encode("UTF-8") or None
+               a = parent.get("title", "").encode("UTF-8") or None
                a = a and _(a)
                if a is None:
                a = a and _(a)
                if a is None:
-                       a = _(parent.getAttribute("text").encode("UTF-8"))
+                       a = _(parent.get("text", "").encode("UTF-8"))
                self["title"] = StaticText(a)
                self.menu_title = a
 
                self["title"] = StaticText(a)
                self.menu_title = a
 
index 3ff0b76e3ed0f9d6f0361765aba8572ffefddca2..35918b5b612e8a50321da775b8adbc27307237fc 100644 (file)
@@ -6,8 +6,7 @@ from Components.ConfigList import ConfigListScreen
 from Components.Label import Label
 from Components.Pixmap import Pixmap
 
 from Components.Label import Label
 from Components.Pixmap import Pixmap
 
-import xml.dom.minidom
-from Tools import XMLTools
+import xml.etree.cElementTree
 
 # FIXME: use resolveFile!
 # read the setupmenu
 
 # FIXME: use resolveFile!
 # read the setupmenu
@@ -17,7 +16,7 @@ try:
 except:
        # if not found in the current path, we use the global datadir-path
        setupfile = file('/usr/share/enigma2/setup.xml', 'r')
 except:
        # if not found in the current path, we use the global datadir-path
        setupfile = file('/usr/share/enigma2/setup.xml', 'r')
-setupdom = xml.dom.minidom.parseString(setupfile.read())
+setupdom = xml.etree.cElementTree.parse(setupfile)
 setupfile.close()
 
 class SetupSummary(Screen):
 setupfile.close()
 
 class SetupSummary(Screen):
@@ -63,16 +62,12 @@ class Setup(ConfigListScreen, Screen):
                self["config"].setList(list)
 
        def refill(self, list):
                self["config"].setList(list)
 
        def refill(self, list):
-               xmldata = setupdom.childNodes[0]
-               entries = xmldata.childNodes
-               for x in entries:             #walk through the actual nodelist
-                       if x.nodeType != xml.dom.minidom.Element.nodeType:
+               xmldata = setupdom.getroot()
+               for x in xmldata.findall("setup"):
+                       if x.get("key") != self.setup:
                                continue
                                continue
-                       elif x.tagName == 'setup':
-                               if x.getAttribute("key") != self.setup:
-                                       continue
-                               self.addItems(list, x.childNodes);
-                               self.setup_title = x.getAttribute("title").encode("UTF-8")
+                       self.addItems(list, x);
+                       self.setup_title = x.get("title", "").encode("UTF-8")
 
        def __init__(self, session, setup):
                Screen.__init__(self, session)
 
        def __init__(self, session, setup):
                Screen.__init__(self, session)
@@ -118,12 +113,10 @@ class Setup(ConfigListScreen, Screen):
        def createSummary(self):
                return SetupSummary
 
        def createSummary(self):
                return SetupSummary
 
-       def addItems(self, list, childNode):
-               for x in childNode:
-                       if x.nodeType != xml.dom.minidom.Element.nodeType:
-                               continue
-                       elif x.tagName == 'item':
-                               item_level = int(x.getAttribute("level") or "0")
+       def addItems(self, list, parentNode):
+               for x in parentNode:
+                       if x.tag == 'item':
+                               item_level = int(x.get("level", 0))
 
                                if not self.levelChanged in config.usage.setup_level.notifiers:
                                        config.usage.setup_level.notifiers.append(self.levelChanged)
 
                                if not self.levelChanged in config.usage.setup_level.notifiers:
                                        config.usage.setup_level.notifiers.append(self.levelChanged)
@@ -132,12 +125,12 @@ class Setup(ConfigListScreen, Screen):
                                if item_level > config.usage.setup_level.index:
                                        continue
 
                                if item_level > config.usage.setup_level.index:
                                        continue
 
-                               requires = x.getAttribute("requires")
+                               requires = x.get("requires")
                                if requires and not SystemInfo.get(requires, False):
                                        continue;
 
                                if requires and not SystemInfo.get(requires, False):
                                        continue;
 
-                               item_text = _(x.getAttribute("text").encode("UTF-8") or "??")
-                               b = eval(XMLTools.mergeText(x.childNodes));
+                               item_text = _(x.get("text", "??").encode("UTF-8"))
+                               b = eval(x.text or "");
                                if b == "":
                                        continue
                                #add to configlist
                                if b == "":
                                        continue
                                #add to configlist
@@ -148,8 +141,8 @@ class Setup(ConfigListScreen, Screen):
                                        list.append( (item_text, item) )
 
 def getSetupTitle(id):
                                        list.append( (item_text, item) )
 
 def getSetupTitle(id):
-       xmldata = setupdom.childNodes[0].childNodes
-       for x in XMLTools.elementsWithTag(xmldata, "setup"):
-               if x.getAttribute("key") == id:
-                       return x.getAttribute("title").encode("UTF-8")
+       xmldata = setupdom.getroot()
+       for x in xmldata.findall("setup"):
+               if x.get("key") == id:
+                       return x.get("title", "").encode("UTF-8")
        raise "unknown setup id '%s'!" % repr(id)
        raise "unknown setup id '%s'!" % repr(id)
index 886efab2fca53b56f9308e69f1d011cfe014ca53..014f94c9a1d40692aba7daf606e1370a8e1bd16d 100644 (file)
--- a/mytest.py
+++ b/mytest.py
@@ -321,7 +321,6 @@ class Session:
 profile("Standby,PowerKey")
 import Screens.Standby
 from Screens.Menu import MainMenu, mdom
 profile("Standby,PowerKey")
 import Screens.Standby
 from Screens.Menu import MainMenu, mdom
-import xml.dom.minidom
 from GlobalActions import globalActionMap
 
 class PowerKey:
 from GlobalActions import globalActionMap
 
 class PowerKey:
@@ -350,21 +349,16 @@ class PowerKey:
                        self.shutdown()
                elif action == "show_menu":
                        print "Show shutdown Menu"
                        self.shutdown()
                elif action == "show_menu":
                        print "Show shutdown Menu"
-                       menu = mdom.childNodes[0]
-                       for x in menu.childNodes:
-                               if x.nodeType != xml.dom.minidom.Element.nodeType:
-                                   continue
-                               elif x.tagName == 'menu':
-                                       for y in x.childNodes:
-                                               if y.nodeType != xml.dom.minidom.Element.nodeType:
-                                                       continue
-                                               elif y.tagName == 'id':
-                                                       id = y.getAttribute("val")
-                                                       if id and id == "shutdown":
-                                                               self.session.infobar = self
-                                                               menu_screen = self.session.openWithCallback(self.MenuClosed, MainMenu, x, x.childNodes)
-                                                               menu_screen.setTitle(_("Standby / Restart"))
-                                                               return
+                       root = mdom.getroot()
+                       for x in root.findall("menu"):
+                               y = x.find("id")
+                               if y is not None:
+                                       id = y.get("val")
+                                       if id and id == "shutdown":
+                                               self.session.infobar = self
+                                               menu_screen = self.session.openWithCallback(self.MenuClosed, MainMenu, x)
+                                               menu_screen.setTitle(_("Standby / Restart"))
+                                               return
 
        def powerdown(self):
                self.standbyblocked = 0
 
        def powerdown(self):
                self.standbyblocked = 0
index 565a838bdbb68b17d38ea19396dd3b4605c7f608..ea8b0bdbfeef40282f707a2d3784020e3ed9cda7 100644 (file)
@@ -24,10 +24,10 @@ def test_timer(repeat = 0, timer_start = 3600, timer_length = 1000, sim_length =
 
 
        # generate a timer to test
 
 
        # generate a timer to test
-       import xml.dom.minidom
+       import xml.etree.cElementTree
        import RecordTimer
 
        import RecordTimer
 
-       timer = RecordTimer.createTimer(xml.dom.minidom.parseString(
+       timer = RecordTimer.createTimer(xml.etree.cElementTree.fromstring(
        """
                <timer 
                        begin="%d" 
        """
                <timer 
                        begin="%d" 
@@ -41,7 +41,7 @@ def test_timer(repeat = 0, timer_start = 3600, timer_length = 1000, sim_length =
                        disabled="0" 
                        justplay="0">
        </timer>""" % (at + timer_start, at + timer_start + timer_length, repeat)
                        disabled="0" 
                        justplay="0">
        </timer>""" % (at + timer_start, at + timer_start + timer_length, repeat)
-       ).childNodes[0])
+       ))
 
        t.record(timer)
 
 
        t.record(timer)