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

# Source Code for Module translate.convert.po2sub

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2008-2009 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
    23  """Convert Gettext PO localization files to subtitle files"""
    24
    25  from translate.storage import factory
    26
    27
    28 -class resub:
    29
    30 -    def __init__(self, templatefile, inputstore):
    31          from translate.storage import subtitles
    32          self.templatefile = templatefile
    33          self.templatestore = subtitles.SubtitleFile(templatefile)
    34          self.inputstore = inputstore
    35
    36 -    def convertstore(self, includefuzzy=False):
    37          self.includefuzzy = includefuzzy
    38          self.inputstore.makeindex()
    39          for unit in self.templatestore.units:
    40              for location in unit.getlocations():
    41                  if location in self.inputstore.locationindex:
    42                      inputunit = self.inputstore.locationindex[location]
    43                      if inputunit.isfuzzy() and not self.includefuzzy:
    44                          unit.target = unit.source
    45                      else:
    46                          unit.target = inputunit.target
    47                  else:
    48                      unit.target = unit.source
    49          return str(self.templatestore)
    50
    51
    52 -def convertsub(inputfile, outputfile, templatefile, includefuzzy=False):
    53      inputstore = factory.getobject(inputfile)
    54      if templatefile is None:
    55          raise ValueError("must have template file for subtitle files")
    56      else:
    57          convertor = resub(templatefile, inputstore)
    58      outputstring = convertor.convertstore(includefuzzy)
    59      outputfile.write(outputstring)
    60      return 1
    61
    62
    63 -def main(argv=None):
    64      # handle command line options
    65      from translate.convert import convert
    66      formats = {
    67           ("po", "srt"): ("srt", convertsub),
    68           ("po", "sub"): ("sub", convertsub),
    69           ("po", "ssa"): ("ssa", convertsub),
    70           ("po", "ass"): ("ass", convertsub),
    71      }
    72      parser = convert.ConvertOptionParser(formats, usetemplates=True,
    73                                           description=__doc__)
    74      parser.add_fuzzy_option()
    75      parser.run(argv)
    76
    77  if __name__ == '__main__':
    78      main()
    79