translate.storage.subtitles

Package translate :: Package storage :: Module subtitles |
[hide private]
[frames] | no frames]

# Source Code for Module translate.storage.subtitles

     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  """Class that manages subtitle files for translation
    23
    24     This class makes use of the subtitle functionality of L{gaupol}
    25     @see: gaupo/agents/open.py::open_main
    26
    27     a patch to gaupol is required to open utf-8 files successfully
    28  """
    29  from translate.storage import base
    30  from StringIO import StringIO
    31  import gaupol
    32
    33 -class SubtitleUnit(base.TranslationUnit):
    34      """A subtitle entry that is translatable"""
    35
    36 -    def __init__(self, source=None, encoding="utf-8"):
    37          self._start = None
    38          self._end = None
    39          if source:
    40              self.source = source
    41          super(SubtitleUnit, self).__init__(source)
    42
    43 -    def getlocations(self):
    44          return ["%s-->%s" % (self._start, self._end)]
    45
    46 -class SubtitleFile(base.TranslationStore):
    47      """A subtitle file"""
    48      UnitClass = SubtitleUnit
    49 -    def __init__(self, inputfile=None, unitclass=UnitClass):
    50          """construct an Subtitle file, optionally reading in from inputfile."""
    51          self.UnitClass = unitclass
    52          base.TranslationStore.__init__(self, unitclass=unitclass)
    53          self.units = []
    54          self.filename = ''
    55          self._subtitlefile = None
    56          self._encoding = 'utf-8'
    57          if inputfile is not None:
    58              self.parse(inputfile)
    59
    60 -    def __str__(self):
    61          subtitles = []
    62          for unit in self.units:
    63              subtitle = gaupol.subtitle.Subtitle()
    64              subtitle.main_text = unit.target or unit.source
    65              subtitle.start = unit._start
    66              subtitle.end = unit._end
    67              subtitles.append(subtitle)
    68          output = StringIO()
    69          self._subtitlefile.write_to_file(subtitles, gaupol.documents.MAIN, output)
    70          return output.getvalue().encode(self._subtitlefile.encoding)
    71
    72
    73 -    def parse(self, input):
    74          """parse the given file"""
    75          if hasattr(input, 'name'):
    76              self.filename = input.name
    77          elif not getattr(self, 'filename', ''):
    78              self.filename = ''
    79          input.close()
    80          self._encoding = gaupol.encodings.detect(self.filename)
    81          if self._encoding == 'ascii':
    82              self._encoding = 'utf-8'
    83          self._format = gaupol.FormatDeterminer().determine(self.filename, self._encoding)
    84          self._subtitlefile = gaupol.files.new(self._format, self.filename, self._encoding)
    85          for subtitle in self._subtitlefile.read():
    86              newunit = self.addsourceunit(subtitle.main_text)
    87              newunit._start = subtitle.start
    88              newunit._end =  subtitle.end
    89              newunit.addnote("visible for %d seconds" % subtitle.duration_seconds, "developer")
    90