summaryrefslogtreecommitdiffhomepage
path: root/libs/pysubs2/formatbase.py
blob: 21ea9c4f864e4b99b04eb0d5eeb21b8f0660420f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from typing import Optional
import io


class FormatBase:
    """
    Base class for subtitle format implementations.

    How to implement a new subtitle format:

    1. Create a subclass of FormatBase and override the methods you want to support.
    2. Decide on a format identifier, like the ``"srt"`` or ``"microdvd"`` already used in the library.
    3. Add your identifier and class to :data:`pysubs2.formats.FORMAT_IDENTIFIER_TO_FORMAT_CLASS`.
    4. (optional) Add your file extension and class to :data:`pysubs2.formats.FILE_EXTENSION_TO_FORMAT_IDENTIFIER`.

    After finishing these steps, you can call :meth:`SSAFile.load()` and :meth:`SSAFile.save()` with your
    format, including autodetection from content and file extension (if you provided these).

    """
    @classmethod
    def from_file(cls, subs, fp: io.TextIOBase, format_: str, **kwargs):
        """
        Load subtitle file into an empty SSAFile.

        If the parser autodetects framerate, set it as ``subs.fps``.

        Arguments:
            subs (SSAFile): An empty :class:`SSAFile`.
            fp (file object): Text file object, the subtitle file.
            format_ (str): Format identifier. Used when one format class
                implements multiple formats (see :class:`SubstationFormat`).
            kwargs: Extra options, eg. `fps`.

        Returns:
            None

        Raises:
            pysubs2.exceptions.UnknownFPSError: Framerate was not provided and cannot
                be detected.
        """
        raise NotImplementedError("Parsing is not supported for this format")

    @classmethod
    def to_file(cls, subs, fp: io.TextIOBase, format_: str, **kwargs):
        """
        Write SSAFile into a file.

        If you need framerate and it is not passed in keyword arguments,
        use ``subs.fps``.

        Arguments:
            subs (SSAFile): Subtitle file to write.
            fp (file object): Text file object used as output.
            format_ (str): Format identifier of desired output format.
                Used when one format class implements multiple formats
                (see :class:`SubstationFormat`).
            kwargs: Extra options, eg. `fps`.

        Returns:
            None

        Raises:
            pysubs2.exceptions.UnknownFPSError: Framerate was not provided and
                ``subs.fps is None``.
        """
        raise NotImplementedError("Writing is not supported for this format")

    @classmethod
    def guess_format(self, text: str) -> Optional[str]:
        """
        Return format identifier of recognized format, or None.

        Arguments:
            text (str): Content of subtitle file. When the file is long,
                this may be only its first few thousand characters.

        Returns:
            format identifier (eg. ``"srt"``) or None (unknown format)
        """
        return None