summaryrefslogtreecommitdiffstats
path: root/util/local_database/qlocalexml2cpp.py
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2017-05-30 15:50:47 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2017-06-09 08:24:08 +0000
commitebb0212133bd91f1da4931b29eb1d33fb77b1444 (patch)
tree115ca1d5ca8c69b2a5c88a4341a4642418565de1 /util/local_database/qlocalexml2cpp.py
parent3a569573b2b8e8dc56f0a152afcb48723b682ba8 (diff)
Rework locale serialization and parsing with less repetition
... because copy-and-paste is worth discouraging. Moved code that writes and digests our Q Local XML form of the data into a common class, localexml.Locale, for use by the scripts that write and read it. Hopefully, it'll be easier to keep what's written and read in sync hereafter. Inlined some trivial functions in the process; and only create a day-number mapping dictionary once, instead of once per use. Also made it easier to see which attributes get which special handling (and documented this); and revised an assertion to be more helpful. Change-Id: I711b6a193a4ad94b5ff714c025f2732cd1a965a7 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'util/local_database/qlocalexml2cpp.py')
-rwxr-xr-xutil/local_database/qlocalexml2cpp.py101
1 files changed, 4 insertions, 97 deletions
diff --git a/util/local_database/qlocalexml2cpp.py b/util/local_database/qlocalexml2cpp.py
index d132027294..d2517aaa48 100755
--- a/util/local_database/qlocalexml2cpp.py
+++ b/util/local_database/qlocalexml2cpp.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python2
#############################################################################
##
-## Copyright (C) 2016 The Qt Company Ltd.
+## Copyright (C) 2017 The Qt Company Ltd.
## Contact: https://www.qt.io/licensing/
##
## This file is part of the test suite of the Qt Toolkit.
@@ -39,6 +39,8 @@ import tempfile
import datetime
import xml.dom.minidom
+from localexml import Locale
+
class Error:
def __init__(self, msg):
self.msg = msg
@@ -191,108 +193,13 @@ def countryNameToId(name, country_map):
return key
return -1
-def convertFormat(format):
- result = ""
- i = 0
- while i < len(format):
- if format[i] == "'":
- result += "'"
- i += 1
- while i < len(format) and format[i] != "'":
- result += format[i]
- i += 1
- if i < len(format):
- result += "'"
- i += 1
- else:
- s = format[i:]
- if s.startswith("EEEE"):
- result += "dddd"
- i += 4
- elif s.startswith("EEE"):
- result += "ddd"
- i += 3
- elif s.startswith("a"):
- result += "AP"
- i += 1
- elif s.startswith("z"):
- result += "t"
- i += 1
- elif s.startswith("v"):
- i += 1
- else:
- result += format[i]
- i += 1
-
- return result
-
-def convertToQtDayOfWeek(firstDay):
- qtDayOfWeek = {"mon":1, "tue":2, "wed":3, "thu":4, "fri":5, "sat":6, "sun":7}
- return qtDayOfWeek[firstDay]
-
-def assertSingleChar(string):
- assert len(string) == 1, "This string is not allowed to be longer than 1 character"
- return string
-
-class Locale:
- def __init__(self, elt):
- self.language = eltText(firstChildElt(elt, "language"))
- self.languageEndonym = eltText(firstChildElt(elt, "languageEndonym"))
- self.script = eltText(firstChildElt(elt, "script"))
- self.country = eltText(firstChildElt(elt, "country"))
- self.countryEndonym = eltText(firstChildElt(elt, "countryEndonym"))
- self.decimal = int(eltText(firstChildElt(elt, "decimal")))
- self.group = int(eltText(firstChildElt(elt, "group")))
- self.listDelim = int(eltText(firstChildElt(elt, "list")))
- self.percent = int(eltText(firstChildElt(elt, "percent")))
- self.zero = int(eltText(firstChildElt(elt, "zero")))
- self.minus = int(eltText(firstChildElt(elt, "minus")))
- self.plus = int(eltText(firstChildElt(elt, "plus")))
- self.exp = int(eltText(firstChildElt(elt, "exp")))
- self.quotationStart = ord(assertSingleChar(eltText(firstChildElt(elt, "quotationStart"))))
- self.quotationEnd = ord(assertSingleChar(eltText(firstChildElt(elt, "quotationEnd"))))
- self.alternateQuotationStart = ord(assertSingleChar(eltText(firstChildElt(elt, "alternateQuotationStart"))))
- self.alternateQuotationEnd = ord(assertSingleChar(eltText(firstChildElt(elt, "alternateQuotationEnd"))))
- self.listPatternPartStart = eltText(firstChildElt(elt, "listPatternPartStart"))
- self.listPatternPartMiddle = eltText(firstChildElt(elt, "listPatternPartMiddle"))
- self.listPatternPartEnd = eltText(firstChildElt(elt, "listPatternPartEnd"))
- self.listPatternPartTwo = eltText(firstChildElt(elt, "listPatternPartTwo"))
- self.am = eltText(firstChildElt(elt, "am"))
- self.pm = eltText(firstChildElt(elt, "pm"))
- self.firstDayOfWeek = convertToQtDayOfWeek(eltText(firstChildElt(elt, "firstDayOfWeek")))
- self.weekendStart = convertToQtDayOfWeek(eltText(firstChildElt(elt, "weekendStart")))
- self.weekendEnd = convertToQtDayOfWeek(eltText(firstChildElt(elt, "weekendEnd")))
- self.longDateFormat = convertFormat(eltText(firstChildElt(elt, "longDateFormat")))
- self.shortDateFormat = convertFormat(eltText(firstChildElt(elt, "shortDateFormat")))
- self.longTimeFormat = convertFormat(eltText(firstChildElt(elt, "longTimeFormat")))
- self.shortTimeFormat = convertFormat(eltText(firstChildElt(elt, "shortTimeFormat")))
- self.standaloneLongMonths = eltText(firstChildElt(elt, "standaloneLongMonths"))
- self.standaloneShortMonths = eltText(firstChildElt(elt, "standaloneShortMonths"))
- self.standaloneNarrowMonths = eltText(firstChildElt(elt, "standaloneNarrowMonths"))
- self.longMonths = eltText(firstChildElt(elt, "longMonths"))
- self.shortMonths = eltText(firstChildElt(elt, "shortMonths"))
- self.narrowMonths = eltText(firstChildElt(elt, "narrowMonths"))
- self.standaloneLongDays = eltText(firstChildElt(elt, "standaloneLongDays"))
- self.standaloneShortDays = eltText(firstChildElt(elt, "standaloneShortDays"))
- self.standaloneNarrowDays = eltText(firstChildElt(elt, "standaloneNarrowDays"))
- self.longDays = eltText(firstChildElt(elt, "longDays"))
- self.shortDays = eltText(firstChildElt(elt, "shortDays"))
- self.narrowDays = eltText(firstChildElt(elt, "narrowDays"))
- self.currencyIsoCode = eltText(firstChildElt(elt, "currencyIsoCode"))
- self.currencySymbol = eltText(firstChildElt(elt, "currencySymbol"))
- self.currencyDisplayName = eltText(firstChildElt(elt, "currencyDisplayName"))
- self.currencyDigits = int(eltText(firstChildElt(elt, "currencyDigits")))
- self.currencyRounding = int(eltText(firstChildElt(elt, "currencyRounding")))
- self.currencyFormat = eltText(firstChildElt(elt, "currencyFormat"))
- self.currencyNegativeFormat = eltText(firstChildElt(elt, "currencyNegativeFormat"))
-
def loadLocaleMap(doc, language_map, script_map, country_map, likely_subtags_map):
result = {}
locale_list_elt = firstChildElt(doc.documentElement, "localeList")
locale_elt = firstChildElt(locale_list_elt, "locale")
while locale_elt:
- locale = Locale(locale_elt)
+ locale = Locale.fromXmlData(lambda k: eltText(firstChildElt(locale_elt, k)))
language_id = languageNameToId(locale.language, language_map)
if language_id == -1:
sys.stderr.write("Cannot find a language id for '%s'\n" % locale.language)