Merge remote branch 'staging/master' into refactor
[profile/ivi/qtbase.git] / util / local_database / qlocalexml2cpp.py
1 #!/usr/bin/env python
2 #############################################################################
3 ##
4 ## Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
5 ## All rights reserved.
6 ## Contact: Nokia Corporation (qt-info@nokia.com)
7 ##
8 ## This file is part of the test suite of the Qt Toolkit.
9 ##
10 ## $QT_BEGIN_LICENSE:LGPL$
11 ## GNU Lesser General Public License Usage
12 ## This file may be used under the terms of the GNU Lesser General Public
13 ## License version 2.1 as published by the Free Software Foundation and
14 ## appearing in the file LICENSE.LGPL included in the packaging of this
15 ## file. Please review the following information to ensure the GNU Lesser
16 ## General Public License version 2.1 requirements will be met:
17 ## http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ##
19 ## In addition, as a special exception, Nokia gives you certain additional
20 ## rights. These rights are described in the Nokia Qt LGPL Exception
21 ## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ##
23 ## GNU General Public License Usage
24 ## Alternatively, this file may be used under the terms of the GNU General
25 ## Public License version 3.0 as published by the Free Software Foundation
26 ## and appearing in the file LICENSE.GPL included in the packaging of this
27 ## file. Please review the following information to ensure the GNU General
28 ## Public License version 3.0 requirements will be met:
29 ## http://www.gnu.org/copyleft/gpl.html.
30 ##
31 ## Other Usage
32 ## Alternatively, this file may be used in accordance with the terms and
33 ## conditions contained in a signed written agreement between you and Nokia.
34 ##
35 ##
36 ##
37 ##
38 ##
39 ## $QT_END_LICENSE$
40 ##
41 #############################################################################
42
43 import os
44 import sys
45 import tempfile
46 import datetime
47 import xml.dom.minidom
48
49 class Error:
50     def __init__(self, msg):
51         self.msg = msg
52     def __str__(self):
53         return self.msg
54
55 def check_static_char_array_length(name, array):
56     # some compilers like VC6 doesn't allow static arrays more than 64K bytes size.
57     size = reduce(lambda x, y: x+len(escapedString(y)), array, 0)
58     if size > 65535:
59         print "\n\n\n#error Array %s is too long! " % name
60         sys.stderr.write("\n\n\nERROR: the content of the array '%s' is too long: %d > 65535 " % (name, size))
61         sys.exit(1)
62
63 def wrap_list(lst):
64     def split(lst, size):
65         for i in range(len(lst)/size+1):
66             yield lst[i*size:(i+1)*size]
67     return ",\n".join(map(lambda x: ", ".join(x), split(lst, 20)))
68
69 def firstChildElt(parent, name):
70     child = parent.firstChild
71     while child:
72         if child.nodeType == parent.ELEMENT_NODE \
73             and (not name or child.nodeName == name):
74             return child
75         child = child.nextSibling
76     return False
77
78 def nextSiblingElt(sibling, name):
79     sib = sibling.nextSibling
80     while sib:
81         if sib.nodeType == sibling.ELEMENT_NODE \
82             and (not name or sib.nodeName == name):
83             return sib
84         sib = sib.nextSibling
85     return False
86
87 def eltText(elt):
88     result = ""
89     child = elt.firstChild
90     while child:
91         if child.nodeType == elt.TEXT_NODE:
92             if result:
93                 result += " "
94             result += child.nodeValue
95         child = child.nextSibling
96     return result
97
98 def loadLanguageMap(doc):
99     result = {}
100
101     language_list_elt = firstChildElt(doc.documentElement, "languageList")
102     language_elt = firstChildElt(language_list_elt, "language")
103     while language_elt:
104         language_id = int(eltText(firstChildElt(language_elt, "id")))
105         language_name = eltText(firstChildElt(language_elt, "name"))
106         language_code = eltText(firstChildElt(language_elt, "code"))
107         result[language_id] = (language_name, language_code)
108         language_elt = nextSiblingElt(language_elt, "language")
109
110     return result
111
112 def loadScriptMap(doc):
113     result = {}
114
115     script_list_elt = firstChildElt(doc.documentElement, "scriptList")
116     script_elt = firstChildElt(script_list_elt, "script")
117     while script_elt:
118         script_id = int(eltText(firstChildElt(script_elt, "id")))
119         script_name = eltText(firstChildElt(script_elt, "name"))
120         script_code = eltText(firstChildElt(script_elt, "code"))
121         result[script_id] = (script_name, script_code)
122         script_elt = nextSiblingElt(script_elt, "script")
123
124     return result
125
126 def loadCountryMap(doc):
127     result = {}
128
129     country_list_elt = firstChildElt(doc.documentElement, "countryList")
130     country_elt = firstChildElt(country_list_elt, "country")
131     while country_elt:
132         country_id = int(eltText(firstChildElt(country_elt, "id")))
133         country_name = eltText(firstChildElt(country_elt, "name"))
134         country_code = eltText(firstChildElt(country_elt, "code"))
135         result[country_id] = (country_name, country_code)
136         country_elt = nextSiblingElt(country_elt, "country")
137
138     return result
139
140 def loadDefaultMap(doc):
141     result = {}
142
143     list_elt = firstChildElt(doc.documentElement, "defaultCountryList")
144     elt = firstChildElt(list_elt, "defaultCountry")
145     while elt:
146         country = eltText(firstChildElt(elt, "country"));
147         language = eltText(firstChildElt(elt, "language"));
148         result[language] = country;
149         elt = nextSiblingElt(elt, "defaultCountry");
150     return result
151
152 def fixedScriptName(name, dupes):
153     name = name.replace(" ", "")
154     if name[-6:] != "Script":
155         name = name + "Script";
156     if name in dupes:
157         sys.stderr.write("\n\n\nERROR: The script name '%s' is messy" % name)
158         sys.exit(1);
159     return name
160
161 def fixedCountryName(name, dupes):
162     if name in dupes:
163         return name.replace(" ", "") + "Country"
164     return name.replace(" ", "")
165
166 def fixedLanguageName(name, dupes):
167     if name in dupes:
168         return name.replace(" ", "") + "Language"
169     return name.replace(" ", "")
170
171 def findDupes(country_map, language_map):
172     country_set = set([ v[0] for a, v in country_map.iteritems() ])
173     language_set = set([ v[0] for a, v in language_map.iteritems() ])
174     return country_set & language_set
175
176 def languageNameToId(name, language_map):
177     for key in language_map.keys():
178         if language_map[key][0] == name:
179             return key
180     return -1
181
182 def scriptNameToId(name, script_map):
183     for key in script_map.keys():
184         if script_map[key][0] == name:
185             return key
186     return -1
187
188 def countryNameToId(name, country_map):
189     for key in country_map.keys():
190         if country_map[key][0] == name:
191             return key
192     return -1
193
194 def convertFormat(format):
195     result = ""
196     i = 0
197     while i < len(format):
198         if format[i] == "'":
199             result += "'"
200             i += 1
201             while i < len(format) and format[i] != "'":
202                 result += format[i]
203                 i += 1
204             if i < len(format):
205                 result += "'"
206                 i += 1
207         else:
208             s = format[i:]
209             if s.startswith("EEEE"):
210                 result += "dddd"
211                 i += 4
212             elif s.startswith("EEE"):
213                 result += "ddd"
214                 i += 3
215             elif s.startswith("a"):
216                 result += "AP"
217                 i += 1
218             elif s.startswith("z"):
219                 result += "t"
220                 i += 1
221             elif s.startswith("v"):
222                 i += 1
223             else:
224                 result += format[i]
225                 i += 1
226
227     return result
228
229 def convertToQtDayOfWeek(firstDay):
230     qtDayOfWeek = {"mon":1, "tue":2, "wed":3, "thu":4, "fri":5, "sat":6, "sun":7}
231     return qtDayOfWeek[firstDay]
232
233 def assertSingleChar(string):
234     assert len(string) == 1, "This string is not allowed to be longer than 1 character"
235     return string
236
237 class Locale:
238     def __init__(self, elt):
239         self.language = eltText(firstChildElt(elt, "language"))
240         self.languageEndonym = eltText(firstChildElt(elt, "languageEndonym"))
241         self.script = eltText(firstChildElt(elt, "script"))
242         self.country = eltText(firstChildElt(elt, "country"))
243         self.countryEndonym = eltText(firstChildElt(elt, "countryEndonym"))
244         self.decimal = int(eltText(firstChildElt(elt, "decimal")))
245         self.group = int(eltText(firstChildElt(elt, "group")))
246         self.listDelim = int(eltText(firstChildElt(elt, "list")))
247         self.percent = int(eltText(firstChildElt(elt, "percent")))
248         self.zero = int(eltText(firstChildElt(elt, "zero")))
249         self.minus = int(eltText(firstChildElt(elt, "minus")))
250         self.plus = int(eltText(firstChildElt(elt, "plus")))
251         self.exp = int(eltText(firstChildElt(elt, "exp")))
252         self.quotationStart = ord(assertSingleChar(eltText(firstChildElt(elt, "quotationStart"))))
253         self.quotationEnd = ord(assertSingleChar(eltText(firstChildElt(elt, "quotationEnd"))))
254         self.alternateQuotationStart = ord(assertSingleChar(eltText(firstChildElt(elt, "alternateQuotationStart"))))
255         self.alternateQuotationEnd = ord(assertSingleChar(eltText(firstChildElt(elt, "alternateQuotationEnd"))))
256         self.listPatternPartStart = eltText(firstChildElt(elt, "listPatternPartStart"))
257         self.listPatternPartMiddle = eltText(firstChildElt(elt, "listPatternPartMiddle"))
258         self.listPatternPartEnd = eltText(firstChildElt(elt, "listPatternPartEnd"))
259         self.listPatternPartTwo = eltText(firstChildElt(elt, "listPatternPartTwo"))
260         self.am = eltText(firstChildElt(elt, "am"))
261         self.pm = eltText(firstChildElt(elt, "pm"))
262         self.firstDayOfWeek = convertToQtDayOfWeek(eltText(firstChildElt(elt, "firstDayOfWeek")))
263         self.weekendStart = convertToQtDayOfWeek(eltText(firstChildElt(elt, "weekendStart")))
264         self.weekendEnd = convertToQtDayOfWeek(eltText(firstChildElt(elt, "weekendEnd")))
265         self.longDateFormat = convertFormat(eltText(firstChildElt(elt, "longDateFormat")))
266         self.shortDateFormat = convertFormat(eltText(firstChildElt(elt, "shortDateFormat")))
267         self.longTimeFormat = convertFormat(eltText(firstChildElt(elt, "longTimeFormat")))
268         self.shortTimeFormat = convertFormat(eltText(firstChildElt(elt, "shortTimeFormat")))
269         self.standaloneLongMonths = eltText(firstChildElt(elt, "standaloneLongMonths"))
270         self.standaloneShortMonths = eltText(firstChildElt(elt, "standaloneShortMonths"))
271         self.standaloneNarrowMonths = eltText(firstChildElt(elt, "standaloneNarrowMonths"))
272         self.longMonths = eltText(firstChildElt(elt, "longMonths"))
273         self.shortMonths = eltText(firstChildElt(elt, "shortMonths"))
274         self.narrowMonths = eltText(firstChildElt(elt, "narrowMonths"))
275         self.standaloneLongDays = eltText(firstChildElt(elt, "standaloneLongDays"))
276         self.standaloneShortDays = eltText(firstChildElt(elt, "standaloneShortDays"))
277         self.standaloneNarrowDays = eltText(firstChildElt(elt, "standaloneNarrowDays"))
278         self.longDays = eltText(firstChildElt(elt, "longDays"))
279         self.shortDays = eltText(firstChildElt(elt, "shortDays"))
280         self.narrowDays = eltText(firstChildElt(elt, "narrowDays"))
281         self.currencyIsoCode = eltText(firstChildElt(elt, "currencyIsoCode"))
282         self.currencySymbol = eltText(firstChildElt(elt, "currencySymbol"))
283         self.currencyDisplayName = eltText(firstChildElt(elt, "currencyDisplayName"))
284         self.currencyDigits = int(eltText(firstChildElt(elt, "currencyDigits")))
285         self.currencyRounding = int(eltText(firstChildElt(elt, "currencyRounding")))
286         self.currencyFormat = eltText(firstChildElt(elt, "currencyFormat"))
287         self.currencyNegativeFormat = eltText(firstChildElt(elt, "currencyNegativeFormat"))
288
289 def loadLocaleMap(doc, language_map, script_map, country_map):
290     result = {}
291
292     locale_list_elt = firstChildElt(doc.documentElement, "localeList")
293     locale_elt = firstChildElt(locale_list_elt, "locale")
294     while locale_elt:
295         locale = Locale(locale_elt)
296         language_id = languageNameToId(locale.language, language_map)
297         if language_id == -1:
298             sys.stderr.write("Cannot find a language id for '%s'\n" % locale.language)
299         script_id = scriptNameToId(locale.script, script_map)
300         if script_id == -1:
301             sys.stderr.write("Cannot find a script id for '%s'\n" % locale.script)
302         country_id = countryNameToId(locale.country, country_map)
303         if country_id == -1:
304             sys.stderr.write("Cannot find a country id for '%s'\n" % locale.country)
305         result[(language_id, script_id, country_id)] = locale
306
307         locale_elt = nextSiblingElt(locale_elt, "locale")
308
309     return result
310
311 def compareLocaleKeys(key1, key2):
312     if key1 == key2:
313         return 0
314
315     if key1[0] == key2[0]:
316         l1 = compareLocaleKeys.locale_map[key1]
317         l2 = compareLocaleKeys.locale_map[key2]
318
319         if l1.language in compareLocaleKeys.default_map:
320             default = compareLocaleKeys.default_map[l1.language]
321             if l1.country == default and key1[1] == 0:
322                 return -1
323             if l2.country == default and key2[1] == 0:
324                 return 1
325
326         if key1[1] != key2[1]:
327             return key1[1] - key2[1]
328     else:
329         return key1[0] - key2[0]
330
331     return key1[2] - key2[2]
332
333
334 def languageCount(language_id, locale_map):
335     result = 0
336     for key in locale_map.keys():
337         if key[0] == language_id:
338             result += 1
339     return result
340
341 def unicode2hex(s):
342     lst = []
343     for x in s:
344         v = ord(x)
345         if v > 0xFFFF:
346             # make a surrogate pair
347             # copied from qchar.h
348             high = (v >> 10) + 0xd7c0
349             low = (v % 0x400 + 0xdc00)
350             lst.append(hex(high))
351             lst.append(hex(low))
352         else:
353             lst.append(hex(v))
354     return lst
355
356 class StringDataToken:
357     def __init__(self, index, length):
358         if index > 0xFFFF or length > 0xFFFF:
359             raise Error("Position exceeds ushort range: %d,%d " % (index, length))
360         self.index = index
361         self.length = length
362     def __str__(self):
363         return " %d,%d " % (self.index, self.length)
364
365 class StringData:
366     def __init__(self):
367         self.data = []
368         self.hash = {}
369     def append(self, s):
370         if s in self.hash:
371             return self.hash[s]
372
373         lst = unicode2hex(s)
374         index = len(self.data)
375         if index > 65535:
376             print "\n\n\n#error Data index is too big!"
377             sys.stderr.write ("\n\n\nERROR: index exceeds the uint16 range! index = %d\n" % index)
378             sys.exit(1)
379         size = len(lst)
380         if size >= 65535:
381             print "\n\n\n#error Data is too big!"
382             sys.stderr.write ("\n\n\nERROR: data size exceeds the uint16 range! size = %d\n" % size)
383             sys.exit(1)
384         token = None
385         try:
386             token = StringDataToken(index, size)
387         except Error as e:
388             sys.stderr.write("\n\n\nERROR: %s: on data '%s'" % (e, s))
389             sys.exit(1)
390         self.hash[s] = token
391         self.data += lst
392         return token
393
394 def escapedString(s):
395     result = ""
396     i = 0
397     while i < len(s):
398         if s[i] == '"':
399             result += '\\"'
400             i += 1
401         else:
402             result += s[i]
403             i += 1
404     s = result
405
406     line = ""
407     need_escape = False
408     result = ""
409     for c in s:
410         if ord(c) < 128 and (not need_escape or ord(c.lower()) < ord('a') or ord(c.lower()) > ord('f')):
411             line += c
412             need_escape = False
413         else:
414             line += "\\x%02x" % (ord(c))
415             need_escape = True
416         if len(line) > 80:
417             result = result + "\n" + "\"" + line + "\""
418             line = ""
419     line += "\\0"
420     result = result + "\n" + "\"" + line + "\""
421     if result[0] == "\n":
422         result = result[1:]
423     return result
424
425 def printEscapedString(s):
426     print escapedString(s);
427
428
429 def currencyIsoCodeData(s):
430     if s:
431         return ",".join(map(lambda x: str(ord(x)), s))
432     return "0,0,0"
433
434 def usage():
435     print "Usage: qlocalexml2cpp.py <path-to-locale.xml> <path-to-qt-src-tree>"
436     sys.exit(1)
437
438 GENERATED_BLOCK_START = "// GENERATED PART STARTS HERE\n"
439 GENERATED_BLOCK_END = "// GENERATED PART ENDS HERE\n"
440
441 def main():
442     if len(sys.argv) != 3:
443         usage()
444
445     localexml = sys.argv[1]
446     qtsrcdir = sys.argv[2]
447
448     if not os.path.exists(qtsrcdir) or not os.path.exists(qtsrcdir):
449         usage()
450     if not os.path.isfile(qtsrcdir + "/src/corelib/tools/qlocale_data_p.h"):
451         usage()
452     if not os.path.isfile(qtsrcdir + "/src/corelib/tools/qlocale.h"):
453         usage()
454     if not os.path.isfile(qtsrcdir + "/src/corelib/tools/qlocale.qdoc"):
455         usage()
456
457     (data_temp_file, data_temp_file_path) = tempfile.mkstemp("qlocale_data_p", dir=qtsrcdir)
458     data_temp_file = os.fdopen(data_temp_file, "w")
459     qlocaledata_file = open(qtsrcdir + "/src/corelib/tools/qlocale_data_p.h", "r")
460     s = qlocaledata_file.readline()
461     while s and s != GENERATED_BLOCK_START:
462         data_temp_file.write(s)
463         s = qlocaledata_file.readline()
464     data_temp_file.write(GENERATED_BLOCK_START)
465
466     doc = xml.dom.minidom.parse(localexml)
467     language_map = loadLanguageMap(doc)
468     script_map = loadScriptMap(doc)
469     country_map = loadCountryMap(doc)
470     default_map = loadDefaultMap(doc)
471     locale_map = loadLocaleMap(doc, language_map, script_map, country_map)
472     dupes = findDupes(language_map, country_map)
473
474     cldr_version = eltText(firstChildElt(doc.documentElement, "version"))
475
476     data_temp_file.write("\n\
477 /*\n\
478     This part of the file was generated on %s from the\n\
479     Common Locale Data Repository v%s\n\
480 \n\
481     http://www.unicode.org/cldr/\n\
482 \n\
483     Do not change it, instead edit CLDR data and regenerate this file using\n\
484     cldr2qlocalexml.py and qlocalexml2cpp.py.\n\
485 */\n\n\n\
486 " % (str(datetime.date.today()), cldr_version) )
487
488     # Locale index
489     data_temp_file.write("static const quint16 locale_index[] = {\n")
490     index = 0
491     for key in language_map.keys():
492         i = 0
493         count = languageCount(key, locale_map)
494         if count > 0:
495             i = index
496             index += count
497         data_temp_file.write("%6d, // %s\n" % (i, language_map[key][0]))
498     data_temp_file.write("     0 // trailing 0\n")
499     data_temp_file.write("};\n")
500
501     data_temp_file.write("\n")
502
503     list_pattern_part_data = StringData()
504     date_format_data = StringData()
505     time_format_data = StringData()
506     months_data = StringData()
507     standalone_months_data = StringData()
508     days_data = StringData()
509     am_data = StringData()
510     pm_data = StringData()
511     currency_symbol_data = StringData()
512     currency_display_name_data = StringData()
513     currency_format_data = StringData()
514     endonyms_data = StringData()
515
516     # Locale data
517     data_temp_file.write("static const QLocalePrivate locale_data[] = {\n")
518     data_temp_file.write("//      lang   script terr    dec  group   list  prcnt   zero  minus  plus    exp quotStart quotEnd altQuotStart altQuotEnd lpStart lpMid lpEnd lpTwo sDtFmt lDtFmt sTmFmt lTmFmt ssMonth slMonth  sMonth lMonth  sDays  lDays  am,len      pm,len\n")
519
520     locale_keys = locale_map.keys()
521     compareLocaleKeys.default_map = default_map
522     compareLocaleKeys.locale_map = locale_map
523     locale_keys.sort(compareLocaleKeys)
524
525     for key in locale_keys:
526         l = locale_map[key]
527         data_temp_file.write("    { %6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, {%s}, %s,%s,%s,%s,%s,%s,%6d,%6d,%6d,%6d,%6d }, // %s/%s/%s\n" \
528                     % (key[0], key[1], key[2],
529                         l.decimal,
530                         l.group,
531                         l.listDelim,
532                         l.percent,
533                         l.zero,
534                         l.minus,
535                         l.plus,
536                         l.exp,
537                         l.quotationStart,
538                         l.quotationEnd,
539                         l.alternateQuotationStart,
540                         l.alternateQuotationEnd,
541                         list_pattern_part_data.append(l.listPatternPartStart),
542                         list_pattern_part_data.append(l.listPatternPartMiddle),
543                         list_pattern_part_data.append(l.listPatternPartEnd),
544                         list_pattern_part_data.append(l.listPatternPartTwo),
545                         date_format_data.append(l.shortDateFormat),
546                         date_format_data.append(l.longDateFormat),
547                         time_format_data.append(l.shortTimeFormat),
548                         time_format_data.append(l.longTimeFormat),
549                         standalone_months_data.append(l.standaloneShortMonths),
550                         standalone_months_data.append(l.standaloneLongMonths),
551                         standalone_months_data.append(l.standaloneNarrowMonths),
552                         months_data.append(l.shortMonths),
553                         months_data.append(l.longMonths),
554                         months_data.append(l.narrowMonths),
555                         days_data.append(l.standaloneShortDays),
556                         days_data.append(l.standaloneLongDays),
557                         days_data.append(l.standaloneNarrowDays),
558                         days_data.append(l.shortDays),
559                         days_data.append(l.longDays),
560                         days_data.append(l.narrowDays),
561                         am_data.append(l.am),
562                         pm_data.append(l.pm),
563                         currencyIsoCodeData(l.currencyIsoCode),
564                         currency_symbol_data.append(l.currencySymbol),
565                         currency_display_name_data.append(l.currencyDisplayName),
566                         currency_format_data.append(l.currencyFormat),
567                         currency_format_data.append(l.currencyNegativeFormat),
568                         endonyms_data.append(l.languageEndonym),
569                         endonyms_data.append(l.countryEndonym),
570                         l.currencyDigits,
571                         l.currencyRounding,
572                         l.firstDayOfWeek,
573                         l.weekendStart,
574                         l.weekendEnd,
575                         l.language,
576                         l.script,
577                         l.country))
578     data_temp_file.write("    {      0,      0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,    0,0,    0,0,    0,0,   0,0,     0,0,     0,0,     0,0,     0,0,     0,0,     0,0,     0,0,    0,0,    0,0,    0,0,   0,0,   0,0,   0,0,   0,0,   0,0,   0,0,   0,0,   0,0, {0,0,0}, 0,0, 0,0, 0,0, 0,0, 0, 0, 0, 0, 0, 0,0, 0,0 }  // trailing 0s\n")
579     data_temp_file.write("};\n")
580
581     data_temp_file.write("\n")
582
583     # List patterns data
584     #check_static_char_array_length("list_pattern_part", list_pattern_part_data.data)
585     data_temp_file.write("static const ushort list_pattern_part_data[] = {\n")
586     data_temp_file.write(wrap_list(list_pattern_part_data.data))
587     data_temp_file.write("\n};\n")
588
589     data_temp_file.write("\n")
590
591     # Date format data
592     #check_static_char_array_length("date_format", date_format_data.data)
593     data_temp_file.write("static const ushort date_format_data[] = {\n")
594     data_temp_file.write(wrap_list(date_format_data.data))
595     data_temp_file.write("\n};\n")
596
597     data_temp_file.write("\n")
598
599     # Time format data
600     #check_static_char_array_length("time_format", time_format_data.data)
601     data_temp_file.write("static const ushort time_format_data[] = {\n")
602     data_temp_file.write(wrap_list(time_format_data.data))
603     data_temp_file.write("\n};\n")
604
605     data_temp_file.write("\n")
606
607     # Months data
608     #check_static_char_array_length("months", months_data.data)
609     data_temp_file.write("static const ushort months_data[] = {\n")
610     data_temp_file.write(wrap_list(months_data.data))
611     data_temp_file.write("\n};\n")
612
613     data_temp_file.write("\n")
614
615     # Standalone months data
616     #check_static_char_array_length("standalone_months", standalone_months_data.data)
617     data_temp_file.write("static const ushort standalone_months_data[] = {\n")
618     data_temp_file.write(wrap_list(standalone_months_data.data))
619     data_temp_file.write("\n};\n")
620
621     data_temp_file.write("\n")
622
623     # Days data
624     #check_static_char_array_length("days", days_data.data)
625     data_temp_file.write("static const ushort days_data[] = {\n")
626     data_temp_file.write(wrap_list(days_data.data))
627     data_temp_file.write("\n};\n")
628
629     data_temp_file.write("\n")
630
631     # AM data
632     #check_static_char_array_length("am", am_data.data)
633     data_temp_file.write("static const ushort am_data[] = {\n")
634     data_temp_file.write(wrap_list(am_data.data))
635     data_temp_file.write("\n};\n")
636
637     data_temp_file.write("\n")
638
639     # PM data
640     #check_static_char_array_length("pm", am_data.data)
641     data_temp_file.write("static const ushort pm_data[] = {\n")
642     data_temp_file.write(wrap_list(pm_data.data))
643     data_temp_file.write("\n};\n")
644
645     data_temp_file.write("\n")
646
647     # Currency symbol data
648     #check_static_char_array_length("currency_symbol", currency_symbol_data.data)
649     data_temp_file.write("static const ushort currency_symbol_data[] = {\n")
650     data_temp_file.write(wrap_list(currency_symbol_data.data))
651     data_temp_file.write("\n};\n")
652
653     data_temp_file.write("\n")
654
655     # Currency display name data
656     #check_static_char_array_length("currency_display_name", currency_display_name_data.data)
657     data_temp_file.write("static const ushort currency_display_name_data[] = {\n")
658     data_temp_file.write(wrap_list(currency_display_name_data.data))
659     data_temp_file.write("\n};\n")
660
661     data_temp_file.write("\n")
662
663     # Currency format data
664     #check_static_char_array_length("currency_format", currency_format_data.data)
665     data_temp_file.write("static const ushort currency_format_data[] = {\n")
666     data_temp_file.write(wrap_list(currency_format_data.data))
667     data_temp_file.write("\n};\n")
668
669     # Endonyms data
670     #check_static_char_array_length("endonyms", endonyms_data.data)
671     data_temp_file.write("static const ushort endonyms_data[] = {\n")
672     data_temp_file.write(wrap_list(endonyms_data.data))
673     data_temp_file.write("\n};\n")
674
675     data_temp_file.write("\n")
676
677     # Language name list
678     data_temp_file.write("static const char language_name_list[] =\n")
679     data_temp_file.write("\"Default\\0\"\n")
680     for key in language_map.keys():
681         if key == 0:
682             continue
683         data_temp_file.write("\"" + language_map[key][0] + "\\0\"\n")
684     data_temp_file.write(";\n")
685
686     data_temp_file.write("\n")
687
688     # Language name index
689     data_temp_file.write("static const quint16 language_name_index[] = {\n")
690     data_temp_file.write("     0, // AnyLanguage\n")
691     index = 8
692     for key in language_map.keys():
693         if key == 0:
694             continue
695         language = language_map[key][0]
696         data_temp_file.write("%6d, // %s\n" % (index, language))
697         index += len(language) + 1
698     data_temp_file.write("};\n")
699
700     data_temp_file.write("\n")
701
702     # Script name list
703     data_temp_file.write("static const char script_name_list[] =\n")
704     data_temp_file.write("\"Default\\0\"\n")
705     for key in script_map.keys():
706         if key == 0:
707             continue
708         data_temp_file.write("\"" + script_map[key][0] + "\\0\"\n")
709     data_temp_file.write(";\n")
710
711     data_temp_file.write("\n")
712
713     # Script name index
714     data_temp_file.write("static const quint16 script_name_index[] = {\n")
715     data_temp_file.write("     0, // AnyScript\n")
716     index = 8
717     for key in script_map.keys():
718         if key == 0:
719             continue
720         script = script_map[key][0]
721         data_temp_file.write("%6d, // %s\n" % (index, script))
722         index += len(script) + 1
723     data_temp_file.write("};\n")
724
725     data_temp_file.write("\n")
726
727     # Country name list
728     data_temp_file.write("static const char country_name_list[] =\n")
729     data_temp_file.write("\"Default\\0\"\n")
730     for key in country_map.keys():
731         if key == 0:
732             continue
733         data_temp_file.write("\"" + country_map[key][0] + "\\0\"\n")
734     data_temp_file.write(";\n")
735
736     data_temp_file.write("\n")
737
738     # Country name index
739     data_temp_file.write("static const quint16 country_name_index[] = {\n")
740     data_temp_file.write("     0, // AnyCountry\n")
741     index = 8
742     for key in country_map.keys():
743         if key == 0:
744             continue
745         country = country_map[key][0]
746         data_temp_file.write("%6d, // %s\n" % (index, country))
747         index += len(country) + 1
748     data_temp_file.write("};\n")
749
750     data_temp_file.write("\n")
751
752     # Language code list
753     data_temp_file.write("static const unsigned char language_code_list[] =\n")
754     for key in language_map.keys():
755         code = language_map[key][1]
756         if len(code) == 2:
757             code += r"\0"
758         data_temp_file.write("\"%2s\" // %s\n" % (code, language_map[key][0]))
759     data_temp_file.write(";\n")
760
761     data_temp_file.write("\n")
762
763     # Script code list
764     data_temp_file.write("static const unsigned char script_code_list[] =\n")
765     for key in script_map.keys():
766         code = script_map[key][1]
767         for i in range(4 - len(code)):
768             code += "\\0"
769         data_temp_file.write("\"%2s\" // %s\n" % (code, script_map[key][0]))
770     data_temp_file.write(";\n")
771
772     # Country code list
773     data_temp_file.write("static const unsigned char country_code_list[] =\n")
774     for key in country_map.keys():
775         code = country_map[key][1]
776         if len(code) == 2:
777             code += "\\0"
778         data_temp_file.write("\"%2s\" // %s\n" % (code, country_map[key][0]))
779     data_temp_file.write(";\n")
780
781     data_temp_file.write("\n")
782     data_temp_file.write(GENERATED_BLOCK_END)
783     s = qlocaledata_file.readline()
784     # skip until end of the block
785     while s and s != GENERATED_BLOCK_END:
786         s = qlocaledata_file.readline()
787
788     s = qlocaledata_file.readline()
789     while s:
790         data_temp_file.write(s)
791         s = qlocaledata_file.readline()
792     data_temp_file.close()
793     qlocaledata_file.close()
794
795     os.rename(data_temp_file_path, qtsrcdir + "/src/corelib/tools/qlocale_data_p.h")
796
797     # qlocale.h
798
799     (qlocaleh_temp_file, qlocaleh_temp_file_path) = tempfile.mkstemp("qlocale.h", dir=qtsrcdir)
800     qlocaleh_temp_file = os.fdopen(qlocaleh_temp_file, "w")
801     qlocaleh_file = open(qtsrcdir + "/src/corelib/tools/qlocale.h", "r")
802     s = qlocaleh_file.readline()
803     while s and s != GENERATED_BLOCK_START:
804         qlocaleh_temp_file.write(s)
805         s = qlocaleh_file.readline()
806     qlocaleh_temp_file.write(GENERATED_BLOCK_START)
807     qlocaleh_temp_file.write("// see qlocale_data_p.h for more info on generated data\n")
808
809     # Language enum
810     qlocaleh_temp_file.write("    enum Language {\n")
811     language = ""
812     for key in language_map.keys():
813         language = fixedLanguageName(language_map[key][0], dupes)
814         qlocaleh_temp_file.write("        " + language + " = " + str(key) + ",\n")
815     # special cases for norwegian. we really need to make it right at some point.
816     qlocaleh_temp_file.write("        NorwegianBokmal = Norwegian,\n")
817     qlocaleh_temp_file.write("        NorwegianNynorsk = Nynorsk,\n")
818     qlocaleh_temp_file.write("        LastLanguage = " + language + "\n")
819     qlocaleh_temp_file.write("    };\n")
820
821     qlocaleh_temp_file.write("\n")
822
823     # Script enum
824     qlocaleh_temp_file.write("    enum Script {\n")
825     script = ""
826     for key in script_map.keys():
827         script = fixedScriptName(script_map[key][0], dupes)
828         qlocaleh_temp_file.write("        " + script + " = " + str(key) + ",\n")
829     qlocaleh_temp_file.write("        SimplifiedChineseScript = SimplifiedHanScript,\n")
830     qlocaleh_temp_file.write("        TraditionalChineseScript = TraditionalHanScript,\n")
831     qlocaleh_temp_file.write("        LastScript = " + script + "\n")
832     qlocaleh_temp_file.write("    };\n")
833
834     # Country enum
835     qlocaleh_temp_file.write("    enum Country {\n")
836     country = ""
837     for key in country_map.keys():
838         country = fixedCountryName(country_map[key][0], dupes)
839         qlocaleh_temp_file.write("        " + country + " = " + str(key) + ",\n")
840     qlocaleh_temp_file.write("        LastCountry = " + country + "\n")
841     qlocaleh_temp_file.write("    };\n")
842
843     qlocaleh_temp_file.write(GENERATED_BLOCK_END)
844     s = qlocaleh_file.readline()
845     # skip until end of the block
846     while s and s != GENERATED_BLOCK_END:
847         s = qlocaleh_file.readline()
848
849     s = qlocaleh_file.readline()
850     while s:
851         qlocaleh_temp_file.write(s)
852         s = qlocaleh_file.readline()
853     qlocaleh_temp_file.close()
854     qlocaleh_file.close()
855
856     os.rename(qlocaleh_temp_file_path, qtsrcdir + "/src/corelib/tools/qlocale.h")
857
858     # qlocale.qdoc
859
860     (qlocaleqdoc_temp_file, qlocaleqdoc_temp_file_path) = tempfile.mkstemp("qlocale.qdoc", dir=qtsrcdir)
861     qlocaleqdoc_temp_file = os.fdopen(qlocaleqdoc_temp_file, "w")
862     qlocaleqdoc_file = open(qtsrcdir + "/src/corelib/tools/qlocale.qdoc", "r")
863     s = qlocaleqdoc_file.readline()
864     DOCSTRING="    QLocale's data is based on Common Locale Data Repository "
865     while s:
866         if DOCSTRING in s:
867             qlocaleqdoc_temp_file.write(DOCSTRING + "v" + cldr_version + ".\n")
868         else:
869             qlocaleqdoc_temp_file.write(s)
870         s = qlocaleqdoc_file.readline()
871     qlocaleqdoc_temp_file.close()
872     qlocaleqdoc_file.close()
873
874     os.rename(qlocaleqdoc_temp_file_path, qtsrcdir + "/src/corelib/tools/qlocale.qdoc")
875
876 if __name__ == "__main__":
877     main()