Cycles: svn merge -r41266:41467 ^/trunk/blender
[blender-staging.git] / po / clean_po.py
1 #!/usr/bin/env python
2
3 # ***** BEGIN GPL LICENSE BLOCK *****
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software Foundation,
17 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #
19 # ***** END GPL LICENSE BLOCK *****
20
21 # <pep8 compliant>
22
23 # update the pot file according the POTFILES.in
24
25 import os
26 import sys
27 import collections
28
29 from codecs import open
30
31 CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
32
33 FILE_NAME_POT = os.path.join(CURRENT_DIR, "blender.pot")
34
35
36 def read_messages(fname):
37     def stripeol(s):
38         return s.rstrip("\n\r")
39
40     last_message = None
41
42     if hasattr(collections, 'OrderedDict'):
43         messages = collections.OrderedDict()
44         commented_messages = collections.OrderedDict()
45     else:
46         messages = {}
47         commented_messages = {}
48
49     reading_message = False
50     reading_translation = False
51     commented = False
52     message = ""
53     translation = ""
54     message_lines = []
55     translation_lines = []
56     comment_lines = []
57     with open(fname, 'r', "utf-8") as handle:
58         while True:
59             line = handle.readline()
60
61             if not line:
62                 break
63
64             line = stripeol(line)
65             if line.startswith("msgid") or line.startswith("#~ msgid"):
66                 if reading_translation:
67                     last_message['translation'] = translation
68                     translation_lines = []
69
70                 reading_message = True
71                 reading_translation = False
72
73                 if line.startswith('#~'):
74                     message = line[10:-1]
75                     commented = True
76                 else:
77                     message = line[7:-1]
78                     commented = False
79
80                 message_lines.append(message)
81             elif line.startswith("msgstr") or line.startswith("#~ msgstr"):
82                 reading_message = False
83                 reading_translation = True
84                 last_message = {'comment_lines': comment_lines,
85                                 'message_lines': message_lines,
86                                 'translation_lines': translation_lines}
87
88                 if commented:
89                     translation = line[11:-1]
90                     commented_messages[message] = last_message
91                 else:
92                     translation = line[8:-1]
93                     messages[message] = last_message
94
95                 message_lines = []
96                 comment_lines = []
97                 translation_lines.append(translation)
98             elif not line.startswith('"') and not line.startswith('#~ "'):
99                 if reading_translation:
100                     last_message['translation'] = translation
101                 else:
102                     comment_lines.append(line)
103
104                 reading_message = False
105                 reading_translation = False
106                 message_lines = []
107                 translation_lines = []
108             elif reading_message:
109                 if line.startswith('#~ "'):
110                     m = line[4:-1]
111                 else:
112                     m = line[1:-1]
113
114                 message += m
115                 message_lines.append(m)
116             elif reading_translation:
117                 if line.startswith('#~ "'):
118                     t = line[4:-1]
119                 else:
120                     t = line[1:-1]
121
122                 translation += t
123                 translation_lines.append(t)
124
125     return (messages, commented_messages)
126
127
128 def do_clean(po, pot_messages):
129     po_messages, commented_messages = read_messages(po)
130
131     for msgid in commented_messages:
132         if pot_messages.get(msgid):
133             t = po_messages.get(msgid)
134             if not t:
135                 print("Reusing full item from commented "
136                        "lines for msgid '%s'" % msgid)
137                 po_messages[msgid] = commented_messages[msgid]
138             elif not t['translation']:
139                 print("Reusing translation from commented "
140                        "lines for msgid '%s'" % msgid)
141                 m = commented_messages[msgid]
142                 t['translation'] = m['translation']
143                 t['translation_lines'] = m['translation_lines']
144
145     with open(po, 'w', 'utf-8') as handle:
146         for msgid in po_messages:
147             item = po_messages[msgid]
148
149             for x in item['comment_lines']:
150                 handle.write(x + "\n")
151
152             first = True
153             for x in item['message_lines']:
154                 if first:
155                     handle.write("msgid \"%s\"\n" % x)
156                 else:
157                     handle.write("\"%s\"\n" % x)
158                 first = False
159
160             first = True
161             for x in item['translation_lines']:
162                 if first:
163                     handle.write("msgstr \"%s\"\n" % x)
164                 else:
165                     handle.write("\"%s\"\n" % x)
166                 first = False
167
168             handle.write("\n")
169
170
171 def main():
172     pot_messages, commented_messages = read_messages(FILE_NAME_POT)
173
174     if len(sys.argv) > 1:
175         for lang in sys.argv[1:]:
176             po = os.path.join(CURRENT_DIR, lang + '.po')
177
178             if os.path.exists(po):
179                 do_clean(po, pot_messages)
180     else:
181         for po in os.listdir(CURRENT_DIR):
182             if po.endswith('.po'):
183                 print('Processing %s...' % (po))
184                 do_clean(po, pot_messages)
185
186
187 if __name__ == "__main__":
188     print("\n\n *** Running %r *** \n" % __file__)
189     main()