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