mymeco.exceptions

mymeco/exceptions.py
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
# coding: utf-8
"""Top Level exceptions module raised by Mymeco."""
import typing


MymecoErrorType = typing.TypeVar('MymecoErrorType', bound='MymecoError')


class MymecoError(BaseException):
    """
    All exception raised by Mymeco should inherit this base class.

    >>> MymecoError('Generic Error')
    MymecoError: Generic Error

    >>> print(MymecoError('Unexpected behaviour'))
    Unexpected behaviour
    """

    def __init__(self: MymecoErrorType, message: str) -> None:
        """
        Build a standard Mymeco exception.

        :param message: Human readable information on exception.
            Message should help end-user to understand why such error occurs,
            and possibly some hints on how to solve issue.
        """
        super().__init__()
        self.message = message

    def __repr__(self: MymecoErrorType) -> str:
        """Give detailed information on error."""
        return '{}: {}'.format(self.__class__.__name__, self.message)

    def __str__(self: MymecoErrorType) -> str:
        """Give human readable description on error."""
        return self.message


class NoConfigurationFile(MymecoError):
    """
    Raise when no configuration file is available.

    >>> NoConfigurationFile()
    NoConfigurationFile: No configuration file was found on your system.
    """

    def __init__(self) -> None:
        """Build a missing configuration file exception."""
        super().__init__('No configuration file was found on your system.')


class MissingConfiguration(MymecoError):
    """
    Raise when a necessary configuration entry is not found.

    >>> MissingConfiguration('section', 'key', 'file')
    MissingConfiguration: [section].key is not found in file
    """

    def __init__(self,
                 section: str,
                 key: typing.Optional[str],
                 filename: str) -> None:
        """
        Build a MissingConfiguration exception.

        :param section: Name of section with missing entry
        :param key: Name of missing key in section. Could be None if even the
            given section is not found.
        :param filename: Configuration filename used in this context.
        """
        super().__init__('[{}].{} is not found in {}'.format(
            section, key, filename))