Package translate :: Package convert :: Module prop2mozfunny

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.prop2mozfunny

      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 properties files back to funny mozilla files"""
     23
     24  from translate.storage import properties
     25  from translate.convert import po2prop
     26  from translate.convert import mozfunny2prop
     27  from translate.misc.wStringIO import StringIO
     28
     29 -def prop2inc(pf):
     30      """convert a properties file back to a .inc file with #defines in it"""
     31      # any leftover blanks will not be included at the end
     32      pendingblanks = []
     33      for unit in pf.units:
     34          for comment in unit.comments:
     35              if comment.startswith("# converted from") and "#defines" in comment:
     36                  pass
     37              else:
     38                  for blank in pendingblanks:
     39                      yield blank
     40                  # TODO: could convert commented # x=y back to # #define x y
     41                  yield comment
     42          if unit.isblank():
     43              pendingblanks.append("\n")
     44          else:
     45              definition = "#define %s %s\n" % (unit.name, unit.value.replace("\n", "\\n"))
     46              if isinstance(definition, unicode):
     47                  definition = definition.encode("UTF-8")
     48              for blank in pendingblanks:
     49                  yield blank
     50              yield definition
     51
     52 -def prop2it(pf):
     53      """convert a properties file back to a pseudo-properties .it file"""
     54      for unit in pf.units:
     55          for comment in unit.comments:
     56              if comment.startswith("# converted from") and "pseudo-properties" in comment:
     57                  pass
     58              elif comment.startswith("# section: "):
     59                  yield comment.replace("# section: ", "", 1)
     60              else:
     61                  yield comment.replace("#", ";", 1)
     62          if unit.isblank():
     63              yield "\n"
     64          else:
     65              definition = "%s=%s\n" % (unit.name, unit.value)
     66              if isinstance(definition, unicode):
     67                  definition = definition.encode("UTF-8")
     68              yield definition
     69
     70 -def prop2funny(src, itencoding="cp1252"):
     71      lines = src.split("\n")
     72      header = lines[0]
     73      if not header.startswith("# converted from "):
     74          waspseudoprops = len([line for line in lines if line.startswith("# section:")])
     75          wasdefines = len([line for line in lines if line.startswith("#filter") or line.startswith("#unfilter")])
     76      else:
     77          waspseudoprops = "pseudo-properties" in header
     78          wasdefines = "#defines" in header
     79          lines = lines[1:]
     80      if not (waspseudoprops ^ wasdefines):
     81          raise ValueError("could not determine file type as pseudo-properties or defines file")
     82      pf = properties.propfile()
     83      pf.parse("\n".join(lines))
     84      if wasdefines:
     85          for line in prop2inc(pf):
     86              yield line + "\n"
     87      elif waspseudoprops:
     88          for line in prop2it(pf):
     89              yield line.decode("utf-8").encode(itencoding) + "\n"
     90
     91 -def po2inc(inputfile, outputfile, templatefile, encoding=None, includefuzzy=False):
     92      """wraps po2prop but converts outputfile to properties first"""
     93      outputpropfile = StringIO()
     94      if templatefile is not None:
     95          templatelines = templatefile.readlines()
     96          templateproplines = [mozfunny2prop.encodepropline(line) for line in mozfunny2prop.inc2prop(templatelines)]
     97          templatepropfile = StringIO("".join(templateproplines))
     98      else:
     99          templatepropfile = None
    100      result = po2prop.convertmozillaprop(inputfile, outputpropfile, templatepropfile, includefuzzy=includefuzzy)
    101      if result:
    102          outputpropfile.seek(0)
    103          pf = properties.propfile(outputpropfile)
    104          outputlines = prop2inc(pf)
    105          outputfile.writelines(outputlines)
    106      return result
    107
    108 -def po2it(inputfile, outputfile, templatefile, encoding="cp1252", includefuzzy=False):
    109      """wraps po2prop but converts outputfile to properties first"""
    110      outputpropfile = StringIO()
    111      if templatefile is not None:
    112          templatelines = templatefile.readlines()
    113          templateproplines = [mozfunny2prop.encodepropline(line) for line in mozfunny2prop.it2prop(templatelines, encoding=encoding)]
    114          templatepropfile = StringIO("".join(templateproplines))
    115      else:
    116          templatepropfile = None
    117      result = po2prop.convertmozillaprop(inputfile, outputpropfile, templatepropfile, includefuzzy=includefuzzy)
    118      if result:
    119          outputpropfile.seek(0)
    120          pf = properties.propfile(outputpropfile)
    121          outputlines = prop2it(pf)
    122          for line in outputlines:
    123              line = line.decode("utf-8").encode(encoding)
    124              outputfile.write(line)
    125      return result
    126
    127 -def po2ini(inputfile, outputfile, templatefile, encoding="UTF-8", includefuzzy=False):
    128      """wraps po2prop but converts outputfile to properties first using UTF-8 encoding"""
    129      return po2it(inputfile=inputfile, outputfile=outputfile, templatefile=templatefile, encoding=encoding, includefuzzy=includefuzzy)
    130
    131 -def main(argv=None):
    132      import sys
    133      # TODO: get encoding from charset.mk, using parameter
    134      src = sys.stdin.read()
    135      for line in prop2funny(src):
    136          sys.stdout.write(line)
    137
    138  if __name__ == "__main__":
    139      main()
    140