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 from translate.storage import base
30 from StringIO import StringIO
31 import gaupol
32
34 """A subtitle entry that is translatable"""
35
36 - def __init__(self, source=None, encoding="utf-8"):
42
44 return ["%s-->%s" % (self._start, self._end)]
45
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
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
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