summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.5-sdk/frontend/framework/tool/modules/cldr.py
blob: 2614f080708da1a93a662387de981094768fcf30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python
################################################################################
#
#  qooxdoo - the new era of web development
#
#  http://qooxdoo.org
#
#  Copyright:
#    2006-2007 1&1 Internet AG, Germany, http://www.1and1.org
#
#  License:
#    LGPL: http://www.gnu.org/licenses/lgpl.html
#    EPL: http://www.eclipse.org/org/documents/epl-v10.php
#    See the LICENSE file in the project's top-level directory for details.
#
#  Authors:
#    * Fabian Jakobs (fjakobs)
#
################################################################################
# encoding: utf-8
"""
extract_cldr.py
"""

import os
import sys
import getopt
import ElementTree

help_message = '''
The help message goes here.
'''


class Usage(Exception):
    def __init__(self, msg):
        self.msg = msg


def getJavaScript(data, locale, language, territory="", namespace="qx.locale.data"):
	str = "/*\n"
	if territory != "":
		str += "#load(%s.%s)\n" % (namespace, language)
	str += '''#require(qx.Locale)
*/
qx.Locale.define("%s.%s", {
''' % (namespace, locale)
	lines = []
	keys = data.keys()
	keys.sort()
	for key in keys:
		lines.append('  cldr_%s: "%s"' % (key.replace("-", "_"), data[key].encode("UTF-8").replace("\n", "\n" + 4 * " ").replace('"', '\\"')) )
		
	body = ",\n".join(lines)
	str += "%s\n});" % body
	return str
	

def getLocale(calendarElement):
	locale = calendarElement.find("identity/language").attrib["type"]
	territoryNode = calendarElement.find("identity/territory")
	territory = ""
	if territoryNode != None:
		territory = territoryNode.attrib["type"]
	return (locale, territory)
	

def extractMonth(calendarElement):
	data = {}
	for monthContext in calendarElement.findall(".//monthContext"):
		for monthWidth in monthContext.findall("monthWidth"):
			monthType = monthWidth.attrib["type"]
			for month in monthWidth.findall("month"):
				if month.attrib.has_key("alt"): continue
				data["month_%s_%s" % (monthType, month.attrib["type"])] = month.text
	return data


def extractDay(calendarElement):
	data = {}
	for dayWidth in calendarElement.findall(".//dayWidth"):
		dayType = dayWidth.attrib["type"]
		for day in dayWidth.findall("day"):
			if day.attrib.has_key("alt"): continue
			data['day_%s_%s' % (dayType, day.attrib["type"])] = day.text
	return data
	
def extractQuarter(calendarElement):
	return {'': ''}

	
def extractAmPm(calendarElement):
	data = {}

	amNode = calendarElement.find(".//am")
	if amNode != None:
		data['am'] = amNode.text
		
	pmNode = calendarElement.find(".//pm")
	if pmNode != None:
		data["pm"] = pmNode.text

	return data

	
def extractDateFormat(calendarElement):
	data = {}
	for dateFormatLength in calendarElement.findall(".//dateFormatLength"):
		dateType = dateFormatLength.attrib["type"]
		for dateFormat in dateFormatLength.findall("dateFormat/pattern"):
			if dateFormat.attrib.has_key("alt"): continue
			data['date_format_%s'% dateType] = dateFormat.text
	return data


def extractTimeFormat(calendarElement):
	data = {}
	for timeFormatLength in calendarElement.findall(".//timeFormatLength"):
		timeType = timeFormatLength.attrib["type"]
		for timeFormat in timeFormatLength.findall("timeFormat/pattern"):
			if timeFormat.attrib.has_key("alt"): continue
			data['time_format_%s' % timeType] = timeFormat.text
	return data

		
def extractDateTimeFormat(calendarElement):
	data = {}
	for dateTimeFormat in calendarElement.findall(".//dateFormatItem"):
		data["date_time_format_%s" % dateTimeFormat.attrib["id"]] = dateTimeFormat.text
	return data

	
def extractFields(calendarElement):
	fields = {}
	for field in calendarElement.findall(".//fields/field"):
		if not field.find("displayName"): break
		fields[field.attrib["type"]] = field.find("displayName").text
		
	return fields
	

def extractDelimiter(tree):
	delimiters = {}
	for delimiter in tree.findall("delimiters/*"):
		delimiters[delimiter.tag] = delimiter.text
	
	return delimiters

		
def extractNumber(tree):
	data = {}
	
	decimalSeparatorNode = tree.find("numbers/symbols/decimal")
	if decimalSeparatorNode != None:
		data['number_decimal_separator'] = decimalSeparatorNode.text
	
	groupSeparator = ","
	groupSeparatorNode = tree.find("numbers/symbols/group")
	if groupSeparatorNode != None:
		data['number_group_separator'] = groupSeparatorNode.text
	
	percentFormatNode = tree.find("numbers/percentFormats/percentFormatLength/percentFormat/pattern")
	if percentFormatNode != None:
		data['number_percent_format'] = percentFormatNode.text

	return data

	
def parseCldrFile(filename, outputDirectory=None):
	tree = ElementTree.parse(filename)
	
	language, territory = getLocale(tree)
	data = {}
	
	for cal in tree.findall('dates/calendars/calendar'):
		if not cal.attrib.has_key("type"): continue
		if cal.attrib["type"] != "gregorian": continue
		data.update(extractMonth(cal))
		data.update(extractDay(cal))
		#data.update(extractQuarter(cal))
		data.update(extractAmPm(cal))
		data.update(extractDateFormat(cal))
		data.update(extractTimeFormat(cal))
		data.update(extractDateTimeFormat(cal))
		data.update(extractFields(cal))
		
	data.update(extractDelimiter(tree))
	data.update(extractNumber(tree))

	locale = language
	if territory != "":
		locale += "_" + territory

	code = getJavaScript(data, locale, language, territory)
	if outputDirectory != None:
		outfile = os.path.join(outputDirectory, locale + ".js");
		open(outfile, "w").write(code)
	else:
		print code


def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "ho:v", ["help", "output="])
        except getopt.error, msg:
            raise Usage(msg)
    
        output = None
        for option, value in opts:
            if option == "-v":
                verbose = True
            if option in ("-h", "--help"):
                raise Usage(help_message)
            if option in ("-o", "--output"):
                output = value
			
        for arg in args:
            parseCldrFile(arg, output)
    
    except Usage, err:
        print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
        print >> sys.stderr, "\t for help use --help"
        return 2


if __name__ == "__main__":
    sys.exit(main())