aboutsummaryrefslogtreecommitdiff
path: root/dodai/exception.py
diff options
context:
space:
mode:
Diffstat (limited to 'dodai/exception.py')
-rw-r--r--dodai/exception.py236
1 files changed, 236 insertions, 0 deletions
diff --git a/dodai/exception.py b/dodai/exception.py
new file mode 100644
index 0000000..5af10b4
--- /dev/null
+++ b/dodai/exception.py
@@ -0,0 +1,236 @@
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
19
20class DodaiException(Exception):
21
22 def _system_encoding(self):
23 # Returns the character encoding of the operating system
24
25 encoding = sys.getdefaultencoding()
26 filesystem_encoding = sys.getfilesystemencoding()
27 if filesystem_encoding:
28 encoding = filesystem_encoding
29 return encoding
30
31
32class HimoAsciiError(DodaiException):
33 """Exception raised when the Himo object can't convert a character
34 down to it's root character.
35
36 Attributes:
37 char: The character that can't be converted down to ascii
38 """
39 MESSAGE="Unable to convert the '{char}' character to ascii"
40
41 def __init__(self, char):
42 self.char = char
43 self.msg = self._build_message()
44
45 def _build_message(self):
46 encoding = self._system_encoding()
47 try:
48 char = self.char.encode(encoding)
49 except UnicodeEncodeError:
50 char = 'unichr({0})'.format(ord(self.char))
51 return self.MESSAGE.format(char=self.char)
52
53 def __str__(self):
54 return self.msg
55
56class DodaiDatabaseConnectionConfigurationError(DodaiException):
57 pass
58
59
60class DatabaseEmptyOptionException(DodaiDatabaseConnectionConfigurationError):
61 """Exception raised for an empty option in the config
62
63 Attributes:
64 section_name: The section of the config file that contains
65 the invalid protocol
66 option_name: The name of the empty option
67
68 """
69 MESSAGE = "In the '{section_name}' section, the '{option_name}' "\
70 "was not set or is missing. Please set this option."
71
72 def __init__(self, section_name, option_name):
73 self.section_name = section_name
74 self.option_name = option_name
75 self.msg = self._build_message()
76
77 def _build_message(self):
78 return self.MESSAGE.format(section_name=self.section_name,
79 option_name=self.option_name)
80
81 def __str__(self):
82 return self.msg
83
84
85class DatabasePortException(DodaiDatabaseConnectionConfigurationError):
86 """Exception raised for invalid database port connection
87
88 Attributes:
89 section_name: The section of the config file that contains
90 the invalid protocol
91 section_port: The port value that was listed in the
92 config file
93
94 """
95 MESSAGE = "In the '{section_name}' section, the port of "\
96 "'{section_port}' is invalid. The port must be a "\
97 "number between 1 and 65535"
98
99 def __init__(self, section_name, section_port):
100 self.section_name = section_name
101 self.section_port = section_port
102 self.msg = self._build_message()
103
104 def _build_message(self):
105 return self.MESSAGE.format(section_name=self.section_name,
106 section_port=self.section_port)
107
108 def __str__(self):
109 return self.msg
110
111
112class DatabaseHostnameException(DodaiDatabaseConnectionConfigurationError):
113 """Exception raised for invalid database hostname
114
115 Attributes:
116 section_name: The section of the config file that contains
117 the invalid protocol
118 section_hostname: The hostname value that was listed in the
119 config file
120
121 """
122 MESSAGE = "In the '{section_name}' section, the hostname of "\
123 "'{section_hostname}' is invalid. Please use a valid "\
124 "hostname."
125
126 MSG_NON = "In the '{section_name}' section, the hostname was "\
127 "not set. Please set the hostname."
128
129 def __init__(self, section_name, section_hostname):
130 self.section_name = section_name
131 self.section_hostname = section_hostname
132 self.msg = self._build_message()
133
134 def _build_message(self):
135 if self.section_hostname:
136 return self.MESSAGE.format(section_name=self.section_name,
137 section_hostname=self.section_hostname)
138 else:
139 return self.MSG_NON.format(section_name=self.section_name)
140
141 def __str__(self):
142 return self.msg
143
144
145class DatabaseProtocolException(DodaiDatabaseConnectionConfigurationError):
146 """Exception raised for invalid database connection protocols
147
148 Attributes:
149 section_name: The section of the config file that contains
150 the invalid protocol
151 section_protocol: The protocol value that was listed in the
152 config file
153 database_type: Usually 'server' or 'file'
154 protocols: List of valid protocols
155
156 """
157 MESSAGE = "In the '{section_name}' section, the protocol of "\
158 "'{section_protocol}' is invalid. The valid protocols "\
159 "for a '{database_type}' connection are: {protocols}"
160
161 def __init__(self, section_name, section_protocol, database_type,
162 protocols):
163 self.section_name = section_name
164 self.section_protocol = section_protocol
165 self.database_type = database_type
166 self.protocols = protocols
167 self.msg = self._build_message()
168
169 def _build_message(self):
170 protocols = list_to_english(self.protocols)
171 return self.MESSAGE.format(section_name=self.section_name,
172 section_protocol=self.section_protocol,
173 database_type=self.database_type,
174 protocols=protocols)
175
176 def __str__(self):
177 return self.msg
178
179
180class DatabaseConnectionException(DodaiDatabaseConnectionConfigurationError):
181 """Exception raised for missing database connection parameters
182
183 Attributes:
184 section_name: The section of the config file that contains
185 the invalid connection information
186 options_required: A dictionary containing the database_type
187 as the key and a list of required options
188 as the value
189
190 """
191 MESSAGE = "The '{section_name}' section does not contain all of the "\
192 "correct information needed to make a database connection."
193 MSG_TYPE = "To make a '{database_type}' connection please make sure "\
194 "To have all of the following options: {options}."
195 MSG_END = "Please remember that the option names are case sensitive."
196
197 def __init__(self, section_name, validators):
198 self.section_name = section_name
199 self.validators = validators
200 self.msg = self._build_message()
201
202 def _build_message(self):
203 out = []
204 out.append(self.MESSAGE.format(section_name=self.section_name))
205 for validator in self.validators:
206 options = list_to_english(validator.REQUIRED)
207 out.append(self.MSG_TYPE.format(database_type=validator.DB_TYPE,
208 options=options))
209 out.append(self.MSG_END)
210 return ' '.join(out)
211
212 def __str__(self):
213 return self.msg
214
215
216class UnknownDatabaseConnectionException(
217 DodaiDatabaseConnectionConfigurationError):
218 """Exception raised for missing database connection parameters
219
220 Attributes:
221 section: The requested section of the config file that can not
222 be found.
223
224 """
225 MESSAGE = "Unable to find the '{section}' section to create a "\
226 "database connection."
227
228 def __init__(self, section):
229 self.section = section
230 self.msg = self._build_message()
231
232 def _build_message(self):
233 return self.MESSAGE.format(section=self.section)
234
235 def __str__(self):
236 return self.msg