2 # -*- coding: utf-8 -*-
4 # Copyright 2005, 2006 Zuza Software Foundation
6 # This file is part of translate.
8 # translate is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # translate is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with translate; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 """converts properties files back to funny mozilla files"""
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
30 """convert a properties file back to a .inc file with #defines in it"""
31 # any leftover blanks will not be included at the end
34 for comment
in unit
.comments
:
35 if comment
.startswith("# converted from") and "#defines" in comment
:
38 for blank
in pendingblanks
:
40 # TODO: could convert commented # x=y back to # #define x y
43 pendingblanks
.append("\n")
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
:
53 """convert a properties file back to a pseudo-properties .it file"""
55 for comment
in unit
.comments
:
56 if comment
.startswith("# converted from") and "pseudo-properties" in comment
:
58 elif comment
.startswith("# section: "):
59 yield comment
.replace("# section: ", "", 1)
61 yield comment
.replace("#", ";", 1)
65 definition
= "%s=%s\n" % (unit
.name
, unit
.value
)
66 if isinstance(definition
, unicode):
67 definition
= definition
.encode("UTF-8")
70 def prop2funny(src
, itencoding
="cp1252"):
71 lines
= src
.split("\n")
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")])
77 waspseudoprops
= "pseudo-properties" in header
78 wasdefines
= "#defines" in header
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
))
85 for line
in prop2inc(pf
):
88 for line
in prop2it(pf
):
89 yield line
.decode("utf-8").encode(itencoding
) + "\n"
91 def po2inc(inputfile
, outputfile
, templatefile
, encoding
=None, includefuzzy
=False):
92 """wraps po2prop but converts outputfile to properties first"""
93 outputpropfile
= StringIO()
94 if templatefile
is not None:
95 templatelines
= templatefile
.readlines()
96 templateproplines
= [mozfunny2prop
.encodepropline(line
) for line
in mozfunny2prop
.inc2prop(templatelines
)]
97 templatepropfile
= StringIO("".join(templateproplines
))
99 templatepropfile
= None
100 result
= po2prop
.convertmozillaprop(inputfile
, outputpropfile
, templatepropfile
, includefuzzy
=includefuzzy
)
102 outputpropfile
.seek(0)
103 pf
= properties
.propfile(outputpropfile
)
104 outputlines
= prop2inc(pf
)
105 outputfile
.writelines(outputlines
)
108 def po2it(inputfile
, outputfile
, templatefile
, encoding
="cp1252", includefuzzy
=False):
109 """wraps po2prop but converts outputfile to properties first"""
110 outputpropfile
= StringIO()
111 if templatefile
is not None:
112 templatelines
= templatefile
.readlines()
113 templateproplines
= [mozfunny2prop
.encodepropline(line
) for line
in mozfunny2prop
.it2prop(templatelines
, encoding
=encoding
)]
114 templatepropfile
= StringIO("".join(templateproplines
))
116 templatepropfile
= None
117 result
= po2prop
.convertmozillaprop(inputfile
, outputpropfile
, templatepropfile
, includefuzzy
=includefuzzy
)
119 outputpropfile
.seek(0)
120 pf
= properties
.propfile(outputpropfile
)
121 outputlines
= prop2it(pf
)
122 for line
in outputlines
:
123 line
= line
.decode("utf-8").encode(encoding
)
124 outputfile
.write(line
)
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
)
133 # TODO: get encoding from charset.mk, using parameter
134 src
= sys
.stdin
.read()
135 for line
in prop2funny(src
):
136 sys
.stdout
.write(line
)
138 if __name__
== "__main__":