def parse_metadata_use(xml_tree): """ Records are wrapped in XML as per GLEP 56 returns a dict with keys constisting of USE flag names and values containing their respective descriptions """ uselist = {} usetags = xml_tree.findall("use") if not usetags: return uselist # It's possible to have multiple 'use' elements. for usetag in usetags: flags = usetag.findall("flag") if not flags: raise exception.ParseError("missing 'flag' tag(s)") for flag in flags: pkg_flag = flag.get("name") if pkg_flag is None: raise exception.ParseError( "missing 'name' attribute for 'flag' tag") flag_restrict = flag.get("restrict") # emulate the Element.itertext() method from python-2.7 inner_text = [] stack = [] stack.append(flag) while stack: obj = stack.pop() if isinstance(obj, basestring): inner_text.append(obj) continue if isinstance(obj.text, basestring): inner_text.append(obj.text) if isinstance(obj.tail, basestring): stack.append(obj.tail) stack.extend(reversed(obj)) if pkg_flag not in uselist: uselist[pkg_flag] = {} # (flag_restrict can be None) uselist[pkg_flag][flag_restrict] = " ".join( "".join(inner_text).split()) return uselist
def parse_metadata_use(mylines, uselist=None): """ Records are wrapped in XML as per GLEP 56 returns a dict of the form a list of flags""" if uselist is None: uselist = [] try: metadatadom = minidom.parse(mylines) except ExpatError as e: raise exception.ParseError("metadata.xml: %s" % (e,)) try: 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) return uselist finally: metadatadom.unlink()