~ubuntu-branches/ubuntu/saucy/gaupol/saucy-proposed

« back to all changes in this revision

Viewing changes to aeidon/files/subrip.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski
  • Date: 2013-01-06 17:10:10 UTC
  • mfrom: (1.2.8)
  • mto: (10.2.3 experimental)
  • mto: This revision was merged to the branch mainline in revision 22.
  • Revision ID: package-import@ubuntu.com-20130106171010-kmlq0sy324jlp9zz
Tags: upstream-0.21
Import upstream version 0.21

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
1
3
# Copyright (C) 2005-2009 Osmo Salomaa
2
4
#
3
5
# This file is part of Gaupol.
27
29
    """SubRip file."""
28
30
 
29
31
    _re_time_line = re.compile((
30
 
            r"^(-?\d\d:\d\d:\d\d,\d\d\d) -->"
31
 
            r" (-?\d\d:\d\d:\d\d,\d\d\d)"
 
32
            # Techically all these fields should have fixed widths, but in the
 
33
            # name of being liberal in accepting input, accept lesser widths
 
34
            # assuming that they are just lacking zero-padding from the side
 
35
            # that is farther from the decimal point.
 
36
            r"^(-?\d{1,2}:\d{1,2}:\d{1,2},\d{1,3}) -->"
 
37
            r" (-?\d{1,2}:\d{1,2}:\d{1,2},\d{1,3})"
32
38
            r"(  X1:(\d+) X2:(\d+) Y1:(\d+) Y2:(\d+))?\s*$"))
33
39
 
34
40
    format = aeidon.formats.SUBRIP
48
54
        return lines
49
55
 
50
56
    def read(self):
51
 
        """Read file and return subtitles.
 
57
        """
 
58
        Read file and return subtitles.
52
59
 
53
60
        Raise :exc:`IOError` if reading fails.
54
61
        Raise :exc:`UnicodeError` if decoding fails.
62
69
                subtitles[-1].main_text += line
63
70
                continue
64
71
            subtitle = self._get_subtitle()
65
 
            subtitle.start = match.group(1).replace(",", ".")
66
 
            subtitle.end = match.group(2).replace(",", ".")
 
72
            subtitle.start_time = subtitle.calc.normalize_time(match.group(1))
 
73
            subtitle.end_time = subtitle.calc.normalize_time(match.group(2))
67
74
            if match.group(3) is not None:
68
75
                subtitle.subrip.x1 = int(match.group(4))
69
76
                subtitle.subrip.x2 = int(match.group(5))
73
80
        return subtitles
74
81
 
75
82
    def write_to_file(self, subtitles, doc, fobj):
76
 
        """Write `subtitles` from `doc` to `fobj`.
 
83
        """
 
84
        Write `subtitles` from `doc` to `fobj`.
77
85
 
78
86
        Raise :exc:`IOError` if writing fails.
79
87
        Raise :exc:`UnicodeError` if encoding fails.
81
89
        n = self.newline.value
82
90
        for i, subtitle in enumerate(subtitles):
83
91
            if i > 0: fobj.write(n)
84
 
            fobj.write("%d%s" % ((i + 1), n))
 
92
            fobj.write("{:d}{}".format((i + 1), n))
85
93
            start = subtitle.start_time.replace(".", ",")
86
94
            end = subtitle.end_time.replace(".", ",")
87
 
            fobj.write("%s --> %s" % (start, end))
 
95
            fobj.write("{} --> {}".format(start, end))
88
96
            # Write Extended SubRip coordinates only if the container
89
97
            # has been initialized and the coordinates make some sense.
90
98
            if subtitle.has_container("subrip"):
93
101
                y1 = subtitle.subrip.y1
94
102
                y2 = subtitle.subrip.y2
95
103
                if not (x1 == x2 == y1 == y2 == 0):
96
 
                    fobj.write("  X1:%03d X2:%03d" % (x1, x2))
97
 
                    fobj.write( " Y1:%03d Y2:%03d" % (y1, y2))
 
104
                    fobj.write("  X1:{:03d} X2:{:03d}".format(x1, x2))
 
105
                    fobj.write( " Y1:{:03d} Y2:{:03d}".format(y1, y2))
98
106
            text = subtitle.get_text(doc).replace("\n", n)
99
 
            fobj.write("%s%s%s" % (n, text, n))
 
107
            fobj.write("{}{}{}".format(n, text, n))