File: exception.py

package info (click to toggle)
python-b2sdk 2.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,020 kB
  • sloc: python: 30,902; sh: 13; makefile: 8
file content (104 lines) | stat: -rw-r--r-- 2,814 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
######################################################################
#
# File: b2sdk/_internal/scan/exception.py
#
# Copyright 2022 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
######################################################################
from __future__ import annotations

from contextlib import contextmanager
from typing import Iterator

from ..exception import B2Error, B2SimpleError


class EnvironmentEncodingError(B2Error):
    """
    Raised when a file name can not be decoded with system encoding.
    """

    def __init__(self, filename, encoding):
        """
        :param filename: an encoded file name
        :type filename: str, bytes
        :param str encoding: file name encoding
        """
        super().__init__()
        self.filename = filename
        self.encoding = encoding

    def __str__(self):
        return f"""file name {self.filename} cannot be decoded with system encoding ({self.encoding}).
We think this is an environment error which you should workaround by
setting your system encoding properly, for example like this:
export LANG=en_US.UTF-8"""


class InvalidArgument(B2Error):
    """
    Raised when one or more arguments are invalid
    """

    def __init__(self, parameter_name, message):
        """
        :param parameter_name: name of the function argument
        :param message: brief explanation of misconfiguration
        """
        super().__init__()
        self.parameter_name = parameter_name
        self.message = message

    def __str__(self):
        return f'{self.parameter_name} {self.message}'


class UnsupportedFilename(B2Error):
    """
    Raised when a filename is not supported by the scan operation
    """

    def __init__(self, message, filename):
        """
        :param message: brief explanation of why the filename was not supported
        :param filename: name of the file which is not supported
        """
        super().__init__()
        self.filename = filename
        self.message = message

    def __str__(self):
        return f'{self.message}: {self.filename}'


@contextmanager
def check_invalid_argument(
    parameter_name: str, message: str, *exceptions: type[Exception]
) -> Iterator[None]:
    """Raise `InvalidArgument` in case of one of given exception was thrown."""
    try:
        yield
    except exceptions as exc:
        if not message:
            message = str(exc)
        raise InvalidArgument(parameter_name, message) from exc


class BaseDirectoryError(B2SimpleError):
    def __init__(self, path):
        self.path = path
        super().__init__(path)


class EmptyDirectory(BaseDirectoryError):
    pass


class UnableToCreateDirectory(BaseDirectoryError):
    pass


class NotADirectory(BaseDirectoryError):
    pass