aboutsummaryrefslogtreecommitdiff
path: root/lib/dodai/exception.py
blob: 23302c7d0ac998e6049d68154120ff2fdb80222a (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# Copyright (C) 2010  Leonard Thomas
#
# This file is part of Dodai.
#
# Dodai is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Dodai is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Dodai.  If not, see <http://www.gnu.org/licenses/>.

from dodai.tools import list_to_english
from dodai.tools import quote_list

class DodaiException(Exception):

    def _system_encoding(self):
        # Returns the character encoding of the operating system

        encoding = sys.getdefaultencoding()
        filesystem_encoding = sys.getfilesystemencoding()
        if filesystem_encoding:
            encoding = filesystem_encoding
        return encoding


class HimoAsciiError(DodaiException):
    """Exception raised when the Himo object can't convert a character
    down to it's root character.

    Attributes:
        char:  The character that can't be converted down to ascii
    """
    MESSAGE="Unable to convert the '{char}' character to ascii"

    def __init__(self, char):
        self.char = char
        self.msg = self._build_message()

    def _build_message(self):
        encoding = self._system_encoding()
        try:
            char = self.char.encode(encoding)
        except UnicodeEncodeError:
            char = 'unichr({0})'.format(ord(self.char))
        return self.MESSAGE.format(char=self.char)

    def __str__(self):
        return self.msg

class DodaiDatabaseConnectionConfigurationError(DodaiException):
    pass


class DatabaseEmptyOptionException(DodaiDatabaseConnectionConfigurationError):
    """Exception raised for an empty option in the config

    Attributes:
        section_name:       The section of the config file that contains
                            the invalid protocol
        option_name:        The name of the empty option

    """
    MESSAGE = "In the '{section_name}' section, the '{option_name}' "\
              "was not set or is missing.  Please set this option."

    def __init__(self, section_name, option_name):
        self.section_name = section_name
        self.option_name = option_name
        self.msg = self._build_message()

    def _build_message(self):
        return self.MESSAGE.format(section_name=self.section_name,
                                   option_name=self.option_name)

    def __str__(self):
        return self.msg


class DatabasePortException(DodaiDatabaseConnectionConfigurationError):
    """Exception raised for invalid database port connection

    Attributes:
        section_name:       The section of the config file that contains
                            the invalid protocol
        section_port:       The port value that was listed in the
                            config file

    """
    MESSAGE = "In the '{section_name}' section, the port of "\
              "'{section_port}' is invalid.  The port must be a "\
              "number between 1 and 65535"

    def __init__(self, section_name, section_port):
        self.section_name = section_name
        self.section_port = section_port
        self.msg = self._build_message()

    def _build_message(self):
        return self.MESSAGE.format(section_name=self.section_name,
                                   section_port=self.section_port)

    def __str__(self):
        return self.msg


class DatabaseHostnameException(DodaiDatabaseConnectionConfigurationError):
    """Exception raised for invalid database hostname

    Attributes:
        section_name:       The section of the config file that contains
                            the invalid protocol
        section_hostname:   The hostname value that was listed in the
                            config file

    """
    MESSAGE = "In the '{section_name}' section, the hostname of "\
              "'{section_hostname}' is invalid.  Please use a valid "\
              "hostname."

    MSG_NON = "In the '{section_name}' section, the hostname was "\
              "not set.  Please set the hostname."

    def __init__(self, section_name, section_hostname):
        self.section_name = section_name
        self.section_hostname = section_hostname
        self.msg = self._build_message()

    def _build_message(self):
        if self.section_hostname:
            return self.MESSAGE.format(section_name=self.section_name,
                                       section_hostname=self.section_hostname)
        else:
            return self.MSG_NON.format(section_name=self.section_name)

    def __str__(self):
        return self.msg


class DatabaseProtocolException(DodaiDatabaseConnectionConfigurationError):
    """Exception raised for invalid database connection protocols

    Attributes:
        section_name:       The section of the config file that contains
                            the invalid protocol
        section_protocol:   The protocol value that was listed in the
                            config file
        database_type:      Usually 'server' or 'file'
        protocols:          List of valid protocols

    """
    MESSAGE = "In the '{section_name}' section, the protocol of "\
              "'{section_protocol}' is invalid.  The valid protocols "\
              "for a '{database_type}' connection are: {protocols}"

    def __init__(self, section_name, section_protocol, database_type,
                 protocols):
        self.section_name = section_name
        self.section_protocol = section_protocol
        self.database_type = database_type
        self.protocols = protocols
        self.msg = self._build_message()

    def _build_message(self):
        protocols = list_to_english(self.protocols)
        return self.MESSAGE.format(section_name=self.section_name,
                                   section_protocol=self.section_protocol,
                                   database_type=self.database_type,
                                   protocols=protocols)

    def __str__(self):
        return self.msg


class DatabaseConnectionException(DodaiDatabaseConnectionConfigurationError):
    """Exception raised for missing database connection parameters

    Attributes:
        section_name:       The section of the config file that contains
                            the invalid connection information
        options_required:   A dictionary containing the database_type
                            as the key and a list of required options
                            as the value

    """
    MESSAGE =  "The '{section_name}' section does not contain all of the "\
               "correct information needed to make a database connection."
    MSG_TYPE = "To make a '{database_type}' connection please make sure "\
               "To have all of the following options: {options}."
    MSG_END =  "Please remember that the option names are case sensitive."

    def __init__(self, section_name, validators):
        self.section_name = section_name
        self.validators = validators
        self.msg = self._build_message()

    def _build_message(self):
        out = []
        out.append(self.MESSAGE.format(section_name=self.section_name))
        for validator in self.validators:
            options = list_to_english(validator.REQUIRED)
            out.append(self.MSG_TYPE.format(database_type=validator.DB_TYPE,
                                            options=options))
        out.append(self.MSG_END)
        return '  '.join(out)

    def __str__(self):
        return self.msg


class UnknownDatabaseConnectionException(
                            DodaiDatabaseConnectionConfigurationError):
    """Exception raised for missing database connection parameters

    Attributes:
        section:  The requested section of the config file that can not
                  be found.

    """
    MESSAGE = "Unable to find the '{section}' section to create a "\
              "database connection."

    def __init__(self, section):
        self.section = section
        self.msg = self._build_message()

    def _build_message(self):
        return self.MESSAGE.format(section=self.section)

    def __str__(self):
        return self.msg


class InvalidConfigParser(DodaiException):
    """Exception raised when an invalid parser is registered in the
    dodai.config.ConfigFiles object.

    """
    MESSAGE = "The parser object '{name}' that you were trying to register "\
              "is not a valid parser object.  Please make sure that this "\
              "object contains all of the following methods: {methods}"

    def __init__(self, required_methods, name):
        self.msg = self._build_message(required_methods, name)

    def _build_message(self, required_methods, name):
        methods = quote_list(required_methods)
        return self.MESSAGE.format(methods=methods, name=name)

    def __str__(self):
        return self.msg


class FileDoesNotExist(DodaiException):
    """Exception raised when a file does not exist.

    """
    MESSAGE = "The file: '{file_}' does not exist."

    def __init__(self, filepath):
        self.msg = self._build_message(filepath)

    def _build_message(self, filepath):
        return self.MESSAGE.format(file_=filepath)

    def __str__(self):
        return self.msg


class FileIsDirectory(DodaiException):
    """Exception raised when a file is a directory.

    """
    MESSAGE = "The file: '{file_}' is a directory."

    def __init__(self, filepath):
        self.msg = self._build_message(filepath)

    def _build_message(self, filepath):
        return self.MESSAGE.format(file_=filepath)

    def __str__(self):
        return self.msg