]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Adding some new error classes
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 4 Aug 2017 09:57:56 +0000 (11:57 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 4 Aug 2017 09:57:56 +0000 (11:57 +0200)
pp_lib/errors.py

index d4bd6dd5fc1f4ef41ac6081d55d0a2268fa68984..2a566e7566945ab4881e1ded0a460c08cbb3315b 100644 (file)
@@ -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)
+
 
 # =============================================================================