]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Applying flake8 to lib/webhooks/lock_handler.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 14 Sep 2018 14:52:29 +0000 (16:52 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 14 Sep 2018 14:52:29 +0000 (16:52 +0200)
lib/webhooks/__init__.py
lib/webhooks/lock_handler.py

index 1184a560d6ab7f694137c45492d819fc2aabe968..77a1597213a6db2c8c221434c1ddb384ff82a0db 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/env python3
 # -*- coding: utf-8 -*-
 
-__version__ = '1.1.1'
+__version__ = '1.1.2'
 
 # vim: ts=4 et list
index 8445c38eea78fb256c82048eda48eaebeadde843..bf5329fd30aeea6c3ca7d3bc23018c7b12cc94dc 100644 (file)
@@ -33,7 +33,7 @@ from .obj import BaseObject
 
 from .handler import BaseHandlerError, BaseHandler
 
-__version__ = '0.2.4'
+__version__ = '0.3.1'
 
 LOG = logging.getLogger(__name__)
 
@@ -656,6 +656,31 @@ class LockHandler(BaseHandler):
         out += ")>"
         return out
 
+    # -------------------------------------------------------------------------
+    def check_for_number(self, value, default, what, must_gt_zero=False, must_ge_zero=False):
+
+        if value is None:
+            return default
+
+        if not isinstance(value, Number):
+            msg = "Value {val!r} for {what} is not a Number.".format(
+                val=value, what=what)
+            raise LockHandlerError(msg)
+
+        if must_gt_zero and value <= 0:
+            msg = "The value for {what} must be greater than zero (is {val!r}).".format(
+                val=value, what=what)
+            raise LockHandlerError(msg)
+
+        if must_ge_zero and value < 0:
+            msg = (
+                "The value for {what} must be greater than "
+                "or equal to zero (is {val!r}).").format(
+                val=value, what=what)
+            raise LockHandlerError(msg)
+
+        return value
+
     # -------------------------------------------------------------------------
     def create_lockfile(
         self, lockfile, delay_start=None, delay_increase=None, max_delay=None,
@@ -708,61 +733,26 @@ class LockHandler(BaseHandler):
 
         """
 
-        if delay_start is None:
-            delay_start = self.lockretry_delay_start
-        else:
-            if not isinstance(delay_start, Number):
-                msg = "Value {val!r} for {what} is not a Number.".format(
-                    val=delay_start, what='delay_start')
-                raise LockHandlerError(msg)
-            if delay_start <= 0:
-                msg = "The value for {what} must be greater than zero (is {val!r}).".format(
-                    val=delay_start, what='delay_start')
-                raise LockHandlerError(msg)
+        delay_start = self.check_for_number(
+            delay_start, self.lockretry_delay_start,
+            what='delay_start', must_gt_zero=True)
 
-        if delay_increase is None:
-            delay_increase = self.lockretry_delay_increase
-        else:
-            if not isinstance(delay_increase, Number):
-                msg = "Value {val!r} for {what} is not a Number.".format(
-                    val=delay_increase, what='delay_increase')
-                raise LockHandlerError(msg)
-            if delay_increase < 0:
-                msg = (
-                    "The value for {what} must be greater than "
-                    "or equal to zero (is {val!r}).").format(
-                    val=delay_increase, what='delay_increase')
-                raise LockHandlerError(msg)
+        delay_increase = self.check_for_number(
+            delay_increase, self.lockretry_delay_increase,
+            what='delay_increase', must_ge_zero=True)
 
-        if max_delay is None:
-            max_delay = self.lockretry_max_delay
-        else:
-            if not isinstance(max_delay, Number):
-                msg = "Value {val!r} for {what} is not a Number.".format(
-                    val=max_delay, what='max_delay')
-                raise LockHandlerError(msg)
-            if max_delay <= 0:
-                msg = "The value for {what} must be greater than zero (is {val!r}).".format(
-                    val=max_delay, what='max_delay')
-                raise LockHandlerError(msg)
-            pass
+        max_delay = self.check_for_number(
+            max_delay, self.lockretry_max_delay,
+            what='max_delay', must_ge_zero=True)
 
         if use_pid is None:
             use_pid = self.locking_use_pid
         else:
             use_pid = bool(use_pid)
 
-        if max_age is None:
-            max_age = self.max_lockfile_age
-        else:
-            if not isinstance(max_age, Number):
-                msg = "Value {val!r} for {what} is not a Number.".format(
-                    val=max_age, what='max_age')
-                raise LockHandlerError(msg)
-            if max_age <= 0:
-                msg = "The value for {what} must be greater than zero (is {val!r}).".format(
-                    val=max_age, what='max_age')
-                raise LockHandlerError(msg)
+        max_age = self.check_for_number(
+            max_age, self.max_lockfile_age,
+            what='max_age', must_ge_zero=True)
 
         if pid is None:
             pid = os.getpid()
@@ -797,6 +787,16 @@ class LockHandler(BaseHandler):
         else:
             stay_opened = bool(stay_opened)
 
+        return self._do_create_lockfile(
+            lockfile=lockfile, delay_start=delay_start, max_delay=max_delay, max_age=max_age,
+            delay_increase=delay_increase, pid=pid, use_pid=use_pid, raise_on_fail=raise_on_fail,
+            stay_opened=stay_opened)
+
+    # -------------------------------------------------------------------------
+    def _do_create_lockfile(
+        self, lockfile, delay_start, max_delay, max_age, delay_increase,
+            pid, use_pid, raise_on_fail, stay_opened):
+
         counter = 0
         delay = delay_start
 
@@ -863,7 +863,7 @@ class LockHandler(BaseHandler):
                 if raise_on_fail:
                     raise e
                 else:
-                    LOG.error(msg)
+                    LOG.error(str(e))
                 return None
 
             # or an int for success