1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """converts funny mozilla files to properties files"""
23
24 import string
25
26 from translate.convert import prop2po
27 from translate.misc.wStringIO import StringIO
28
29
31 """convert a .inc file with #defines in it to a properties file"""
32 yield "# converted from #defines file\n"
33 for line in lines:
34 line = line.decode("utf-8")
35 if line.startswith("# "):
36 commented = True
37 line = line.replace("# ", "", 1)
38 else:
39 commented = False
40 if not line.strip():
41 yield line
42 elif line.startswith("#define"):
43 parts = string.split(line.replace("#define", "", 1).strip(), maxsplit=1)
44 if not parts:
45 continue
46 if len(parts) == 1:
47 key, value = parts[0], ""
48 else:
49 key, value = parts
50
51 if key == "MOZ_LANGPACK_CONTRIBUTORS":
52 commented = False
53 if commented:
54 yield "# "
55 yield "%s = %s\n" % (key, value)
56 else:
57 if commented:
58 yield "# "
59 yield line
60
61
62 -def it2prop(lines, encoding="cp1252"):
63 """convert a pseudo-properties .it file to a conventional properties file"""
64 yield "# converted from pseudo-properties .it file\n"
65
66
67 for line in lines:
68 line = line.decode(encoding)
69 if not line.strip():
70 yield line
71 elif line.lstrip().startswith(";"):
72 yield line.replace(";", "#", 1)
73 elif line.lstrip().startswith("[") and line.rstrip().endswith("]"):
74 yield "# section: " + line
75 else:
76 yield line
77
78
87
88
89 -def inc2po(inputfile, outputfile, templatefile, encoding=None, pot=False, duplicatestyle="msgctxt"):
90 """wraps prop2po but converts input/template files to properties first"""
91 inputlines = inputfile.readlines()
92 inputproplines = [line for line in inc2prop(inputlines)]
93 inputpropfile = StringIO("".join(inputproplines))
94 if templatefile is not None:
95 templatelines = templatefile.readlines()
96 templateproplines = [line for line in inc2prop(templatelines)]
97 templatepropfile = StringIO("".join(templateproplines))
98 else:
99 templatepropfile = None
100 return prop2po.convertprop(inputpropfile, outputfile, templatepropfile, personality="mozilla", pot=pot, duplicatestyle=duplicatestyle)
101
102
103 -def it2po(inputfile, outputfile, templatefile, encoding="cp1252", pot=False, duplicatestyle="msgctxt"):
104 """wraps prop2po but converts input/template files to properties first"""
105 inputlines = inputfile.readlines()
106 inputproplines = [line for line in it2prop(inputlines, encoding=encoding)]
107 inputpropfile = StringIO("".join(inputproplines))
108 if templatefile is not None:
109 templatelines = templatefile.readlines()
110 templateproplines = [line for line in it2prop(templatelines, encoding=encoding)]
111 templatepropfile = StringIO("".join(templateproplines))
112 else:
113 templatepropfile = None
114 return prop2po.convertprop(inputpropfile, outputfile, templatepropfile, personality="mozilla", pot=pot, duplicatestyle=duplicatestyle)
115
116
117 -def ini2po(inputfile, outputfile, templatefile, encoding="UTF-8", pot=False, duplicatestyle="msgctxt"):
118 return it2po(inputfile=inputfile, outputfile=outputfile, templatefile=templatefile, encoding=encoding, pot=pot, duplicatestyle=duplicatestyle)
119
120
121 -def main(argv=None):
122 import sys
123 lines = sys.stdin.readlines()
124 for line in funny2prop(lines):
125 sys.stdout.write(line)
126
127
128 if __name__ == "__main__":
129 main()
130