From: Frank Brehm Date: Fri, 4 Aug 2017 09:57:56 +0000 (+0200) Subject: Adding some new error classes X-Git-Tag: 0.1.2~143 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=8147630804aa9e49d230784ab25dede99cc5066d;p=pixelpark%2Fadmin-tools.git Adding some new error classes --- diff --git a/pp_lib/errors.py b/pp_lib/errors.py index d4bd6dd..2a566e7 100644 --- a/pp_lib/errors.py +++ b/pp_lib/errors.py @@ -6,10 +6,10 @@ """ # Standard modules -# import errno +import errno -__version__ = '0.3.2' +__version__ = '0.4.1' # ============================================================================= class PpError(Exception): @@ -83,6 +83,116 @@ class FunctionNotImplementedError(PpError, NotImplementedError): 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) + # =============================================================================