translate.tools.pomerge

Package translate :: Package tools :: Module pomerge |
[hide private]
[frames] | no frames]

# Source Code for Module translate.tools.pomerge

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2002-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  """Merges XLIFF and Gettext PO localization files
     23
     24  Snippet file produced by pogrep or updated by a translator can be merged into
     25  existing files
     26
     27  See: http://translate.sourceforge.net/wiki/toolkit/pomerge for examples and
     28  usage instructions
     29  """
     30
     31  import logging
     32  from translate.storage import factory
     33  from translate.storage.poheader import poheader
     34
     35
     36 -def mergestores(store1, store2, mergeblanks, mergecomments):
     37      """Take any new translations in store2 and write them into store1."""
     38
     39      for unit2 in store2.units:
     40          if unit2.isheader():
     41              if isinstance(store1, poheader):
     42                  store1.mergeheaders(store2)
     43              continue
     44          unit1 = store1.findid(unit2.getid())
     45          if unit1 is None:
     46              unit1 = store1.findunit(unit2.source)
     47          if unit1 is None:
     48              logging.error("The template does not contain the following unit:\n%s",
     49                            str(unit2))
     50          else:
     51              if not mergeblanks:
     52                  if len(unit2.target.strip()) == 0:
     53                      continue
     54              unit1.merge(unit2, overwrite=True, comments=mergecomments)
     55      return store1
     56
     57
     58 -def str2bool(option):
     59      """Convert a string value to boolean
     60
     61      @param option: yes, true, 1, no, false, 0
     62      @type option: String
     63      @rtype: Boolean
     64
     65      """
     66      option = option.lower()
     67      if option in ("yes", "true", "1"):
     68          return True
     69      elif option in ("no", "false", "0"):
     70          return False
     71      else:
     72          raise ValueError("invalid boolean value: %r" % option)
     73
     74
     75 -def mergestore(inputfile, outputfile, templatefile, mergeblanks="no",
     76                 mergecomments="yes"):
     77      try:
     78          mergecomments = str2bool(mergecomments)
     79      except ValueError:
     80          raise ValueError("invalid mergecomments value: %r" % mergecomments)
     81      try:
     82          mergeblanks = str2bool(mergeblanks)
     83      except ValueError:
     84          raise ValueError("invalid mergeblanks value: %r" % mergeblanks)
     85      inputstore = factory.getobject(inputfile)
     86      if templatefile is None:
     87          # just merge nothing
     88          templatestore = type(inputstore)()
     89      else:
     90          templatestore = factory.getobject(templatefile)
     91      outputstore = mergestores(templatestore, inputstore, mergeblanks,
     92                                mergecomments)
     93      if outputstore.isempty():
     94          return 0
     95      outputfile.write(str(outputstore))
     96      return 1
     97
     98
     99 -def main():
    100      from translate.convert import convert
    101      pooutput = ("po", mergestore)
    102      potoutput = ("pot", mergestore)
    103      xliffoutput = ("xlf", mergestore)
    104      formats = {("po", "po"): pooutput, ("po", "pot"): pooutput,
    105                 ("pot", "po"): pooutput, ("pot", "pot"): potoutput,
    106                 "po": pooutput, "pot": pooutput,
    107                 ("xlf", "po"): pooutput, ("xlf", "pot"): pooutput,
    108                 ("xlf", "xlf"): xliffoutput, ("po", "xlf"): xliffoutput,
    109                }
    110      mergeblanksoption = convert.optparse.Option("", "--mergeblanks",
    111          dest="mergeblanks", action="store", default="yes",
    112          help="whether to overwrite existing translations with blank translations (yes/no). Default is yes.")
    113      mergecommentsoption = convert.optparse.Option("", "--mergecomments",
    114          dest="mergecomments", action="store", default="yes",
    115          help="whether to merge comments as well as translations (yes/no). Default is yes.")
    116      parser = convert.ConvertOptionParser(formats, usetemplates=True,
    117                                           description=__doc__)
    118      parser.add_option(mergeblanksoption)
    119      parser.passthrough.append("mergeblanks")
    120      parser.add_option(mergecommentsoption)
    121      parser.passthrough.append("mergecomments")
    122      parser.run()
    123
    124
    125  if __name__ == '__main__':
    126      main()
    127