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