Package translate :: Package convert :: Module mozfunny2prop
[hide private]
[frames] | no frames]

Source Code for Module translate.convert.mozfunny2prop

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  #  
  4  # Copyright 2005, 2006 Zuza Software Foundation 
  5  #  
  6  # This file is part of translate. 
  7  # 
  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. 
 12  #  
 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. 
 17  # 
 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 
 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   
29 -def encodepropline(line):
30 """helper which strips off any end of line, encodes for properties file, and adds on the end of line""" 31 strippedline = line.rstrip("\n") 32 if line == strippedline: 33 ending = "" 34 else: 35 ending = line[len(strippedline)-len(line):] 36 return quote.mozillapropertiesencode(strippedline) + ending
37
38 -def inc2prop(lines):
39 """convert a .inc file with #defines in it to a properties file""" 40 yield "# converted from #defines file\n" 41 for line in lines: 42 line = line.decode("utf-8") 43 if line.startswith("# "): 44 commented = True 45 line = line.replace("# ", "", 1) 46 else: 47 commented = False 48 if not line.strip(): 49 yield line 50 elif line.startswith("#define"): 51 parts = string.split(line.replace("#define", "", 1).strip(), maxsplit=1) 52 if not parts: 53 continue 54 if len(parts) == 1: 55 key, value = parts[0], "" 56 else: 57 key, value = parts 58 # special case: uncomment MOZ_LANGPACK_CONTRIBUTORS 59 if key == "MOZ_LANGPACK_CONTRIBUTORS": 60 commented = False 61 if commented: 62 yield "# " 63 yield "%s = %s\n" % (key, value) 64 else: 65 if commented: 66 yield "# " 67 yield line
68
69 -def it2prop(lines, encoding="cp1252"):
70 """convert a pseudo-properties .it file to a conventional properties file""" 71 yield "# converted from pseudo-properties .it file\n" 72 # differences: ; instead of # for comments 73 # [section] titles that we replace with # section: comments 74 for line in lines: 75 line = line.decode(encoding) 76 if not line.strip(): 77 yield line 78 elif line.lstrip().startswith(";"): 79 yield line.replace(";", "#", 1) 80 elif line.lstrip().startswith("[") and line.rstrip().endswith("]"): 81 yield "# section: "+line 82 else: 83 yield line
84
85 -def funny2prop(lines, itencoding="cp1252"):
86 hashstarts = len([line for line in lines if line.startswith("#")]) 87 if hashstarts: 88 for line in inc2prop(lines): 89 yield encodepropline(line) 90 else: 91 for line in it2prop(lines, encoding=itencoding): 92 yield encodepropline(line)
93
94 -def inc2po(inputfile, outputfile, templatefile, encoding=None, pot=False, duplicatestyle="msgid_comment"):
95 """wraps prop2po but converts input/template files to properties first""" 96 inputlines = inputfile.readlines() 97 inputproplines = [encodepropline(line) for line in inc2prop(inputlines)] 98 inputpropfile = StringIO("".join(inputproplines)) 99 if templatefile is not None: 100 templatelines = templatefile.readlines() 101 templateproplines = [encodepropline(line) for line in inc2prop(templatelines)] 102 templatepropfile = StringIO("".join(templateproplines)) 103 else: 104 templatepropfile = None 105 return prop2po.convertprop(inputpropfile, outputfile, templatepropfile, pot=pot, duplicatestyle=duplicatestyle)
106
107 -def it2po(inputfile, outputfile, templatefile, encoding="cp1252", pot=False, duplicatestyle="msgid_comment"):
108 """wraps prop2po but converts input/template files to properties first""" 109 inputlines = inputfile.readlines() 110 inputproplines = [encodepropline(line) for line in it2prop(inputlines, encoding=encoding)] 111 inputpropfile = StringIO("".join(inputproplines)) 112 if templatefile is not None: 113 templatelines = templatefile.readlines() 114 templateproplines = [encodepropline(line) for line in it2prop(templatelines, encoding=encoding)] 115 templatepropfile = StringIO("".join(templateproplines)) 116 else: 117 templatepropfile = None 118 return prop2po.convertprop(inputpropfile, outputfile, templatepropfile, pot=pot, duplicatestyle=duplicatestyle)
119
120 -def ini2po(inputfile, outputfile, templatefile, encoding="UTF-8", pot=False, duplicatestyle="msgid_comment"):
121 return it2po(inputfile=inputfile, outputfile=outputfile, templatefile=templatefile, encoding=encoding, pot=pot, duplicatestyle=duplicatestyle)
122
123 -def main(argv=None):
124 import sys 125 lines = sys.stdin.readlines() 126 for line in funny2prop(lines): 127 sys.stdout.write(line)
128 129 if __name__ == "__main__": 130 main() 131