Port parse_metadata_use() code from minidom to ElementTree.
authorZac Medico <zmedico@gentoo.org>
Mon, 12 Apr 2010 00:45:33 +0000 (17:45 -0700)
committerZac Medico <zmedico@gentoo.org>
Mon, 12 Apr 2010 00:45:33 +0000 (17:45 -0700)
pym/repoman/utilities.py

index 719e910d7fa5bfc8faed4bf93f62dc2969644264..bb4281c1fb4764c824175ad3ceeb14348b0150e3 100644 (file)
@@ -23,16 +23,10 @@ import codecs
 import errno
 import logging
 import sys
-try:
-       from subprocess import getstatusoutput as subprocess_getstatusoutput
-except ImportError:
-       from commands import getstatusoutput as subprocess_getstatusoutput
-
-from xml.dom import minidom
-from xml.dom import NotFoundErr
 from xml.parsers.expat import ExpatError
 import xml.etree.ElementTree as ET
 from portage import os
+from portage import subprocess_getstatusoutput
 from portage import _encodings
 from portage import _unicode_decode
 from portage import _unicode_encode
@@ -120,37 +114,28 @@ def parse_metadata_use(metadata_xml_content, uselist=None):
        returns a dict of the form a list of flags"""
        if uselist is None:
                uselist = []
-       try:
-               metadatadom = minidom.parseString(metadata_xml_content)
-       except ExpatError as e:
-               raise exception.ParseError("metadata.xml: %s" % (e,))
 
        try:
+               xml_tree = ET.fromstring(metadata_xml_content)
+       except (ExpatError, ) as e:
+               raise exception.ParseError("metadata.xml: " + str(e))
 
-               try:
-                       usetag = metadatadom.getElementsByTagName("use")
-                       if not usetag:
-                               return uselist
-               except NotFoundErr:
-                       return uselist
-
-               try:
-                       flags = usetag[0].getElementsByTagName("flag")
-               except NotFoundErr:
-                       raise exception.ParseError("metadata.xml: " + \
-                               "Malformed input: missing 'flag' tag(s)")
-
-               for flag in flags:
-                       pkg_flag = flag.getAttribute("name")
-                       if not pkg_flag:
-                               raise exception.ParseError("metadata.xml: " + \
-                                       "Malformed input: missing 'name' attribute for 'flag' tag")
-                       uselist.append(pkg_flag)
+       usetag = xml_tree.findall("use")
+       if not usetag:
                return uselist
 
-       finally:
-               metadatadom.unlink()
+       flags = usetag[0].findall("flag")
+       if not flags:
+               raise exception.ParseError("metadata.xml: " + \
+                       "Malformed input: missing 'flag' tag(s)")
 
+       for flag in flags:
+               pkg_flag = flag.get("name")
+               if pkg_flag is None:
+                       raise exception.ParseError("metadata.xml: " + \
+                               "Malformed input: missing 'name' attribute for 'flag' tag")
+               uselist.append(pkg_flag)
+       return uselist
 
 class UnknownHerdsError(ValueError):
        def __init__(self, herd_names):