Turn a next-node function into an iterator over nodes
This simplifies a duplicated iteration pattern in the calling code. It also frees the first-node function to raise an Error (which the iterator now catches) if it finds no node, where it used to return False (instead of a node, so other code using it would raise confusing errors from trying to use False as a node, where now it'll get a clear Error about a missing node). There were also no callers passing an empty name, so the test for matching nodes (here moved to its own short function) didn't need to handle that as a special case. Change-Id: Ife6cad8943cf5dc2c6ed68429d4a217cb9bea446 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
parent
ad1cec5a5e
commit
e93af7dafd
@ -54,23 +54,28 @@ def wrap_list(lst):
|
|||||||
yield head
|
yield head
|
||||||
return ",\n".join(", ".join(x) for x in split(lst, 20))
|
return ",\n".join(", ".join(x) for x in split(lst, 20))
|
||||||
|
|
||||||
|
def isNodeNamed(elt, name, TYPE=xml.dom.minidom.Node.ELEMENT_NODE):
|
||||||
|
return elt.nodeType == TYPE and elt.nodeName == name
|
||||||
|
|
||||||
def firstChildElt(parent, name):
|
def firstChildElt(parent, name):
|
||||||
child = parent.firstChild
|
child = parent.firstChild
|
||||||
while child:
|
while child:
|
||||||
if child.nodeType == parent.ELEMENT_NODE \
|
if isNodeNamed(child, name):
|
||||||
and (not name or child.nodeName == name):
|
|
||||||
return child
|
return child
|
||||||
child = child.nextSibling
|
child = child.nextSibling
|
||||||
return False
|
|
||||||
|
|
||||||
def nextSiblingElt(sibling, name):
|
raise Error('No %s child found' % name)
|
||||||
sib = sibling.nextSibling
|
|
||||||
while sib:
|
def eachEltInGroup(parent, group, key):
|
||||||
if sib.nodeType == sibling.ELEMENT_NODE \
|
try:
|
||||||
and (not name or sib.nodeName == name):
|
element = firstChildElt(parent, group).firstChild
|
||||||
return sib
|
except Error:
|
||||||
sib = sib.nextSibling
|
element = None
|
||||||
return False
|
|
||||||
|
while element:
|
||||||
|
if isNodeNamed(element, key):
|
||||||
|
yield element
|
||||||
|
element = element.nextSibling
|
||||||
|
|
||||||
def eltText(elt):
|
def eltText(elt):
|
||||||
result = ""
|
result = ""
|
||||||
@ -86,42 +91,33 @@ def eltText(elt):
|
|||||||
def loadLanguageMap(doc):
|
def loadLanguageMap(doc):
|
||||||
result = {}
|
result = {}
|
||||||
|
|
||||||
language_list_elt = firstChildElt(doc.documentElement, "languageList")
|
for language_elt in eachEltInGroup(doc.documentElement, "languageList", "language"):
|
||||||
language_elt = firstChildElt(language_list_elt, "language")
|
|
||||||
while language_elt:
|
|
||||||
language_id = int(eltText(firstChildElt(language_elt, "id")))
|
language_id = int(eltText(firstChildElt(language_elt, "id")))
|
||||||
language_name = eltText(firstChildElt(language_elt, "name"))
|
language_name = eltText(firstChildElt(language_elt, "name"))
|
||||||
language_code = eltText(firstChildElt(language_elt, "code"))
|
language_code = eltText(firstChildElt(language_elt, "code"))
|
||||||
result[language_id] = (language_name, language_code)
|
result[language_id] = (language_name, language_code)
|
||||||
language_elt = nextSiblingElt(language_elt, "language")
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def loadScriptMap(doc):
|
def loadScriptMap(doc):
|
||||||
result = {}
|
result = {}
|
||||||
|
|
||||||
script_list_elt = firstChildElt(doc.documentElement, "scriptList")
|
for script_elt in eachEltInGroup(doc.documentElement, "scriptList", "script"):
|
||||||
script_elt = firstChildElt(script_list_elt, "script")
|
|
||||||
while script_elt:
|
|
||||||
script_id = int(eltText(firstChildElt(script_elt, "id")))
|
script_id = int(eltText(firstChildElt(script_elt, "id")))
|
||||||
script_name = eltText(firstChildElt(script_elt, "name"))
|
script_name = eltText(firstChildElt(script_elt, "name"))
|
||||||
script_code = eltText(firstChildElt(script_elt, "code"))
|
script_code = eltText(firstChildElt(script_elt, "code"))
|
||||||
result[script_id] = (script_name, script_code)
|
result[script_id] = (script_name, script_code)
|
||||||
script_elt = nextSiblingElt(script_elt, "script")
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def loadCountryMap(doc):
|
def loadCountryMap(doc):
|
||||||
result = {}
|
result = {}
|
||||||
|
|
||||||
country_list_elt = firstChildElt(doc.documentElement, "countryList")
|
for country_elt in eachEltInGroup(doc.documentElement, "countryList", "country"):
|
||||||
country_elt = firstChildElt(country_list_elt, "country")
|
|
||||||
while country_elt:
|
|
||||||
country_id = int(eltText(firstChildElt(country_elt, "id")))
|
country_id = int(eltText(firstChildElt(country_elt, "id")))
|
||||||
country_name = eltText(firstChildElt(country_elt, "name"))
|
country_name = eltText(firstChildElt(country_elt, "name"))
|
||||||
country_code = eltText(firstChildElt(country_elt, "code"))
|
country_code = eltText(firstChildElt(country_elt, "code"))
|
||||||
result[country_id] = (country_name, country_code)
|
result[country_id] = (country_name, country_code)
|
||||||
country_elt = nextSiblingElt(country_elt, "country")
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@ -129,9 +125,7 @@ def loadLikelySubtagsMap(doc):
|
|||||||
result = {}
|
result = {}
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
list_elt = firstChildElt(doc.documentElement, "likelySubtags")
|
for elt in eachEltInGroup(doc.documentElement, "likelySubtags", "likelySubtag"):
|
||||||
elt = firstChildElt(list_elt, "likelySubtag")
|
|
||||||
while elt:
|
|
||||||
elt_from = firstChildElt(elt, "from")
|
elt_from = firstChildElt(elt, "from")
|
||||||
from_language = eltText(firstChildElt(elt_from, "language"));
|
from_language = eltText(firstChildElt(elt_from, "language"));
|
||||||
from_script = eltText(firstChildElt(elt_from, "script"));
|
from_script = eltText(firstChildElt(elt_from, "script"));
|
||||||
@ -147,7 +141,6 @@ def loadLikelySubtagsMap(doc):
|
|||||||
tmp["to"] = (to_language, to_script, to_country)
|
tmp["to"] = (to_language, to_script, to_country)
|
||||||
result[i] = tmp;
|
result[i] = tmp;
|
||||||
i += 1
|
i += 1
|
||||||
elt = nextSiblingElt(elt, "likelySubtag");
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def fixedScriptName(name, dupes):
|
def fixedScriptName(name, dupes):
|
||||||
@ -196,9 +189,7 @@ def countryNameToId(name, country_map):
|
|||||||
def loadLocaleMap(doc, language_map, script_map, country_map, likely_subtags_map):
|
def loadLocaleMap(doc, language_map, script_map, country_map, likely_subtags_map):
|
||||||
result = {}
|
result = {}
|
||||||
|
|
||||||
locale_list_elt = firstChildElt(doc.documentElement, "localeList")
|
for locale_elt in eachEltInGroup(doc.documentElement, "localeList", "locale"):
|
||||||
locale_elt = firstChildElt(locale_list_elt, "locale")
|
|
||||||
while locale_elt:
|
|
||||||
locale = Locale.fromXmlData(lambda k: eltText(firstChildElt(locale_elt, k)))
|
locale = Locale.fromXmlData(lambda k: eltText(firstChildElt(locale_elt, k)))
|
||||||
language_id = languageNameToId(locale.language, language_map)
|
language_id = languageNameToId(locale.language, language_map)
|
||||||
if language_id == -1:
|
if language_id == -1:
|
||||||
@ -233,8 +224,6 @@ def loadLocaleMap(doc, language_map, script_map, country_map, likely_subtags_map
|
|||||||
|
|
||||||
result[(language_id, script_id, country_id)] = locale
|
result[(language_id, script_id, country_id)] = locale
|
||||||
|
|
||||||
locale_elt = nextSiblingElt(locale_elt, "locale")
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def compareLocaleKeys(key1, key2):
|
def compareLocaleKeys(key1, key2):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user