from .handler import BaseHandlerError, BaseHandler
-__version__ = '0.2.4'
+__version__ = '0.3.1'
LOG = logging.getLogger(__name__)
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,
"""
- 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()
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
if raise_on_fail:
raise e
else:
- LOG.error(msg)
+ LOG.error(str(e))
return None
# or an int for success