From 82f8afe6bab42b7b70f571825d23cbb9270b9071 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Fri, 19 Jan 2024 15:59:33 +0100 Subject: [PATCH] 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 --- util/locale_database/ldml.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/util/locale_database/ldml.py b/util/locale_database/ldml.py index 2a029ce98b6..d259bedba1c 100644 --- a/util/locale_database/ldml.py +++ b/util/locale_database/ldml.py @@ -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):