# 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 . 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