File: exceptions.py

package info (click to toggle)
python-mediafile 0.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,968 kB
  • sloc: python: 2,953; makefile: 193
file content (72 lines) | stat: -rw-r--r-- 1,875 bytes parent folder | download
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
"""Custom exceptions for MediaFile metadata handling."""

from __future__ import annotations


class MediaFileError(Exception):
    """Base exception for all MediaFile-related errors."""

    def __init__(
        self,
        message: str,
        filename: str | None = None,
    ):
        self.filename = filename
        self.message = message
        super().__init__(self._format_message())

    def _format_message(self) -> str:
        if self.filename:
            return f"{self.filename}: {self.message}"
        return self.message

    def __str__(self) -> str:
        return self._format_message()


class UnreadableFileError(MediaFileError):
    """Raised when Mutagen cannot extract information from the file."""

    def __init__(
        self,
        filename: str,
        message: str,
    ):
        super().__init__(
            message,
            filename,
        )


class FileTypeError(UnreadableFileError):
    """Reading this type of file is not supported.

    If passed the `mutagen_type` argument this indicates that the
    mutagen type is not supported by `Mediafile`.
    """

    def __init__(
        self,
        filename: str,
        mutagen_type: str | None = None,
    ):
        if mutagen_type is None:
            msg = "File is not in a recognized format"
        else:
            msg = f"File type '{mutagen_type}' is not supported"

        super().__init__(filename, msg)


class MutagenError(UnreadableFileError):
    """Raised when Mutagen fails unexpectedly, likely due to a bug."""

    mutagen_exception: Exception

    def __init__(self, filename: str, mutagen_exception: Exception):
        self.mutagen_exception = mutagen_exception
        message = f"Mutagen internal error: {mutagen_exception}"
        super().__init__(filename, message)


__all__ = ["UnreadableFileError", "FileTypeError", "MutagenError"]