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
30 """convert a properties file back to a .inc file with #defines in it"""
31
32 pendingblanks = []
33 for unit in pf.units:
34 for comment in unit.comments:
35 if comment.startswith("# converted from") and "#defines" in comment:
36 pass
37 else:
38 for blank in pendingblanks:
39 yield blank
40
41 yield comment
42 if unit.isblank():
43 pendingblanks.append("\n")
44 else:
45 definition = "#define %s %s\n" % (unit.name, unit.value.replace("\n", "\\n"))
46 if isinstance(definition, unicode):
47 definition = definition.encode("UTF-8")
48 for blank in pendingblanks:
49 yield blank
50 yield definition
51
53 """convert a properties file back to a pseudo-properties .it file"""
54 for unit in pf.units:
55 for comment in unit.comments:
56 if comment.startswith("# converted from") and "pseudo-properties" in comment:
57 pass
58 elif comment.startswith("# section: "):
59 yield comment.replace("# section: ", "", 1)
60 else:
61 yield comment.replace("#", ";", 1)
62 if unit.isblank():
63 yield "\n"
64 else:
65 definition = "%s=%s\n" % (unit.name, unit.value)
66 if isinstance(definition, unicode):
67 definition = definition.encode("UTF-8")
68 yield definition
69
71 lines = src.split("\n")
72 header = lines[0]
73 if not header.startswith("# converted from "):
74 waspseudoprops = len([line for line in lines if line.startswith("# section:")])
75 wasdefines = len([line for line in lines if line.startswith("#filter") or line.startswith("#unfilter")])
76 else:
77 waspseudoprops = "pseudo-properties" in header
78 wasdefines = "#defines" in header
79 lines = lines[1:]
80 if not (waspseudoprops ^ wasdefines):
81 raise ValueError("could not determine file type as pseudo-properties or defines file")
82 pf = properties.propfile()
83 pf.parse("\n".join(lines))
84 if wasdefines:
85 for line in prop2inc(pf):
86 yield line + "\n"
87 elif waspseudoprops:
88 for line in prop2it(pf):
89 yield line.decode("utf-8").encode(itencoding) + "\n"
90
91 -def po2inc(inputfile, outputfile, templatefile, encoding=None, includefuzzy=False):
107
108 -def po2it(inputfile, outputfile, templatefile, encoding="cp1252", includefuzzy=False):
126
127 -def po2ini(inputfile, outputfile, templatefile, encoding="UTF-8", includefuzzy=False):
128 """wraps po2prop but converts outputfile to properties first using UTF-8 encoding"""
129 return po2it(inputfile=inputfile, outputfile=outputfile, templatefile=templatefile, encoding=encoding, includefuzzy=includefuzzy)
130
131 -def main(argv=None):
132 import sys
133
134 src = sys.stdin.read()
135 for line in prop2funny(src):
136 sys.stdout.write(line)
137
138 if __name__ == "__main__":
139 main()
140