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