From 08cfd02312d21391c956b8bf50ab7d6caa0d8082 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Fri, 12 May 2017 11:59:18 +0200 Subject: Use python more competently in CLDR/qLocaleXML scripts Make the python2 dependency explicit (sooner or later, python3 shall be the default /bin/env python), make time-zone script executable (it had a shebang). Use triple-quoted strings, or single quotes, to avoid extra backslashes, remove some simply spurious backslashes. Use generators rather than map or filter with lambdas and iterate rather than duplicating code. Clarify some comments. Regenerated headers: this upates the date of generation, cuts back a double-blank-line to single and skips a spurious trailing comma-newline on an array's data. Change-Id: I54439f0dec132865991fe5147d509cea0f9419a0 Reviewed-by: Lars Knoll --- util/local_database/cldr2qlocalexml.py | 343 +++++++++++---------------------- util/local_database/cldr2qtimezone.py | 28 +-- util/local_database/qlocalexml2cpp.py | 60 +++--- 3 files changed, 155 insertions(+), 276 deletions(-) mode change 100644 => 100755 util/local_database/cldr2qtimezone.py (limited to 'util') diff --git a/util/local_database/cldr2qlocalexml.py b/util/local_database/cldr2qlocalexml.py index 3d388118c3..4e15eab335 100755 --- a/util/local_database/cldr2qlocalexml.py +++ b/util/local_database/cldr2qlocalexml.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python2 ############################################################################# ## ## Copyright (C) 2016 The Qt Company Ltd. @@ -95,7 +95,7 @@ def parse_list_pattern_part_format(pattern): def ordStr(c): if len(c) == 1: return str(ord(c)) - raise xpathlite.Error("Unable to handle value \"%s\"" % addEscapes(c)) + raise xpathlite.Error('Unable to handle value "%s"' % addEscapes(c)) return "##########" # the following functions are supposed to fix the problem with QLocale @@ -129,7 +129,7 @@ def generateLocaleInfo(path): # skip legacy/compatibility ones alias = findAlias(path) if alias: - raise xpathlite.Error("alias to \"%s\"" % alias) + raise xpathlite.Error('alias to "%s"' % alias) language_code = findEntryInFile(path, "identity/language", attribute="type")[0] country_code = findEntryInFile(path, "identity/territory", attribute="type")[0] @@ -150,16 +150,16 @@ def _generateLocaleInfo(path, language_code, script_code, country_code, variant_ # ### actually there is only one locale with variant: en_US_POSIX # does anybody care about it at all? if variant_code: - raise xpathlite.Error("we do not support variants (\"%s\")" % variant_code) + raise xpathlite.Error('we do not support variants ("%s")' % variant_code) language_id = enumdata.languageCodeToId(language_code) if language_id <= 0: - raise xpathlite.Error("unknown language code \"%s\"" % language_code) + raise xpathlite.Error('unknown language code "%s"' % language_code) language = enumdata.language_list[language_id][0] script_id = enumdata.scriptCodeToId(script_code) if script_id == -1: - raise xpathlite.Error("unknown script code \"%s\"" % script_code) + raise xpathlite.Error('unknown script code "%s"' % script_code) script = enumdata.script_list[script_id][0] # we should handle fully qualified names with the territory @@ -167,7 +167,7 @@ def _generateLocaleInfo(path, language_code, script_code, country_code, variant_ return {} country_id = enumdata.countryCodeToId(country_code) if country_id <= 0: - raise xpathlite.Error("unknown country code \"%s\"" % country_code) + raise xpathlite.Error('unknown country code "%s"' % country_code) country = enumdata.country_list[country_id][0] # So we say we accept only those values that have "contributed" or @@ -198,17 +198,17 @@ def _generateLocaleInfo(path, language_code, script_code, country_code, variant_ for e in currencies: if e[0] == 'currency': tender = True - t = filter(lambda x: x[0] == 'tender', e[1]) + t = [x for x in e[1] if x[0] == 'tender'] if t and t[0][1] == 'false': tender = False; - if tender and not filter(lambda x: x[0] == 'to', e[1]): - result['currencyIsoCode'] = filter(lambda x: x[0] == 'iso4217', e[1])[0][1] + if tender and not any(x[0] == 'to' for x in e[1]): + result['currencyIsoCode'] = (x[1] for x in e[1] if x[0] == 'iso4217').next() break if result['currencyIsoCode']: t = findTagsInFile(supplementalPath, "currencyData/fractions/info[iso4217=%s]"%result['currencyIsoCode']); if t and t[0][0] == 'info': - result['currencyDigits'] = int(filter(lambda x: x[0] == 'digits', t[0][1])[0][1]) - result['currencyRounding'] = int(filter(lambda x: x[0] == 'rounding', t[0][1])[0][1]) + result['currencyDigits'] = (int(x[1]) for x in t[0][1] if x[0] == 'digits').next() + result['currencyRounding'] = (int(x[1]) for x in t[0][1] if x[0] == 'rounding').next() numbering_system = None try: numbering_system = findEntry(path, "numbers/defaultNumberingSystem") @@ -292,165 +292,40 @@ def _generateLocaleInfo(path, language_code, script_code, country_code, variant_ result['currencyDisplayName'] = '' if result['currencyIsoCode']: result['currencySymbol'] = findEntryDef(path, "numbers/currencies/currency[%s]/symbol" % result['currencyIsoCode']) - display_name_path = "numbers/currencies/currency[%s]/displayName" % result['currencyIsoCode'] - result['currencyDisplayName'] \ - = findEntryDef(path, display_name_path) + ";" \ - + findEntryDef(path, display_name_path + "[count=zero]") + ";" \ - + findEntryDef(path, display_name_path + "[count=one]") + ";" \ - + findEntryDef(path, display_name_path + "[count=two]") + ";" \ - + findEntryDef(path, display_name_path + "[count=few]") + ";" \ - + findEntryDef(path, display_name_path + "[count=many]") + ";" \ - + findEntryDef(path, display_name_path + "[count=other]") + ";" - - standalone_long_month_path = "dates/calendars/calendar[gregorian]/months/monthContext[stand-alone]/monthWidth[wide]/month" - result['standaloneLongMonths'] \ - = findEntry(path, standalone_long_month_path + "[1]") + ";" \ - + findEntry(path, standalone_long_month_path + "[2]") + ";" \ - + findEntry(path, standalone_long_month_path + "[3]") + ";" \ - + findEntry(path, standalone_long_month_path + "[4]") + ";" \ - + findEntry(path, standalone_long_month_path + "[5]") + ";" \ - + findEntry(path, standalone_long_month_path + "[6]") + ";" \ - + findEntry(path, standalone_long_month_path + "[7]") + ";" \ - + findEntry(path, standalone_long_month_path + "[8]") + ";" \ - + findEntry(path, standalone_long_month_path + "[9]") + ";" \ - + findEntry(path, standalone_long_month_path + "[10]") + ";" \ - + findEntry(path, standalone_long_month_path + "[11]") + ";" \ - + findEntry(path, standalone_long_month_path + "[12]") + ";" - - standalone_short_month_path = "dates/calendars/calendar[gregorian]/months/monthContext[stand-alone]/monthWidth[abbreviated]/month" - result['standaloneShortMonths'] \ - = findEntry(path, standalone_short_month_path + "[1]") + ";" \ - + findEntry(path, standalone_short_month_path + "[2]") + ";" \ - + findEntry(path, standalone_short_month_path + "[3]") + ";" \ - + findEntry(path, standalone_short_month_path + "[4]") + ";" \ - + findEntry(path, standalone_short_month_path + "[5]") + ";" \ - + findEntry(path, standalone_short_month_path + "[6]") + ";" \ - + findEntry(path, standalone_short_month_path + "[7]") + ";" \ - + findEntry(path, standalone_short_month_path + "[8]") + ";" \ - + findEntry(path, standalone_short_month_path + "[9]") + ";" \ - + findEntry(path, standalone_short_month_path + "[10]") + ";" \ - + findEntry(path, standalone_short_month_path + "[11]") + ";" \ - + findEntry(path, standalone_short_month_path + "[12]") + ";" - - standalone_narrow_month_path = "dates/calendars/calendar[gregorian]/months/monthContext[stand-alone]/monthWidth[narrow]/month" - result['standaloneNarrowMonths'] \ - = findEntry(path, standalone_narrow_month_path + "[1]") + ";" \ - + findEntry(path, standalone_narrow_month_path + "[2]") + ";" \ - + findEntry(path, standalone_narrow_month_path + "[3]") + ";" \ - + findEntry(path, standalone_narrow_month_path + "[4]") + ";" \ - + findEntry(path, standalone_narrow_month_path + "[5]") + ";" \ - + findEntry(path, standalone_narrow_month_path + "[6]") + ";" \ - + findEntry(path, standalone_narrow_month_path + "[7]") + ";" \ - + findEntry(path, standalone_narrow_month_path + "[8]") + ";" \ - + findEntry(path, standalone_narrow_month_path + "[9]") + ";" \ - + findEntry(path, standalone_narrow_month_path + "[10]") + ";" \ - + findEntry(path, standalone_narrow_month_path + "[11]") + ";" \ - + findEntry(path, standalone_narrow_month_path + "[12]") + ";" - - long_month_path = "dates/calendars/calendar[gregorian]/months/monthContext[format]/monthWidth[wide]/month" - result['longMonths'] \ - = findEntry(path, long_month_path + "[1]") + ";" \ - + findEntry(path, long_month_path + "[2]") + ";" \ - + findEntry(path, long_month_path + "[3]") + ";" \ - + findEntry(path, long_month_path + "[4]") + ";" \ - + findEntry(path, long_month_path + "[5]") + ";" \ - + findEntry(path, long_month_path + "[6]") + ";" \ - + findEntry(path, long_month_path + "[7]") + ";" \ - + findEntry(path, long_month_path + "[8]") + ";" \ - + findEntry(path, long_month_path + "[9]") + ";" \ - + findEntry(path, long_month_path + "[10]") + ";" \ - + findEntry(path, long_month_path + "[11]") + ";" \ - + findEntry(path, long_month_path + "[12]") + ";" - - short_month_path = "dates/calendars/calendar[gregorian]/months/monthContext[format]/monthWidth[abbreviated]/month" - result['shortMonths'] \ - = findEntry(path, short_month_path + "[1]") + ";" \ - + findEntry(path, short_month_path + "[2]") + ";" \ - + findEntry(path, short_month_path + "[3]") + ";" \ - + findEntry(path, short_month_path + "[4]") + ";" \ - + findEntry(path, short_month_path + "[5]") + ";" \ - + findEntry(path, short_month_path + "[6]") + ";" \ - + findEntry(path, short_month_path + "[7]") + ";" \ - + findEntry(path, short_month_path + "[8]") + ";" \ - + findEntry(path, short_month_path + "[9]") + ";" \ - + findEntry(path, short_month_path + "[10]") + ";" \ - + findEntry(path, short_month_path + "[11]") + ";" \ - + findEntry(path, short_month_path + "[12]") + ";" - - narrow_month_path = "dates/calendars/calendar[gregorian]/months/monthContext[format]/monthWidth[narrow]/month" - result['narrowMonths'] \ - = findEntry(path, narrow_month_path + "[1]") + ";" \ - + findEntry(path, narrow_month_path + "[2]") + ";" \ - + findEntry(path, narrow_month_path + "[3]") + ";" \ - + findEntry(path, narrow_month_path + "[4]") + ";" \ - + findEntry(path, narrow_month_path + "[5]") + ";" \ - + findEntry(path, narrow_month_path + "[6]") + ";" \ - + findEntry(path, narrow_month_path + "[7]") + ";" \ - + findEntry(path, narrow_month_path + "[8]") + ";" \ - + findEntry(path, narrow_month_path + "[9]") + ";" \ - + findEntry(path, narrow_month_path + "[10]") + ";" \ - + findEntry(path, narrow_month_path + "[11]") + ";" \ - + findEntry(path, narrow_month_path + "[12]") + ";" - - long_day_path = "dates/calendars/calendar[gregorian]/days/dayContext[format]/dayWidth[wide]/day" - result['longDays'] \ - = findEntry(path, long_day_path + "[sun]") + ";" \ - + findEntry(path, long_day_path + "[mon]") + ";" \ - + findEntry(path, long_day_path + "[tue]") + ";" \ - + findEntry(path, long_day_path + "[wed]") + ";" \ - + findEntry(path, long_day_path + "[thu]") + ";" \ - + findEntry(path, long_day_path + "[fri]") + ";" \ - + findEntry(path, long_day_path + "[sat]") + ";" - - short_day_path = "dates/calendars/calendar[gregorian]/days/dayContext[format]/dayWidth[abbreviated]/day" - result['shortDays'] \ - = findEntry(path, short_day_path + "[sun]") + ";" \ - + findEntry(path, short_day_path + "[mon]") + ";" \ - + findEntry(path, short_day_path + "[tue]") + ";" \ - + findEntry(path, short_day_path + "[wed]") + ";" \ - + findEntry(path, short_day_path + "[thu]") + ";" \ - + findEntry(path, short_day_path + "[fri]") + ";" \ - + findEntry(path, short_day_path + "[sat]") + ";" - - narrow_day_path = "dates/calendars/calendar[gregorian]/days/dayContext[format]/dayWidth[narrow]/day" - result['narrowDays'] \ - = findEntry(path, narrow_day_path + "[sun]") + ";" \ - + findEntry(path, narrow_day_path + "[mon]") + ";" \ - + findEntry(path, narrow_day_path + "[tue]") + ";" \ - + findEntry(path, narrow_day_path + "[wed]") + ";" \ - + findEntry(path, narrow_day_path + "[thu]") + ";" \ - + findEntry(path, narrow_day_path + "[fri]") + ";" \ - + findEntry(path, narrow_day_path + "[sat]") + ";" - - standalone_long_day_path = "dates/calendars/calendar[gregorian]/days/dayContext[stand-alone]/dayWidth[wide]/day" - result['standaloneLongDays'] \ - = findEntry(path, standalone_long_day_path + "[sun]") + ";" \ - + findEntry(path, standalone_long_day_path + "[mon]") + ";" \ - + findEntry(path, standalone_long_day_path + "[tue]") + ";" \ - + findEntry(path, standalone_long_day_path + "[wed]") + ";" \ - + findEntry(path, standalone_long_day_path + "[thu]") + ";" \ - + findEntry(path, standalone_long_day_path + "[fri]") + ";" \ - + findEntry(path, standalone_long_day_path + "[sat]") + ";" - - standalone_short_day_path = "dates/calendars/calendar[gregorian]/days/dayContext[stand-alone]/dayWidth[abbreviated]/day" - result['standaloneShortDays'] \ - = findEntry(path, standalone_short_day_path + "[sun]") + ";" \ - + findEntry(path, standalone_short_day_path + "[mon]") + ";" \ - + findEntry(path, standalone_short_day_path + "[tue]") + ";" \ - + findEntry(path, standalone_short_day_path + "[wed]") + ";" \ - + findEntry(path, standalone_short_day_path + "[thu]") + ";" \ - + findEntry(path, standalone_short_day_path + "[fri]") + ";" \ - + findEntry(path, standalone_short_day_path + "[sat]") + ";" - - standalone_narrow_day_path = "dates/calendars/calendar[gregorian]/days/dayContext[stand-alone]/dayWidth[narrow]/day" - result['standaloneNarrowDays'] \ - = findEntry(path, standalone_narrow_day_path + "[sun]") + ";" \ - + findEntry(path, standalone_narrow_day_path + "[mon]") + ";" \ - + findEntry(path, standalone_narrow_day_path + "[tue]") + ";" \ - + findEntry(path, standalone_narrow_day_path + "[wed]") + ";" \ - + findEntry(path, standalone_narrow_day_path + "[thu]") + ";" \ - + findEntry(path, standalone_narrow_day_path + "[fri]") + ";" \ - + findEntry(path, standalone_narrow_day_path + "[sat]") + ";" + result['currencyDisplayName'] = ';'.join( + findEntryDef(path, 'numbers/currencies/currency[' + result['currencyIsoCode'] + + ']/displayName' + tail) + for tail in ['',] + [ + '[count=%s]' % x for x in ('zero', 'one', 'two', 'few', 'many', 'other') + ]) + ';' + + # Used for month and day data: + namings = ( + ('standaloneLong', 'stand-alone', 'wide'), + ('standaloneShort', 'stand-alone', 'abbreviated'), + ('standaloneNarrow', 'stand-alone', 'narrow'), + ('long', 'format', 'wide'), + ('short', 'format', 'abbreviated'), + ('narrow', 'format', 'narrow'), + ) + + # Month data: + for cal in ('gregorian',): # We shall want to add to this + stem = 'dates/calendars/calendar[' + cal + ']/months/' + for (key, mode, size) in namings: + prop = 'monthContext[' + mode + ']/monthWidth[' + size + ']/' + result[key + 'Months'] = ';'.join( + findEntry(path, stem + prop + "month[%d]" % i) + for i in range(1, 13)) + ';' + + # Day data (for Gregorian, at least): + stem = 'dates/calendars/calendar[gregorian]/days/' + days = ('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat') + for (key, mode, size) in namings: + prop = 'dayContext[' + mode + ']/dayWidth[' + size + ']/day' + result[key + 'Days'] = ';'.join( + findEntry(path, stem + prop + '[' + day + ']') + for day in days) + ';' return result @@ -592,21 +467,21 @@ for file in defaultContent_locales: country_code = items[2] else: if len(items) != 2: - sys.stderr.write("skipping defaultContent locale \"" + file + "\"\n") + sys.stderr.write('skipping defaultContent locale "' + file + '"\n') continue language_code = items[0] script_code = "" country_code = items[1] if len(country_code) == 4: - sys.stderr.write("skipping defaultContent locale \"" + file + "\"\n") + sys.stderr.write('skipping defaultContent locale "' + file + '"\n') continue try: l = _generateLocaleInfo(cldr_dir + "/" + file + ".xml", language_code, script_code, country_code) if not l: - sys.stderr.write("skipping defaultContent locale \"" + file + "\"\n") + sys.stderr.write('skipping defaultContent locale "' + file + '"\n') continue except xpathlite.Error as e: - sys.stderr.write("skipping defaultContent locale \"%s\" (%s)\n" % (file, str(e))) + sys.stderr.write('skipping defaultContent locale "%s" (%s)\n' % (file, str(e))) continue locale_database[(l['language_id'], l['script_id'], l['country_id'], l['variant_code'])] = l @@ -615,10 +490,10 @@ for file in cldr_files: try: l = generateLocaleInfo(cldr_dir + "/" + file) if not l: - sys.stderr.write("skipping file \"" + file + "\"\n") + sys.stderr.write('skipping file "' + file + '"\n') continue except xpathlite.Error as e: - sys.stderr.write("skipping file \"%s\" (%s)\n" % (file, str(e))) + sys.stderr.write('skipping file "%s" (%s)\n' % (file, str(e))) continue locale_database[(l['language_id'], l['script_id'], l['country_id'], l['variant_code'])] = l @@ -678,7 +553,7 @@ def _parseLocale(l): if language_code != "und": language_id = enumdata.languageCodeToId(language_code) if language_id == -1: - raise xpathlite.Error("unknown language code \"%s\"" % language_code) + raise xpathlite.Error('unknown language code "%s"' % language_code) language = enumdata.language_list[language_id][0] if len(items) > 1: @@ -689,14 +564,14 @@ def _parseLocale(l): if len(script_code) == 4: script_id = enumdata.scriptCodeToId(script_code) if script_id == -1: - raise xpathlite.Error("unknown script code \"%s\"" % script_code) + raise xpathlite.Error('unknown script code "%s"' % script_code) script = enumdata.script_list[script_id][0] else: country_code = script_code if country_code: country_id = enumdata.countryCodeToId(country_code) if country_id == -1: - raise xpathlite.Error("unknown country code \"%s\"" % country_code) + raise xpathlite.Error('unknown country code "%s"' % country_code) country = enumdata.country_list[country_id][0] return (language, script, country) @@ -710,12 +585,12 @@ for ns in findTagsInFile(cldr_dir + "/../supplemental/likelySubtags.xml", "likel try: (from_language, from_script, from_country) = _parseLocale(tmp[u"from"]) except xpathlite.Error as e: - sys.stderr.write("skipping likelySubtag \"%s\" -> \"%s\" (%s)\n" % (tmp[u"from"], tmp[u"to"], str(e))) + sys.stderr.write('skipping likelySubtag "%s" -> "%s" (%s)\n' % (tmp[u"from"], tmp[u"to"], str(e))) continue try: (to_language, to_script, to_country) = _parseLocale(tmp[u"to"]) except xpathlite.Error as e: - sys.stderr.write("skipping likelySubtag \"%s\" -> \"%s\" (%s)\n" % (tmp[u"from"], tmp[u"to"], str(e))) + sys.stderr.write('skipping likelySubtag "%s" -> "%s" (%s)\n' % (tmp[u"from"], tmp[u"to"], str(e))) continue # substitute according to http://www.unicode.org/reports/tr35/#Likely_Subtags if to_country == "AnyCountry" and from_country != to_country: @@ -738,58 +613,58 @@ for ns in findTagsInFile(cldr_dir + "/../supplemental/likelySubtags.xml", "likel print " " print " " -print \ -" \n\ - C\n\ - \n\ - \n\ - AnyCountry\n\ - \n\ - 46\n\ - 44\n\ - 59\n\ - 37\n\ - 48\n\ - 45\n\ - 43\n\ - 101\n\ - \"\n\ - \"\n\ - \'\n\ - \'\n\ - %1, %2\n\ - %1, %2\n\ - %1, %2\n\ - %1, %2\n\ - AM\n\ - PM\n\ - mon\n\ - sat\n\ - sun\n\ - EEEE, d MMMM yyyy\n\ - d MMM yyyy\n\ - HH:mm:ss z\n\ - HH:mm:ss\n\ - January;February;March;April;May;June;July;August;September;October;November;December;\n\ - Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec;\n\ - J;F;M;A;M;J;J;A;S;O;N;D;\n\ - January;February;March;April;May;June;July;August;September;October;November;December;\n\ - Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec;\n\ - 1;2;3;4;5;6;7;8;9;10;11;12;\n\ - Sunday;Monday;Tuesday;Wednesday;Thursday;Friday;Saturday;\n\ - Sun;Mon;Tue;Wed;Thu;Fri;Sat;\n\ - 7;1;2;3;4;5;6;\n\ - Sunday;Monday;Tuesday;Wednesday;Thursday;Friday;Saturday;\n\ - Sun;Mon;Tue;Wed;Thu;Fri;Sat;\n\ - S;M;T;W;T;F;S;\n\ - \n\ - \n\ - ;;;;;;;\n\ - 2\n\ - 1\n\ - %1%2\n\ - \n\ - " +print '''\ + + C + + + AnyCountry + + 46 + 44 + 59 + 37 + 48 + 45 + 43 + 101 + " + " + \' + \' + %1, %2 + %1, %2 + %1, %2 + %1, %2 + AM + PM + mon + sat + sun + EEEE, d MMMM yyyy + d MMM yyyy + HH:mm:ss z + HH:mm:ss + January;February;March;April;May;June;July;August;September;October;November;December; + Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec; + J;F;M;A;M;J;J;A;S;O;N;D; + January;February;March;April;May;June;July;August;September;October;November;December; + Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec; + 1;2;3;4;5;6;7;8;9;10;11;12; + Sunday;Monday;Tuesday;Wednesday;Thursday;Friday;Saturday; + Sun;Mon;Tue;Wed;Thu;Fri;Sat; + 7;1;2;3;4;5;6; + Sunday;Monday;Tuesday;Wednesday;Thursday;Friday;Saturday; + Sun;Mon;Tue;Wed;Thu;Fri;Sat; + S;M;T;W;T;F;S; + + + ;;;;;;; + 2 + 1 + %1%2 + + ''' for key in locale_keys: l = locale_database[key] diff --git a/util/local_database/cldr2qtimezone.py b/util/local_database/cldr2qtimezone.py old mode 100644 new mode 100755 index bf30e39910..72abb3c355 --- a/util/local_database/cldr2qtimezone.py +++ b/util/local_database/cldr2qtimezone.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python2 ############################################################################# ## ## Copyright (C) 2016 The Qt Company Ltd. @@ -335,15 +335,17 @@ while s and s != GENERATED_BLOCK_START: # Write out generated block start tag and warning newTempFile.write(GENERATED_BLOCK_START) -newTempFile.write("\n\ -/*\n\ - This part of the file was generated on %s from the\n\ - Common Locale Data Repository v%s supplemental/windowsZones.xml file %s\n\ -\n\ - http://www.unicode.org/cldr/\n\ -\n\ - Do not change this data, only generate it using cldr2qtimezone.py.\n\ -*/\n\n" % (str(datetime.date.today()), cldr_version, versionNumber) ) +newTempFile.write(""" +/* + This part of the file was generated on %s from the + Common Locale Data Repository v%s supplemental/windowsZones.xml file %s + + http://www.unicode.org/cldr/ + + Do not change this data, only generate it using cldr2qtimezone.py. +*/ + +""" % (str(datetime.date.today()), cldr_version, versionNumber) ) windowsIdData = ByteArrayData() ianaIdData = ByteArrayData() @@ -353,7 +355,7 @@ newTempFile.write("// Windows ID Key, Country Enum, IANA ID Index\n") newTempFile.write("static const QZoneData zoneDataTable[] = {\n") for index in windowsIdDict: data = windowsIdDict[index] - newTempFile.write(" { %6d,%6d,%6d }, // %s / %s\n" \ + newTempFile.write(" { %6d,%6d,%6d }, // %s / %s\n" % (data['windowsKey'], data['countryId'], ianaIdData.append(data['ianaList']), @@ -368,7 +370,7 @@ print "Done Zone Data" newTempFile.write("// Windows ID Key, Windows ID Index, IANA ID Index, UTC Offset\n") newTempFile.write("static const QWindowsData windowsDataTable[] = {\n") for windowsKey in windowsIdList: - newTempFile.write(" { %6d,%6d,%6d,%6d }, // %s\n" \ + newTempFile.write(" { %6d,%6d,%6d,%6d }, // %s\n" % (windowsKey, windowsIdData.append(windowsIdList[windowsKey][0]), ianaIdData.append(defaultDict[windowsKey]), @@ -384,7 +386,7 @@ newTempFile.write("// IANA ID Index, UTC Offset\n") newTempFile.write("static const QUtcData utcDataTable[] = {\n") for index in utcIdList: data = utcIdList[index] - newTempFile.write(" { %6d,%6d }, // %s\n" \ + newTempFile.write(" { %6d,%6d }, // %s\n" % (ianaIdData.append(data[0]), data[1], data[0])) diff --git a/util/local_database/qlocalexml2cpp.py b/util/local_database/qlocalexml2cpp.py index c63739e892..41adbb96ee 100755 --- a/util/local_database/qlocalexml2cpp.py +++ b/util/local_database/qlocalexml2cpp.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python2 ############################################################################# ## ## Copyright (C) 2016 The Qt Company Ltd. @@ -47,9 +47,10 @@ class Error: def wrap_list(lst): def split(lst, size): - for i in range(len(lst)/size+1): - yield lst[i*size:(i+1)*size] - return ",\n".join(map(lambda x: ", ".join(x), split(lst, 20))) + while lst: + head, lst = lst[:size], lst[size:] + yield head + return ",\n".join(", ".join(x) for x in split(lst, 20)) def firstChildElt(parent, name): child = parent.firstChild @@ -443,10 +444,10 @@ def escapedString(s): line += "\\x%02x" % (ord(c)) need_escape = True if len(line) > 80: - result = result + "\n" + "\"" + line + "\"" + result = result + "\n" + '"' + line + '"' line = "" line += "\\0" - result = result + "\n" + "\"" + line + "\"" + result = result + "\n" + '"' + line + '"' if result[0] == "\n": result = result[1:] return result @@ -457,7 +458,7 @@ def printEscapedString(s): def currencyIsoCodeData(s): if s: - return ",".join(map(lambda x: str(ord(x)), s)) + return ",".join(str(ord(x)) for x in s) return "0,0,0" def usage(): @@ -507,17 +508,18 @@ def main(): cldr_version = eltText(firstChildElt(doc.documentElement, "version")) - data_temp_file.write("\n\ -/*\n\ - This part of the file was generated on %s from the\n\ - Common Locale Data Repository v%s\n\ -\n\ - http://www.unicode.org/cldr/\n\ -\n\ - Do not change it, instead edit CLDR data and regenerate this file using\n\ - cldr2qlocalexml.py and qlocalexml2cpp.py.\n\ -*/\n\n\n\ -" % (str(datetime.date.today()), cldr_version) ) + data_temp_file.write(""" +/* + This part of the file was generated on %s from the + Common Locale Data Repository v%s + + http://www.unicode.org/cldr/ + + Do not change it, instead edit CLDR data and regenerate this file using + cldr2qlocalexml.py and qlocalexml2cpp.py. +*/ + +""" % (str(datetime.date.today()), cldr_version) ) # Likely subtags map data_temp_file.write("static const QLocaleId likely_subtags[] = {\n") @@ -741,11 +743,11 @@ def main(): # Language name list data_temp_file.write("static const char language_name_list[] =\n") - data_temp_file.write("\"Default\\0\"\n") + data_temp_file.write('"Default\\0"\n') for key in language_map.keys(): if key == 0: continue - data_temp_file.write("\"" + language_map[key][0] + "\\0\"\n") + data_temp_file.write('"' + language_map[key][0] + '\\0"\n') data_temp_file.write(";\n") data_temp_file.write("\n") @@ -766,11 +768,11 @@ def main(): # Script name list data_temp_file.write("static const char script_name_list[] =\n") - data_temp_file.write("\"Default\\0\"\n") + data_temp_file.write('"Default\\0"\n') for key in script_map.keys(): if key == 0: continue - data_temp_file.write("\"" + script_map[key][0] + "\\0\"\n") + data_temp_file.write('"' + script_map[key][0] + '\\0"\n') data_temp_file.write(";\n") data_temp_file.write("\n") @@ -791,11 +793,11 @@ def main(): # Country name list data_temp_file.write("static const char country_name_list[] =\n") - data_temp_file.write("\"Default\\0\"\n") + data_temp_file.write('"Default\\0"\n') for key in country_map.keys(): if key == 0: continue - data_temp_file.write("\"" + country_map[key][0] + "\\0\"\n") + data_temp_file.write('"' + country_map[key][0] + '\\0"\n') data_temp_file.write(";\n") data_temp_file.write("\n") @@ -820,7 +822,7 @@ def main(): code = language_map[key][1] if len(code) == 2: code += r"\0" - data_temp_file.write("\"%2s\" // %s\n" % (code, language_map[key][0])) + data_temp_file.write('"%2s" // %s\n' % (code, language_map[key][0])) data_temp_file.write(";\n") data_temp_file.write("\n") @@ -831,7 +833,7 @@ def main(): code = script_map[key][1] for i in range(4 - len(code)): code += "\\0" - data_temp_file.write("\"%2s\" // %s\n" % (code, script_map[key][0])) + data_temp_file.write('"%2s" // %s\n' % (code, script_map[key][0])) data_temp_file.write(";\n") # Country code list @@ -840,13 +842,13 @@ def main(): code = country_map[key][1] if len(code) == 2: code += "\\0" - data_temp_file.write("\"%2s\" // %s\n" % (code, country_map[key][0])) + data_temp_file.write('"%2s" // %s\n' % (code, country_map[key][0])) data_temp_file.write(";\n") data_temp_file.write("\n") data_temp_file.write(GENERATED_BLOCK_END) s = qlocaledata_file.readline() - # skip until end of the block + # skip until end of the old block while s and s != GENERATED_BLOCK_END: s = qlocaledata_file.readline() @@ -938,7 +940,7 @@ def main(): qlocaleh_temp_file.write(GENERATED_BLOCK_END) s = qlocaleh_file.readline() - # skip until end of the block + # skip until end of the old block while s and s != GENERATED_BLOCK_END: s = qlocaleh_file.readline() -- cgit v1.2.3