1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
37 """A subtitle entry that is translatable"""
38
39 - def __init__(self, source=None, encoding="utf_8"):
45
47 if origin in ['programmer', 'developer', 'source code', None]:
48 return "visible for %d seconds" % self._duration
49 else:
50 return ''
51
53 return ["%s-->%s" % (self._start, self._end)]
54
57
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
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
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
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
110 """parse the given file"""
111 newstore = cls()
112 newstore._parsefile(storefile)
113 return newstore
114
115 @classmethod
117
118
119
120 raise NotImplementedError
121
123
124 raise NotImplementedError
125
126
127
128
129
130
131
132 from gaupol.files import MicroDVD, SubStationAlpha, AdvSubStationAlpha, SubRip
133 from gaupol.newlines import newlines
135 """specialized class for SubRipFile's only"""
136 Name = _("SubRip subtitles file")
137 Extensions = ['srt']
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
146 """specialized class for SubRipFile's only"""
147 Name = _("MicroDVD subtitles file")
148 Extensions = ['sub']
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
157 """specialized class for SubRipFile's only"""
158 Name = _("Advanced Substation Alpha subtitles file")
159 Extensions = ['ass']
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
168 """specialized class for SubRipFile's only"""
169 Name = _("Substation Alpha subtitles file")
170 Extensions = ['ssa']
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