translate.convert.po2csv

Package translate :: Package convert :: Module po2csv |
[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.po2csv

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2003-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  """convert Gettext PO localization files to Comma-Separated Value (.csv) files
     23
     24  see: http://translate.sourceforge.net/wiki/toolkit/po2csv for examples and
     25  usage instructions
     26  """
     27
     28  from translate.storage import po
     29  from translate.storage import csvl10n
     30
     31
     32 -class po2csv:
     33
     34 -    def convertcomments(self, inputunit):
     35          return " ".join(inputunit.getlocations())
     36
     37 -    def convertunit(self, inputunit):
     38          csvunit = csvl10n.csvunit()
     39          if inputunit.isheader():
     40              return None
     41              #csvunit.location = "location"
     42              #csvunit.source = "source"
     43              #csvunit.target = "target"
     44          elif inputunit.isblank():
     45              return None
     46          else:
     47              csvunit.location = self.convertcomments(inputunit)
     48              csvunit.source = inputunit.source
     49              csvunit.target = inputunit.target
     50          return csvunit
     51
     52 -    def convertplurals(self, inputunit):
     53          """Convert PO plural units
     54
     55          We only convert the first plural form.  So languages with multiple
     56          plurals are not handled.  For single plural languages we simply
     57          skip this plural extraction.
     58          """
     59          if len(inputunit.target.strings) == 1:  # No plural forms
     60              return None
     61          csvunit = csvl10n.csvunit()
     62          csvunit.location = self.convertcomments(inputunit)
     63          csvunit.source = inputunit.source.strings[1]
     64          csvunit.target = inputunit.target.strings[1]
     65          return csvunit
     66
     67 -    def convertstore(self, inputstore, columnorder=None):
     68          if columnorder is None:
     69              columnorder = ['location', 'source', 'target']
     70          outputstore = csvl10n.csvfile(fieldnames=columnorder)
     71          for inputunit in inputstore.units:
     72              outputunit = self.convertunit(inputunit)
     73              if outputunit is not None:
     74                  outputstore.addunit(outputunit)
     75              if inputunit.hasplural():
     76                  outputunit = self.convertplurals(inputunit)
     77                  if outputunit is not None:
     78                      outputstore.addunit(outputunit)
     79          return outputstore
     80
     81
     82 -def convertcsv(inputfile, outputfile, templatefile, columnorder=None):
     83      """reads in inputfile using po, converts using po2csv, writes to outputfile"""
     84      # note that templatefile is not used, but it is required by the converter...
     85      inputstore = po.pofile(inputfile)
     86      if inputstore.isempty():
     87          return 0
     88      convertor = po2csv()
     89      outputstore = convertor.convertstore(inputstore, columnorder)
     90      outputfile.write(str(outputstore))
     91      return 1
     92
     93
     94 -def main(argv=None):
     95      from translate.convert import convert
     96      formats = {"po": ("csv", convertcsv)}
     97      parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__)
     98      parser.add_option("", "--columnorder", dest="columnorder", default=None,
     99          help="specify the order and position of columns (location,source,target)")
    100      parser.passthrough.append("columnorder")
    101      parser.run(argv)
    102
    103
    104  if __name__ == '__main__':
    105      main()
    106