Package DOM attributes for Node objects

The Supplement type did the needed mapping (using nodeValue when the
value wasn't a string) and it turns out to be useful to do the same
for the DOM object packaged by a Node, too. Pull out into a helper
function, use dict-comprehension and expose as a method of Node.

Change-Id: Ice6737a54a33372b45cf42152e3fdbf5f2da7ba4
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
This commit is contained in:
Edward Welbourne 2024-01-19 15:59:33 +01:00
parent 904f1d3b13
commit 82f8afe6ba

View File

@ -21,6 +21,10 @@ See individual classes for further detail.
from localetools import Error
from dateconverter import convert_date
def _attrsFromDom(dom):
return { k: (v if isinstance(v, str) else v.nodeValue)
for k, v in dom.attributes.items() }
class Node (object):
"""Wrapper for an arbitrary DOM node.
@ -50,6 +54,9 @@ class Node (object):
else:
self.draft = max(draft, self.draftScore(attr))
def attributes(self):
return _attrsFromDom(self.dom)
def findAllChildren(self, tag, wanted = None, allDull = False):
"""All children that do have the given tag and attributes.
@ -184,9 +191,7 @@ class Supplement (XmlScanner):
if not any(a in e.dom.attributes
for a in exclude)):
if elt.attributes:
yield (elt.nodeName,
dict((k, v if isinstance(v, str) else v.nodeValue)
for k, v in elt.attributes.items()))
yield elt.nodeName, _attrsFromDom(elt)
class LocaleScanner (object):
def __init__(self, name, nodes, root):