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