1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """converts properties files back to funny mozilla files"""
23
24 from translate.storage import properties
25 from translate.convert import po2prop
26 from translate.convert import mozfunny2prop
27 from translate.misc.wStringIO import StringIO
28
29
31 """convert a properties file back to a .inc file with #defines in it"""
32
33 pendingblanks = []
34 for unit in pf.units:
35 for comment in unit.comments:
36 if comment.startswith("# converted from") and "#defines" in comment:
37 pass
38 else:
39 for blank in pendingblanks:
40 yield blank
41
42 yield comment
43 if unit.isblank():
44 pendingblanks.append("\n")
45 else:
46 definition = "#define %s %s\n" % (unit.name, unit.value.replace("\n", "\\n"))
47 if isinstance(definition, unicode):
48 definition = definition.encode("UTF-8")
49 for blank in pendingblanks:
50 yield blank
51 yield definition
52
53
55 """convert a properties file back to a pseudo-properties .it file"""
56 for unit in pf.units:
57 for comment in unit.comments:
58 if comment.startswith("# converted from") and "pseudo-properties" in comment:
59 pass
60 elif comment.startswith("# section: "):
61 yield comment.replace("# section: ", "", 1) + "\n"
62 else:
63 yield comment.replace("#", ";", 1) + "\n"
64 if unit.isblank():
65 yield ""
66 else:
67 definition = "%s=%s\n" % (unit.name, unit.value)
68 if isinstance(definition, unicode):
69 definition = definition.encode("UTF-8")
70 yield definition
71
72
74 lines = src.split("\n")
75 header = lines[0]
76 if not header.startswith("# converted from "):
77 waspseudoprops = len([line for line in lines if line.startswith("# section:")])
78 wasdefines = len([line for line in lines if line.startswith("#filter") or line.startswith("#unfilter")])
79 else:
80 waspseudoprops = "pseudo-properties" in header
81 wasdefines = "#defines" in header
82 lines = lines[1:]
83 if not (waspseudoprops ^ wasdefines):
84 raise ValueError("could not determine file type as pseudo-properties or defines file")
85 pf = properties.propfile(personality="mozilla")
86 pf.parse("\n".join(lines))
87 if wasdefines:
88 for line in prop2inc(pf):
89 yield line + "\n"
90 elif waspseudoprops:
91 for line in prop2it(pf):
92 yield line.decode("utf-8").encode(itencoding) + "\n"
93
94
95 -def po2inc(inputfile, outputfile, templatefile, encoding=None, includefuzzy=False):
111
112
113 -def po2it(inputfile, outputfile, templatefile, encoding="cp1252", includefuzzy=False):
131
132
133 -def po2ini(inputfile, outputfile, templatefile, encoding="UTF-8", includefuzzy=False):
136
137
138 -def main(argv=None):
139 import sys
140
141 src = sys.stdin.read()
142 for line in prop2funny(src):
143 sys.stdout.write(line)
144
145
146 if __name__ == "__main__":
147 main()
148