aboutsummaryrefslogtreecommitdiff
path: root/lib/dodai/exception.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/dodai/exception.py')
-rw-r--r--lib/dodai/exception.py289
1 files changed, 289 insertions, 0 deletions
diff --git a/lib/dodai/exception.py b/lib/dodai/exception.py
new file mode 100644
index 0000000..23302c7
--- /dev/null
+++ b/lib/dodai/exception.py
@@ -0,0 +1,289 @@
1# Copyright (C) 2010 Leonard Thomas
2#
3# This file is part of Dodai.
4#
5# Dodai is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# Dodai is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with Dodai. If not, see <http://www.gnu.org/licenses/>.
17
18from dodai.tools import list_to_english
19from dodai.tools import quote_list
20
21class DodaiException(Exception):
22
23 def _system_encoding(self):
24 # Returns the character encoding of the operating system
25
26 encoding = sys.getdefaultencoding()
27 filesystem_encoding = sys.getfilesystemencoding()
28 if filesystem_encoding:
29 encoding = filesystem_encoding
30 return encoding
31
32
33class HimoAsciiError(DodaiException):
34 """Exception raised when the Himo object can't convert a character
35 down to it's root character.
36
37 Attributes:
38 char: The character that can't be converted down to ascii
39 """
40 MESSAGE="Unable to convert the '{char}' character to ascii"
41
42 def __init__(self, char):
43 self.char = char
44 self.msg = self._build_message()
45
46 def _build_message(self):
47 encoding = self._system_encoding()
48 try:
49 char = self.char.encode(encoding)
50 except UnicodeEncodeError:
51 char = 'unichr({0})'.format(ord(self.char))
52 return self.MESSAGE.format(char=self.char)
53
54 def __str__(self):
55 return self.msg
56
57class DodaiDatabaseConnectionConfigurationError(DodaiException):
58 pass
59
60
61class DatabaseEmptyOptionException(DodaiDatabaseConnectionConfigurationError):
62 """Exception raised for an empty option in the config
63
64 Attributes:
65 section_name: The section of the config file that contains
66 the invalid protocol
67 option_name: The name of the empty option
68
69 """
70 MESSAGE = "In the '{section_name}' section, the '{option_name}' "\
71 "was not set or is missing. Please set this option."
72
73 def __init__(self, section_name, option_name):
74 self.section_name = section_name
75 self.option_name = option_name
76 self.msg = self._build_message()
77
78 def _build_message(self):
79 return self.MESSAGE.format(section_name=self.section_name,
80 option_name=self.option_name)
81
82 def __str__(self):
83 return self.msg
84
85
86class DatabasePortException(DodaiDatabaseConnectionConfigurationError):
87 """Exception raised for invalid database port connection
88
89 Attributes:
90 section_name: The section of the config file that contains
91 the invalid protocol
92 section_port: The port value that was listed in the
93 config file
94
95 """
96 MESSAGE = "In the '{section_name}' section, the port of "\
97 "'{section_port}' is invalid. The port must be a "\
98 "number between 1 and 65535"
99
100 def __init__(self, section_name, section_port):
101 self.section_name = section_name
102 self.section_port = section_port
103 self.msg = self._build_message()
104
105 def _build_message(self):
106 return self.MESSAGE.format(section_name=self.section_name,
107 section_port=self.section_port)
108
109 def __str__(self):
110 return self.msg
111
112
113class DatabaseHostnameException(DodaiDatabaseConnectionConfigurationError):
114 """Exception raised for invalid database hostname
115
116 Attributes:
117 section_name: The section of the config file that contains
118 the invalid protocol
119 section_hostname: The hostname value that was listed in the
120 config file
121
122 """
123 MESSAGE = "In the '{section_name}' section, the hostname of "\
124 "'{section_hostname}' is invalid. Please use a valid "\
125 "hostname."
126
127 MSG_NON = "In the '{section_name}' section, the hostname was "\
128 "not set. Please set the hostname."
129
130 def __init__(self, section_name, section_hostname):
131 self.section_name = section_name
132 self.section_hostname = section_hostname
133 self.msg = self._build_message()
134
135 def _build_message(self):
136 if self.section_hostname:
137 return self.MESSAGE.format(section_name=self.section_name,
138 section_hostname=self.section_hostname)
139 else:
140 return self.MSG_NON.format(section_name=self.section_name)
141
142 def __str__(self):
143 return self.msg
144
145
146class DatabaseProtocolException(DodaiDatabaseConnectionConfigurationError):
147 """Exception raised for invalid database connection protocols
148
149 Attributes:
150 section_name: The section of the config file that contains
151 the invalid protocol
152 section_protocol: The protocol value that was listed in the
153 config file
154 database_type: Usually 'server' or 'file'
155 protocols: List of valid protocols
156
157 """
158 MESSAGE = "In the '{section_name}' section, the protocol of "\
159 "'{section_protocol}' is invalid. The valid protocols "\
160 "for a '{database_type}' connection are: {protocols}"
161
162 def __init__(self, section_name, section_protocol, database_type,
163 protocols):
164 self.section_name = section_name
165 self.section_protocol = section_protocol
166 self.database_type = database_type
167 self.protocols = protocols
168 self.msg = self._build_message()
169
170 def _build_message(self):
171 protocols = list_to_english(self.protocols)
172 return self.MESSAGE.format(section_name=self.section_name,
173 section_protocol=self.section_protocol,
174 database_type=self.database_type,
175 protocols=protocols)
176
177 def __str__(self):
178 return self.msg
179
180
181class DatabaseConnectionException(DodaiDatabaseConnectionConfigurationError):
182 """Exception raised for missing database connection parameters
183
184 Attributes:
185 section_name: The section of the config file that contains
186 the invalid connection information
187 options_required: A dictionary containing the database_type
188 as the key and a list of required options
189 as the value
190
191 """
192 MESSAGE = "The '{section_name}' section does not contain all of the "\
193 "correct information needed to make a database connection."
194 MSG_TYPE = "To make a '{database_type}' connection please make sure "\
195 "To have all of the following options: {options}."
196 MSG_END = "Please remember that the option names are case sensitive."
197
198 def __init__(self, section_name, validators):
199 self.section_name = section_name
200 self.validators = validators
201 self.msg = self._build_message()
202
203 def _build_message(self):
204 out = []
205 out.append(self.MESSAGE.format(section_name=self.section_name))
206 for validator in self.validators:
207 options = list_to_english(validator.REQUIRED)
208 out.append(self.MSG_TYPE.format(database_type=validator.DB_TYPE,
209 options=options))
210 out.append(self.MSG_END)
211 return ' '.join(out)
212
213 def __str__(self):
214 return self.msg
215
216
217class UnknownDatabaseConnectionException(
218 DodaiDatabaseConnectionConfigurationError):
219 """Exception raised for missing database connection parameters
220
221 Attributes:
222 section: The requested section of the config file that can not
223 be found.
224
225 """
226 MESSAGE = "Unable to find the '{section}' section to create a "\
227 "database connection."
228
229 def __init__(self, section):
230 self.section = section
231 self.msg = self._build_message()
232
233 def _build_message(self):
234 return self.MESSAGE.format(section=self.section)
235
236 def __str__(self):
237 return self.msg
238
239
240class InvalidConfigParser(DodaiException):
241 """Exception raised when an invalid parser is registered in the
242 dodai.config.ConfigFiles object.
243
244 """
245 MESSAGE = "The parser object '{name}' that you were trying to register "\
246 "is not a valid parser object. Please make sure that this "\
247 "object contains all of the following methods: {methods}"
248
249 def __init__(self, required_methods, name):
250 self.msg = self._build_message(required_methods, name)
251
252 def _build_message(self, required_methods, name):
253 methods = quote_list(required_methods)
254 return self.MESSAGE.format(methods=methods, name=name)
255
256 def __str__(self):
257 return self.msg
258
259
260class FileDoesNotExist(DodaiException):
261 """Exception raised when a file does not exist.
262
263 """
264 MESSAGE = "The file: '{file_}' does not exist."
265
266 def __init__(self, filepath):
267 self.msg = self._build_message(filepath)
268
269 def _build_message(self, filepath):
270 return self.MESSAGE.format(file_=filepath)
271
272 def __str__(self):
273 return self.msg
274
275
276class FileIsDirectory(DodaiException):
277 """Exception raised when a file is a directory.
278
279 """
280 MESSAGE = "The file: '{file_}' is a directory."
281
282 def __init__(self, filepath):
283 self.msg = self._build_message(filepath)
284
285 def _build_message(self, filepath):
286 return self.MESSAGE.format(file_=filepath)
287
288 def __str__(self):
289 return self.msg