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