translate.convert.prop2mozfunny

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