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