File: extract_strings.py

package info (click to toggle)
supertux 0.6.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 264,184 kB
  • sloc: cpp: 113,426; ansic: 9,654; sh: 4,483; cs: 1,296; makefile: 407; yacc: 398; python: 382; lisp: 285; objc: 248; csh: 219; lex: 140; perl: 118; xml: 53; ruby: 36
file content (45 lines) | stat: -rwxr-xr-x 1,010 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import sys
import re

pattern = re.compile(r"(translate|_)\((.*?)\)")

lisp_template_begin = """(msgids
""";
lisp_template_end = ")"

lisp_content = lisp_template_begin

if(len(sys.argv) < 3):
	print("Usage: " + sys.argv[0] + " <filename> <output file>")
else:
	filename = sys.argv[1]
	out_file = sys.argv[2]
	strings_found = False
	with open(filename, 'r') as f:
		s = f.read()
	matches = pattern.findall(s)
	if len(matches) > 0:
		strings_found = True

	for match in matches:
		lines = match[1].split("\\n")
		num_lines = len(lines)
		if num_lines == 1:
			lisp_content += "  (msgid "
			lisp_content += "(_ " + match[1] + ")"
			lisp_content += ")\r\n"
		else:
			lisp_content += "  (msgid (_ "
			for i, line in enumerate(lines):
				lisp_content += line
				if i < num_lines:
					if i == num_lines - 1:
						lisp_content += "))"
					lisp_content += "\n"

	lisp_content += lisp_template_end

	if strings_found:
		output_file = open(out_file, "w")
		output_file.write(lisp_content)
		output_file.close()