1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18   
 19   
 20   
 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   
 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   
 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               
 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       
 73       
 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   
 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