remove $Id: tags after discussion on the mailign list: http://markmail.org/message...
[blender-staging.git] / po / merge_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 sys
26 import collections
27
28 from codecs import open
29
30
31 def read_messages(fname):
32     def stripeol(s):
33         return s.rstrip("\n\r")
34
35     last_message = None
36
37     if hasattr(collections, 'OrderedDict'):
38         messages = collections.OrderedDict()
39         commented_messages = collections.OrderedDict()
40     else:
41         messages = {}
42         commented_messages = {}
43
44     reading_message = False
45     reading_translation = False
46     commented = False
47     message = ""
48     translation = ""
49     message_lines = []
50     translation_lines = []
51     comment_lines = []
52     with open(fname, 'r', "utf-8") as handle:
53         while True:
54             line = handle.readline()
55
56             if not line:
57                 break
58
59             line = stripeol(line)
60             if line.startswith("msgid") or line.startswith("#~ msgid"):
61                 if reading_translation:
62                     last_message['translation'] = translation
63                     translation_lines = []
64
65                 reading_message = True
66                 reading_translation = False
67
68                 if line.startswith('#~'):
69                     message = line[10:-1]
70                     commented = True
71                 else:
72                     message = line[7:-1]
73                     commented = False
74
75                 message_lines.append(message)
76             elif line.startswith("msgstr") or line.startswith("#~ msgstr"):
77                 reading_message = False
78                 reading_translation = True
79                 last_message = {'comment_lines': comment_lines,
80                                 'message_lines': message_lines,
81                                 'translation_lines': translation_lines}
82
83                 if commented:
84                     translation = line[11:-1]
85                     commented_messages[message] = last_message
86                 else:
87                     translation = line[8:-1]
88                     messages[message] = last_message
89
90                 message_lines = []
91                 comment_lines = []
92                 translation_lines.append(translation)
93             elif not line.startswith('"') and not line.startswith('#~ "'):
94                 if reading_translation:
95                     last_message['translation'] = translation
96                 else:
97                     comment_lines.append(line)
98
99                 reading_message = False
100                 reading_translation = False
101                 message_lines = []
102                 translation_lines = []
103             elif reading_message:
104                 if line.startswith('#~ "'):
105                     m = line[4:-1]
106                 else:
107                     m = line[1:-1]
108
109                 message += m
110                 message_lines.append(m)
111             elif reading_translation:
112                 if line.startswith('#~ "'):
113                     t = line[4:-1]
114                 else:
115                     t = line[1:-1]
116
117                 translation += t
118                 translation_lines.append(t)
119
120     return (messages, commented_messages)
121
122
123 def main():
124     if len(sys.argv) == 3:
125         dst_messages, tmp = read_messages(sys.argv[1])
126         from_messages, tmp = read_messages(sys.argv[2])
127
128         for msgid in dst_messages:
129             msg = dst_messages.get(msgid)
130             from_msg = from_messages.get(msgid)
131
132             if from_msg and from_msg['translation']:
133                 msg['translation'] = from_msg['translation']
134                 msg['translation_lines'] = from_msg['translation_lines']
135
136         with open(sys.argv[1], 'w', 'utf-8') as handle:
137             for msgid in dst_messages:
138                 item = dst_messages[msgid]
139
140                 for x in item['comment_lines']:
141                     handle.write(x + "\n")
142
143                 first = True
144                 for x in item['message_lines']:
145                     if first:
146                         handle.write("msgid \"%s\"\n" % x)
147                     else:
148                         handle.write("\"%s\"\n" % x)
149                     first = False
150
151                 first = True
152                 for x in item['translation_lines']:
153                     if first:
154                         handle.write("msgstr \"%s\"\n" % x)
155                     else:
156                         handle.write("\"%s\"\n" % x)
157                     first = False
158
159                 handle.write("\n")
160     else:
161         print('Usage: %s <destination-po> <source-po>' % (sys.argv[0]))
162
163
164 if __name__ == "__main__":
165     print("\n\n *** Running %r *** \n" % __file__)
166     main()