translate.convert.mozfunny2prop

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