From: Frank Brehm Date: Fri, 14 Sep 2018 14:52:29 +0000 (+0200) Subject: Applying flake8 to lib/webhooks/lock_handler.py X-Git-Tag: 1.6.4^2~114 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=3c2be3b3cca8da9f025d37ebc3ea131c024f6b31;p=pixelpark%2Fpuppetmaster-webhooks.git Applying flake8 to lib/webhooks/lock_handler.py --- diff --git a/lib/webhooks/__init__.py b/lib/webhooks/__init__.py index 1184a56..77a1597 100644 --- a/lib/webhooks/__init__.py +++ b/lib/webhooks/__init__.py @@ -1,6 +1,6 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '1.1.1' +__version__ = '1.1.2' # vim: ts=4 et list diff --git a/lib/webhooks/lock_handler.py b/lib/webhooks/lock_handler.py index 8445c38..bf5329f 100644 --- a/lib/webhooks/lock_handler.py +++ b/lib/webhooks/lock_handler.py @@ -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