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   
 30  from StringIO import StringIO 
 31   
 32  import gaupol 
 33   
 34  from translate.storage import base 
35 36 -class SubtitleUnit(base.TranslationUnit):
37 """A subtitle entry that is translatable""" 38
39 - def __init__(self, source=None, encoding="utf_8"):
40 self._start = None 41 self._end = None 42 if source: 43 self.source = source 44 super(SubtitleUnit, self).__init__(source)
45
46 - def getnotes(self, origin=None):
47 if origin in ['programmer', 'developer', 'source code', None]: 48 return "visible for %d seconds" % self._duration 49 else: 50 return ''
51
52 - def getlocations(self):
53 return ["%s-->%s" % (self._start, self._end)]
54
55 - def getid(self):
56 return self.getlocations()[0]
57
58 -class SubtitleFile(base.TranslationStore):
59 """A subtitle file""" 60 UnitClass = SubtitleUnit
61 - def __init__(self, inputfile=None, unitclass=UnitClass):
62 """construct an Subtitle file, optionally reading in from inputfile.""" 63 self.UnitClass = unitclass 64 base.TranslationStore.__init__(self, unitclass=unitclass) 65 self.units = [] 66 self.filename = None 67 self._subtitlefile = None 68 self._encoding = 'utf_8' 69 if inputfile is not None: 70 self._parsefile(inputfile)
71
72 - def __str__(self):
73 subtitles = [] 74 for unit in self.units: 75 subtitle = gaupol.subtitle.Subtitle() 76 subtitle.main_text = unit.target or unit.source 77 subtitle.start = unit._start 78 subtitle.end = unit._end 79 subtitles.append(subtitle) 80 output = StringIO() 81 self._subtitlefile.write_to_file(subtitles, gaupol.documents.MAIN, output) 82 return output.getvalue().encode(self._subtitlefile.encoding)
83
84 - def _parse(self):
85 self._encoding = gaupol.encodings.detect(self.filename) 86 if self._encoding == 'ascii': 87 self._encoding = 'utf_8' 88 self._format = gaupol.FormatDeterminer().determine(self.filename, self._encoding) 89 self._subtitlefile = gaupol.files.new(self._format, self.filename, self._encoding) 90 for subtitle in self._subtitlefile.read(): 91 newunit = self.addsourceunit(subtitle.main_text) 92 newunit._start = subtitle.start 93 newunit._end = subtitle.end 94 newunit._duration = subtitle.duration_seconds
95
96 - def _parsefile(self, storefile):
97 if hasattr(storefile, 'name'): 98 self.filename = storefile.name 99 storefile.close() 100 elif hasattr(storefile, 'filename'): 101 self.filename = storefile.filename 102 storefile.close() 103 elif isinstance(storefile, basestring): 104 self.filename = storefile 105 if self.filename: 106 self._parse()
107 108 @classmethod
109 - def parsefile(cls, storefile):
110 """parse the given file""" 111 newstore = cls() 112 newstore._parsefile(storefile) 113 return newstore
114 115 @classmethod
116 - def parsestring(cls, storestring):
117 # Gaupol does not allow parsing from strings 118 119 #FIXME: maybe we can write to a temporary file? 120 raise NotImplementedError
121
122 - def parse(self, data):
123 # Gaupol does not allow parsing from strings 124 raise NotImplementedError
125 126 127 ############# format specific classes ################### 128 129 # the generic SubtitleFile can adapt to any format, but only the 130 # specilized classes can be used to construct a new file 131 132 from gaupol.files import MicroDVD, SubStationAlpha, AdvSubStationAlpha, SubRip 133 from gaupol.newlines import newlines
134 -class SubRipFile(SubtitleFile):
135 """specialized class for SubRipFile's only""" 136 Name = _("SubRip subtitles file") 137 Extensions = ['srt']
138 - def __init__(self, *args, **kwargs):
139 super(SubRipFile, self).__init__(*args, **kwargs) 140 if self._subtitlefile is None: 141 self._subtitlefile = SubRip(self.filename or '', self._encoding) 142 if self._subtitlefile.newline is None: 143 self._subtitlefile.newline = newlines.UNIX
144
145 -class MicroDVDFile(SubtitleFile):
146 """specialized class for SubRipFile's only""" 147 Name = _("MicroDVD subtitles file") 148 Extensions = ['sub']
149 - def __init__(self, *args, **kwargs):
150 super(SubRipFile, self).__init__(*args, **kwargs) 151 if self._subtitlefile is None: 152 self._subtitlefile = MicroDVD(self.filename or '', self._encoding) 153 if self._subtitlefile.newline is None: 154 self._subtitlefile.newline = newlines.UNIX
155
156 -class AdvSubStationAlphaFile(SubtitleFile):
157 """specialized class for SubRipFile's only""" 158 Name = _("Advanced Substation Alpha subtitles file") 159 Extensions = ['ass']
160 - def __init__(self, *args, **kwargs):
161 super(SubRipFile, self).__init__(*args, **kwargs) 162 if self._subtitlefile is None: 163 self._subtitlefile = AdvSubStationAlpha(self.filename or '', self._encoding) 164 if self._subtitlefile.newline is None: 165 self._subtitlefile.newline = newlines.UNIX
166
167 -class SubStationAlphaFile(SubtitleFile):
168 """specialized class for SubRipFile's only""" 169 Name = _("Substation Alpha subtitles file") 170 Extensions = ['ssa']
171 - def __init__(self, *args, **kwargs):
172 super(SubRipFile, self).__init__(*args, **kwargs) 173 if self._subtitlefile is None: 174 self._subtitlefile = SubStationAlpha(self.filename or '', self._encoding) 175 if self._subtitlefile.newline is None: 176 self._subtitlefile.newline = newlines.UNIX
177