+++ /dev/null
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-"""
-@author: Frank Brehm
-@summary: module for some common used error classes
-"""
-
-# Standard modules
-import errno
-
-
-__version__ = '1.0.0'
-
-# =============================================================================
-class PpError(Exception):
- """
- Base error class for all other self defined exceptions.
- """
-
- pass
-
-
-# =============================================================================
-class PpAppError(PpError):
-
- pass
-
-
-# =============================================================================
-class InvalidMailAddressError(PpError):
- """Class for a exception in case of a malformed mail address."""
-
- # -------------------------------------------------------------------------
- def __init__(self, address, msg=None):
-
- self.address = address
- self.msg = msg
-
- # -------------------------------------------------------------------------
- def __str__(self):
-
- msg = "Wrong mail address {a!r} ({c})".format(
- a=self.address, c=self.address.__class__.__name__)
- if self.msg:
- msg += ': ' + self.msg
- else:
- msg += '.'
- return msg
-
-
-# =============================================================================
-class FunctionNotImplementedError(PpError, NotImplementedError):
- """
- Error class for not implemented functions.
- """
-
- # -------------------------------------------------------------------------
- def __init__(self, function_name, class_name):
- """
- Constructor.
-
- @param function_name: the name of the not implemented function
- @type function_name: str
- @param class_name: the name of the class of the function
- @type class_name: str
-
- """
-
- self.function_name = function_name
- if not function_name:
- self.function_name = '__unkown_function__'
-
- self.class_name = class_name
- if not class_name:
- self.class_name = '__unkown_class__'
-
- # -------------------------------------------------------------------------
- def __str__(self):
- """
- Typecasting into a string for error output.
- """
-
- msg = "Function {func}() has to be overridden in class {cls!r}."
- return msg.format(func=self.function_name, cls=self.class_name)
-
-# =============================================================================
-class IoTimeoutError(PpError, IOError):
- """
- Special error class indicating a timout error on a read/write operation
- """
-
- # -------------------------------------------------------------------------
- def __init__(self, strerror, timeout, filename=None):
- """
- Constructor.
-
- @param strerror: the error message about the operation
- @type strerror: str
- @param timeout: the timout in seconds leading to the error
- @type timeout: float
- @param filename: the filename leading to the error
- @type filename: str
-
- """
-
- t_o = None
- try:
- t_o = float(timeout)
- except ValueError:
- pass
- self.timeout = t_o
-
- if t_o is not None:
- strerror += " (timeout after {:0.1f} secs)".format(t_o)
-
- if filename is None:
- super(IoTimeoutError, self).__init__(errno.ETIMEDOUT, strerror)
- else:
- super(IoTimeoutError, self).__init__(
- errno.ETIMEDOUT, strerror, filename)
-
-# =============================================================================
-class ReadTimeoutError(IoTimeoutError):
- """
- Special error class indicating a timout error on reading of a file.
- """
-
- # -------------------------------------------------------------------------
- def __init__(self, timeout, filename):
- """
- Constructor.
-
- @param timeout: the timout in seconds leading to the error
- @type timeout: float
- @param filename: the filename leading to the error
- @type filename: str
-
- """
-
- strerror = "Timeout error on reading"
- super(ReadTimeoutError, self).__init__(strerror, timeout, filename)
-
-
-# =============================================================================
-class WriteTimeoutError(IoTimeoutError):
- """
- Special error class indicating a timout error on a writing into a file.
- """
-
- # -------------------------------------------------------------------------
- def __init__(self, timeout, filename):
- """
- Constructor.
-
- @param timeout: the timout in seconds leading to the error
- @type timeout: float
- @param filename: the filename leading to the error
- @type filename: str
-
- """
-
- strerror = "Timeout error on writing"
- super(WriteTimeoutError, self).__init__(strerror, timeout, filename)
-
-# =============================================================================
-class CouldntOccupyLockfileError(PpError):
- """
- Special error class indicating, that a lockfile couldn't coccupied
- after a defined time.
- """
-
- # -----------------------------------------------------
- def __init__(self, lockfile, duration, tries):
- """
- Constructor.
-
- @param lockfile: the lockfile, which could't be occupied.
- @type lockfile: str
- @param duration: The duration in seconds, which has lead to this situation
- @type duration: float
- @param tries: the number of tries creating the lockfile
- @type tries: int
-
- """
-
- self.lockfile = str(lockfile)
- self.duration = float(duration)
- self.tries = int(tries)
-
- # -----------------------------------------------------
- def __str__(self):
-
- return "Couldn't occupy lockfile {!r} in {:0.1f} seconds with {} tries.".format(
- self.lockfile, self.duration, self.tries)
-
-
-# =============================================================================
-class CommandNotFoundError(PpError):
- """
- Special exception, if one ore more OS commands were not found.
-
- """
-
- # -------------------------------------------------------------------------
- def __init__(self, cmd_list):
- """
- Constructor.
-
- @param cmd_list: all not found OS commands.
- @type cmd_list: list
-
- """
-
- self.cmd_list = None
- if cmd_list is None:
- self.cmd_list = ["Unknown OS command."]
- elif isinstance(cmd_list, list):
- self.cmd_list = cmd_list
- else:
- self.cmd_list = [cmd_list]
-
- if len(self.cmd_list) < 1:
- raise ValueError("Empty command list given.")
-
- # -------------------------------------------------------------------------
- def __str__(self):
- """
- Typecasting into a string for error output.
- """
-
- cmds = ', '.join([("'" + str(x) + "'") for x in self.cmd_list])
- msg = "Could not found OS command"
- if len(self.cmd_list) != 1:
- msg += 's'
- msg += ": " + cmds
- return msg
-
-
-# =============================================================================
-
-if __name__ == "__main__":
- pass
-
-# =============================================================================
-
-# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4