comparison htdocs/svg-edit/extras/update-langs.py @ 515:55ffbd41c42d

svg-edit: updated to version r1502
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Mon, 05 Apr 2010 12:54:47 +0200
parents 1ef31d76eb88
children
comparison
equal deleted inserted replaced
512:31e327c450ca 515:55ffbd41c42d
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*- 2 # -*- coding: iso-8859-15 -*-
3 """
4 This is a helper script for the svg-edit project, useful for managing
5 all the language files
3 6
4 # This is a helper script for the svg-edit project, useful for managing 7 Licensed under the Apache 2 License as is the rest of the project
5 # all the language files 8 Requires Python 2.6
6 #
7 # Licensed under the Apache 2 License as is the rest of the project
8 # Requires Python 2.6
9 #
10 # Copyright (c) 2010 Jeff Schiller
11 9
10 Copyright (c) 2010 Jeff Schiller
11 """
12 import os 12 import os
13 import sys
14 import re
15 import json 13 import json
16 from types import * 14 from types import DictType
17 15
18 def updateMainMenu(j): 16 def updateMainMenu(j):
19 """ 17 """
20 Converts title into textContent for items in the main menu 18 Converts title into textContent for items in the main menu
21 """ 19 """
22 tools = ['tool_clear', 'tool_open', 'tool_save', 'tool_docprops'] 20 tools = ['tool_clear', 'tool_open', 'tool_save', 'tool_docprops']
23 for row in j: 21 for row in j:
24 try: 22 try:
25 id = row['id'] 23 ids = row['id']
26 if id in tools: 24 if ids in tools:
27 row[u'textContent'] = row.pop('title') 25 row[u'textContent'] = row.pop('title')
28 except KeyError: 26 except KeyError:
29 pass 27 pass
30 28
31 def ourPrettyPrint(j): 29 def ourPrettyPrint(j):
32 """ 30 """
33 Outputs a string representation of the JSON object passed in 31 Outputs a string representation of the JSON object passed in
34 formatted properly for our lang.XX.js files. 32 formatted properly for our lang.XX.js files.
35 """ 33 """
36 s = '[' + os.linesep 34 s = '[' + os.linesep
37 js_strings = None 35 js_strings = None
38 j.sort() 36 j.sort()
39 for row in j: 37 for row in j:
40 try: 38 try:
41 id = row['id'] 39 ids = row['id']
42 row_string = json.dumps(row, sort_keys=True, ensure_ascii=False) 40 row_string = json.dumps(row, sort_keys=True, ensure_ascii=False)
43 s += row_string + ',' + os.linesep 41 s += row_string + ',' + os.linesep
44 except KeyError: 42 except KeyError:
45 if type(row) is DictType: 43 if type(row) is DictType:
46 js_strings = row 44 js_strings = row
47 45
48 s += json.dumps(js_strings, sort_keys=True, ensure_ascii=False, indent=1) + os.linesep 46 s += json.dumps(js_strings, sort_keys=True, ensure_ascii=False, indent=1) + os.linesep
49 s += ']' 47 s += ']'
50 return s 48 return s
51 49
52 def processFile(filename): 50 def processFile(filename):
53 """ 51 """
54 Loads the given lang.XX.js file, processes it and saves it 52 Loads the given lang.XX.js file, processes it and saves it
55 back to the file system 53 back to the file system
56 """ 54 """
57 in_file = open('./editor/locale/'+filename, 'r') 55 in_string = open('./editor/locale/' + filename, 'r').read()
58 in_string = in_file.read()
59 in_file.close()
60
61 try:
62 j = json.loads(in_string)
63
64 # process the JSON object here
65 # updateMainMenu(j)
66
67 # now write it out back to the file
68 s = ourPrettyPrint(j).encode("UTF-8");
69 out_file = open('./editor/locale/'+filename, 'w')
70 out_file.write( s )
71 out_file.close()
72 56
73 print "Updated " + filename 57 try:
74 except ValueError: 58 j = json.loads(in_string)
75 print "ERROR! " + filename + " was not valid JSON, please fix it!" 59
60 # process the JSON object here
61 # updateMainMenu(j)
62
63 # now write it out back to the file
64 s = ourPrettyPrint(j).encode("UTF-8")
65 open('./editor/locale/' + filename, 'w').write(s)
66
67 print "Updated " + filename
68 except ValueError:
69 print "ERROR! " + filename + " was not valid JSON, please fix it!"
76 70
77 if __name__ == '__main__': 71 if __name__ == '__main__':
78 # get list of all lang files and process them 72 # get list of all lang files and process them
79 for file in os.listdir('./editor/locale/'): 73 for file_name in os.listdir('./editor/locale/'):
80 if file[:4] == "lang": 74 if file_name[:4] == "lang":
81 processFile(file) 75 processFile(file_name)