From 6a4875f0d17f7ab282eebfd2870918a1abf032a5 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 8 Jun 2017 12:30:22 +0200 Subject: Replace three functions with one simpler function load{Language,Script,Country}Map() were all structurally very similar, so replace them with a single loadMap() that takes a second argument to say *which* map to load. At the same time, use a dict comprehension to simplify constructing the result. Change-Id: Ie43a71156010277200543a8937056efd35251955 Reviewed-by: Lars Knoll --- util/local_database/qlocalexml2cpp.py | 44 +++++++---------------------------- 1 file changed, 9 insertions(+), 35 deletions(-) (limited to 'util') diff --git a/util/local_database/qlocalexml2cpp.py b/util/local_database/qlocalexml2cpp.py index 42e0dbbf3c..44187ede59 100755 --- a/util/local_database/qlocalexml2cpp.py +++ b/util/local_database/qlocalexml2cpp.py @@ -88,38 +88,12 @@ def eltText(elt): child = child.nextSibling return result -def loadLanguageMap(doc): - result = {} - - for language_elt in eachEltInGroup(doc.documentElement, "languageList", "language"): - language_id = int(eltText(firstChildElt(language_elt, "id"))) - language_name = eltText(firstChildElt(language_elt, "name")) - language_code = eltText(firstChildElt(language_elt, "code")) - result[language_id] = (language_name, language_code) - - return result - -def loadScriptMap(doc): - result = {} - - for script_elt in eachEltInGroup(doc.documentElement, "scriptList", "script"): - script_id = int(eltText(firstChildElt(script_elt, "id"))) - script_name = eltText(firstChildElt(script_elt, "name")) - script_code = eltText(firstChildElt(script_elt, "code")) - result[script_id] = (script_name, script_code) - - return result - -def loadCountryMap(doc): - result = {} - - for country_elt in eachEltInGroup(doc.documentElement, "countryList", "country"): - country_id = int(eltText(firstChildElt(country_elt, "id"))) - country_name = eltText(firstChildElt(country_elt, "name")) - country_code = eltText(firstChildElt(country_elt, "code")) - result[country_id] = (country_name, country_code) - - return result +def loadMap(doc, category): + return dict((int(eltText(firstChildElt(element, 'id'))), + (eltText(firstChildElt(element, 'name')), + eltText(firstChildElt(element, 'code')))) + for element in eachEltInGroup(doc.documentElement, + category + 'List', category)) def loadLikelySubtagsMap(doc): result = {} @@ -391,9 +365,9 @@ def main(): data_temp_file.write(GENERATED_BLOCK_START) doc = xml.dom.minidom.parse(localexml) - language_map = loadLanguageMap(doc) - script_map = loadScriptMap(doc) - country_map = loadCountryMap(doc) + language_map = loadMap(doc, 'language') + script_map = loadMap(doc, 'script') + country_map = loadMap(doc, 'country') likely_subtags_map = loadLikelySubtagsMap(doc) default_map = {} for key in likely_subtags_map.keys(): -- cgit v1.2.3