fix [#29120] project painting error
[blender.git] / po / update_pot.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 subprocess
26 import os
27 from codecs import open
28
29 GETTEXT_XGETTEXT_EXECUTABLE = "xgettext"
30 CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
31 SOURCE_DIR = os.path.normpath(os.path.abspath(os.path.join(CURRENT_DIR, "..")))
32 DOMAIN = "blender"
33 COMMENT_PREFIX = "#~ "  # from update_msg.py
34
35 FILE_NAME_POT = os.path.join(CURRENT_DIR, "blender.pot")
36 FILE_NAME_MESSAGES = os.path.join(CURRENT_DIR, "messages.txt")
37
38
39 def main():
40     cmd = (GETTEXT_XGETTEXT_EXECUTABLE,
41            "--files-from=%s" % os.path.join(SOURCE_DIR, "po", "POTFILES.in"),
42            "--keyword=_",
43            "--keyword=N_",
44            "--directory=%s" % SOURCE_DIR,
45            "--output=%s" % os.path.join(SOURCE_DIR, "po", "%s.pot" % DOMAIN),
46            "--from-code=utf-8",
47            )
48
49     print(" ".join(cmd))
50     process = subprocess.Popen(cmd)
51     process.wait()
52
53     def stripeol(s):
54         return s.rstrip("\n\r")
55
56     pot_messages = {}
57     reading_message = False
58     message = ""
59     with open(FILE_NAME_POT, 'r', "utf-8") as handle:
60         while True:
61             line = handle.readline()
62
63             if not line:
64                 break
65
66             line = stripeol(line)
67             if line.startswith("msgid"):
68                 reading_message = True
69                 message = line[7:-1]
70             elif line.startswith("msgstr"):
71                 reading_message = False
72                 pot_messages[message] = True
73             elif reading_message:
74                 message += line[1:-1]
75
76     # add messages collected automatically from RNA
77     with open(FILE_NAME_POT, "a", "utf-8") as pot_handle:
78         with open(FILE_NAME_MESSAGES, 'r', "utf-8") as handle:
79             msgsrc_ls = []
80             while True:
81                 line = handle.readline()
82
83                 if not line:
84                     break
85
86                 line = stripeol(line)
87
88                 # COMMENT_PREFIX
89                 if line.startswith(COMMENT_PREFIX):
90                     msgsrc_ls.append(line[len(COMMENT_PREFIX):].strip())
91                 else:
92                     line = line.replace("\\", "\\\\")
93                     line = line.replace("\"", "\\\"")
94                     line = line.replace("\t", "\\t")
95
96                     if not pot_messages.get(line):
97                         for msgsrc in msgsrc_ls:
98                             pot_handle.write("#: %s\n" % msgsrc)
99                         pot_handle.write("msgid \"%s\"\n" % line)
100                         pot_handle.write("msgstr \"\"\n\n")
101                     msgsrc_ls[:] = []
102
103
104 if __name__ == "__main__":
105     print("\n\n *** Running %r *** \n" % __file__)
106     main()