Exemplo n.º 1
0
    def __init__(
        self, interval: Optional[DurationLiteral] = 60, instant_run: bool = False, **kwargs: Any
    ):
        super().__init__(**kwargs)

        self._assert_polling_compat()

        if interval is None:
            # No scheduled execution. Use endpoint `/trigger` of api to execute.
            self._poll_interval = None
            self.interval = None
            self.is_cron = False
        else:
            try:
                # Literals such as 60s, 1m, 1h, ...
                self._poll_interval = parse_duration_literal(interval)
                self.interval = interval
                self.is_cron = False
            except TypeError:
                # ... or a cron-like expression is valid
                from cronex import CronExpression  # type: ignore
                self._cron_interval = CronExpression(interval)
                self.interval = self._cron_interval
                self.is_cron = True

        self._is_running = False
        self._scheduler: Optional[Scheduler] = None
        self._instant_run = try_parse_bool(instant_run, False)
Exemplo n.º 2
0
 def __init__(self,
              reset_retry_threshold: DurationLiteral = 60,
              **kwargs: Any):
     super().__init__(**kwargs)
     # Reset retry_count after x seconds of successful running
     self.reset_retry_threshold = parse_duration_literal(
         reset_retry_threshold)
     self.last_error = None  # type: Optional[datetime]
Exemplo n.º 3
0
    def __init__(self, throttle: Optional[DurationLiteral] = None, **kwargs: Any):
        """
        Initializer.

        Args:
            throttle: If set to a valid duration literal (e.g. 5m) the return value of the
              called functions will be cached for the given amount of time.
        """
        super().__init__(**kwargs)
        self.throttle = throttle and parse_duration_literal(throttle)
        self._cache = None
        if self.throttle:
            self._cache = cachetools.TTLCache(self.MAX_CACHE_SIZE, ttl=self.throttle)
Exemplo n.º 4
0
 def __init__(self,
              command,
              args=None,
              cwd=None,
              capture=True,
              timeout="5s",
              **kwargs):
     super().__init__(**kwargs)
     self._command = str(command) if command else None
     self._args = self._parse_args(args)
     self._cwd = str(cwd) if cwd else self.base_path
     if self._cwd:
         validator.is_directory(cwd=self._cwd)
     self._capture = bool(capture)
     self._timeout = timeout and parse_duration_literal(timeout)
Exemplo n.º 5
0
def test_callback_from_str():
    res = gpio.Callback.from_str("2")
    assert isinstance(res, gpio.RisingCallback)
    assert res.gpio_pin == 2

    res = gpio.Callback.from_str("2", default=gpio.CONST_FALLING)
    assert isinstance(res, gpio.FallingCallback)
    assert res.gpio_pin == 2

    res = gpio.Callback.from_str("2:rising", default=gpio.CONST_FALLING)
    assert isinstance(res, gpio.RisingCallback)
    assert res.gpio_pin == 2

    res = gpio.Callback.from_str("2:falling", default=gpio.CONST_RISING)
    assert isinstance(res, gpio.FallingCallback)
    assert res.gpio_pin == 2

    res = gpio.Callback.from_str("2:switch", default=gpio.CONST_RISING)
    assert isinstance(res, gpio.SwitchCallback)
    assert res.gpio_pin == 2
    assert res.delay == gpio.SwitchCallback.BOUNCE_DEFAULT

    res = gpio.Callback.from_str("2:switch(999)", default=gpio.CONST_RISING)
    assert isinstance(res, gpio.SwitchCallback)
    assert res.gpio_pin == 2
    assert res.delay == 999

    try:
        gpio.Callback.from_str("2:switch()", default=gpio.CONST_RISING)
    except ValueError:
        pass

    res = gpio.Callback.from_str("2:motion", default=gpio.CONST_RISING)
    assert isinstance(res, gpio.MotionCallback)
    assert res.gpio_pin == 2
    assert res.delay == parse_duration_literal(
        gpio.MotionCallback.DELAY_DEFAULT)

    res = gpio.Callback.from_str("2:motion(2m)", default=gpio.CONST_RISING)
    assert isinstance(res, gpio.MotionCallback)
    assert res.gpio_pin == 2
    assert res.delay == 120

    try:
        gpio.Callback.from_str("2:motion()", default=gpio.CONST_RISING)
    except ValueError:
        pass
Exemplo n.º 6
0
 def __init__(self, gpio_pin: str, delay: str):
     super().__init__(gpio_pin)
     self.delay = parse_duration_literal(delay)
     self._debouncer = None  # type: Optional[Debounce]
Exemplo n.º 7
0
 def __init__(self, retry_wait: DurationLiteral = 60, **kwargs: Any):
     super().__init__(**kwargs)
     self.retry_wait = parse_duration_literal(retry_wait)
     self.retry_count = 0
Exemplo n.º 8
0
 def _wait_for_duration_literal_converter(cls, value: Any) -> int:
     typeguard.check_type('wait_for', value,
                          DurationLiteral)  # type: ignore
     return parse_duration_literal(value)