add more spacing
[personal-kdebase.git] / runtime / kcontrol / locale / scoop-tzones.py
blob5bb125669e665db43549a785f820332a3daeb05a
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
4 # Takes given zone.tab file, or looks for expected locations on the system,
5 # and uses it to update dummy TIMEZONES file for i18n message extraction.
7 # Timezones read from zone.tab are only added into TIMEZONES,
8 # no timezone (or timezone comment) is removed from TIMEZONES.
9 # This in order not to lose i18n for timezones and comments
10 # found on various newer and older systems.
12 # Chusslove Illich (Часлав Илић) <caslav.ilic@gmx.net>
14 import sys
15 import os
16 import codecs
17 import re
19 cmdname = os.path.basename(sys.argv[0])
20 def error (msg):
21 print >>sys.stderr, "%s: %s" % (cmdname, msg)
22 sys.exit(1)
24 system_zonetab_paths = [
25 "/usr/share/zoneinfo/zone.tab",
28 if len(sys.argv) not in (2, 3):
29 print >>sys.stderr, "usage: %s TIMEZONES_PATH [ZONETAB_PATH]" % cmdname
30 sys.exit(1)
32 timezone_path = sys.argv[1]
33 if not os.path.isfile(timezone_path):
34 error("TIMEZONES file '%s' does not exist "
35 "(create manually an empty file if really starting from scratch")
37 if len(sys.argv) >= 3:
38 zonetab_path = sys.argv[2]
39 if not os.path.isfile(zonetab_path):
40 error("zone.tab file '%s' does not exist" % zonetab_path)
41 print "using given zone.tab file at '%s'" % zonetab_path
42 else:
43 for system_zonetab_path in system_zonetab_paths:
44 if os.path.isfile(system_zonetab_path):
45 zonetab_path = system_zonetab_path
46 break
47 if not zonetab_path:
48 error("cannot fine zone.tab file at any of known system locations")
49 print "found system zone.tab file at '%s'" % zonetab_path
51 # Parse current timezones into dictionary zone->[comments].
52 ifs = codecs.open(timezone_path, "r", "UTF-8")
53 message_rx = re.compile(r"i18n\(\"(.*?)\"\)")
54 tzcomment_rx = re.compile(r"^// i18n:.*comment")
55 tzcomment_follows = False
56 current_timezones = {}
57 for line in ifs:
58 if tzcomment_rx.search(line):
59 tzcomment_follows = True
60 else:
61 m = message_rx.search(line)
62 if m:
63 message = m.group(1)
64 if not tzcomment_follows:
65 tzone = message
66 if tzone not in current_timezones:
67 current_timezones[tzone] = []
68 else:
69 tzcomment = message
70 if tzcomment not in current_timezones[tzone]:
71 current_timezones[tzone].append(tzcomment)
72 tzcomment_follows = False
73 ifs.close()
75 # Parse system timezones into same form.
76 ifs = codecs.open(zonetab_path, "r", "UTF-8")
77 system_timezones = {}
78 for line in ifs:
79 if line.lstrip().startswith("#"):
80 continue
81 line = line.rstrip("\n")
82 els = line.split("\t")
83 if len(els) >= 3:
84 tzone = els[2]
85 if tzone not in system_timezones:
86 system_timezones[tzone] = []
87 if len(els) >= 4:
88 tzcomment = els[3]
89 if tzcomment not in system_timezones[tzone]:
90 system_timezones[tzone].append(tzcomment)
91 ifs.close()
93 # Compose new timezones by adding new timezones read from the system,
94 # and appending new comments to existing timezones.
95 num_new_tzones = 0
96 num_new_tzcomments = 0
97 for tzone, tzcomments in system_timezones.items():
98 if tzone not in current_timezones:
99 current_timezones[tzone] = tzcomments
100 num_new_tzones += 1
101 else:
102 for tzcomment in tzcomments:
103 if tzcomment not in current_timezones[tzone]:
104 current_timezones[tzone].append(tzcomment)
105 num_new_tzcomments += 1
107 if num_new_tzones or num_new_tzcomments:
108 tzlines = []
109 tzones = current_timezones.keys()
110 tzones.sort()
111 for tzone in tzones:
112 tzlines.append("i18n(\"%s\");\n" % tzone);
113 for tzcomment in current_timezones[tzone]:
114 tzlines.append("// i18n: comment to the previous timezone\n")
115 tzlines.append("i18n(\"%s\");\n" % tzcomment)
116 ofs = codecs.open(timezone_path, "w", "UTF-8")
117 ofs.writelines(tzlines)
118 ofs.close()
119 num_new = num_new_tzones + num_new_tzcomments
120 print "added %d new timezones/comments to '%s'" % (num_new, timezone_path)
121 else:
122 print "no new timezones/comments to add"