/usr/share/pyshared/translate/convert/prop2mozfunny.py is in translate-toolkit 1.10.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2005, 2006 Zuza Software Foundation
#
# This file is part of translate.
#
# translate is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# translate is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
"""Converts properties files to additional Mozilla format files.
"""
from translate.storage import properties
from translate.convert import po2prop
from translate.convert import mozfunny2prop
from translate.misc.wStringIO import StringIO
def prop2inc(pf):
"""convert a properties file back to a .inc file with #defines in it"""
# any leftover blanks will not be included at the end
pendingblanks = []
for unit in pf.units:
for comment in unit.comments:
if comment.startswith("# converted from") and "#defines" in comment:
pass
else:
for blank in pendingblanks:
yield blank
# TODO: could convert commented # x=y back to # #define x y
yield comment + "\n"
if unit.isblank():
pendingblanks.append("\n")
else:
definition = "#define %s %s\n" % (unit.name, unit.value.replace("\n", "\\n"))
if isinstance(definition, unicode):
definition = definition.encode("UTF-8")
for blank in pendingblanks:
yield blank
yield definition
def prop2it(pf):
"""convert a properties file back to a pseudo-properties .it file"""
for unit in pf.units:
for comment in unit.comments:
if comment.startswith("# converted from") and "pseudo-properties" in comment:
pass
elif comment.startswith("# section: "):
yield comment.replace("# section: ", "", 1) + "\n"
else:
yield comment.replace("#", ";", 1) + "\n"
if unit.isblank():
yield ""
else:
definition = "%s=%s\n" % (unit.name, unit.value)
if isinstance(definition, unicode):
definition = definition.encode("UTF-8")
yield definition
def prop2funny(src, itencoding="cp1252"):
lines = src.split("\n")
header = lines[0]
if not header.startswith("# converted from "):
waspseudoprops = len([line for line in lines if line.startswith("# section:")])
wasdefines = len([line for line in lines if line.startswith("#filter") or line.startswith("#unfilter")])
else:
waspseudoprops = "pseudo-properties" in header
wasdefines = "#defines" in header
lines = lines[1:]
if not (waspseudoprops ^ wasdefines):
raise ValueError("could not determine file type as pseudo-properties or defines file")
pf = properties.propfile(personality="mozilla")
pf.parse("\n".join(lines))
if wasdefines:
for line in prop2inc(pf):
yield line + "\n"
elif waspseudoprops:
for line in prop2it(pf):
yield line.decode("utf-8").encode(itencoding) + "\n"
def po2inc(inputfile, outputfile, templatefile, encoding=None, includefuzzy=False):
"""wraps po2prop but converts outputfile to properties first"""
outputpropfile = StringIO()
if templatefile is not None:
templatelines = templatefile.readlines()
templateproplines = [line for line in mozfunny2prop.inc2prop(templatelines)]
templatepropfile = StringIO("".join(templateproplines))
else:
templatepropfile = None
result = po2prop.convertmozillaprop(inputfile, outputpropfile, templatepropfile, includefuzzy=includefuzzy)
if result:
outputpropfile.seek(0)
pf = properties.propfile(outputpropfile, personality="mozilla")
outputlines = prop2inc(pf)
outputfile.writelines(outputlines)
return result
def po2it(inputfile, outputfile, templatefile, encoding="cp1252", includefuzzy=False):
"""wraps po2prop but converts outputfile to properties first"""
outputpropfile = StringIO()
if templatefile is not None:
templatelines = templatefile.readlines()
templateproplines = [line for line in mozfunny2prop.it2prop(templatelines, encoding=encoding)]
templatepropfile = StringIO("".join(templateproplines))
else:
templatepropfile = None
result = po2prop.convertmozillaprop(inputfile, outputpropfile, templatepropfile, includefuzzy=includefuzzy)
if result:
outputpropfile.seek(0)
pf = properties.propfile(outputpropfile, personality="mozilla")
outputlines = prop2it(pf)
for line in outputlines:
line = line.decode("utf-8").encode(encoding)
outputfile.write(line)
return result
def po2ini(inputfile, outputfile, templatefile, encoding="UTF-8", includefuzzy=False):
"""wraps po2prop but converts outputfile to properties first using UTF-8 encoding"""
return po2it(inputfile=inputfile, outputfile=outputfile, templatefile=templatefile, encoding=encoding, includefuzzy=includefuzzy)
def main(argv=None):
import sys
# TODO: get encoding from charset.mk, using parameter
src = sys.stdin.read()
for line in prop2funny(src):
sys.stdout.write(line)
if __name__ == "__main__":
main()
|