stop.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright 2016–2021 Julien Danjou
  2. # Copyright 2016 Joshua Harlow
  3. # Copyright 2013-2014 Ray Holder
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import abc
  17. import typing
  18. if typing.TYPE_CHECKING:
  19. import threading
  20. from pip._vendor.tenacity import RetryCallState
  21. class stop_base(abc.ABC):
  22. """Abstract base class for stop strategies."""
  23. @abc.abstractmethod
  24. def __call__(self, retry_state: "RetryCallState") -> bool:
  25. pass
  26. def __and__(self, other: "stop_base") -> "stop_all":
  27. return stop_all(self, other)
  28. def __or__(self, other: "stop_base") -> "stop_any":
  29. return stop_any(self, other)
  30. class stop_any(stop_base):
  31. """Stop if any of the stop condition is valid."""
  32. def __init__(self, *stops: stop_base) -> None:
  33. self.stops = stops
  34. def __call__(self, retry_state: "RetryCallState") -> bool:
  35. return any(x(retry_state) for x in self.stops)
  36. class stop_all(stop_base):
  37. """Stop if all the stop conditions are valid."""
  38. def __init__(self, *stops: stop_base) -> None:
  39. self.stops = stops
  40. def __call__(self, retry_state: "RetryCallState") -> bool:
  41. return all(x(retry_state) for x in self.stops)
  42. class _stop_never(stop_base):
  43. """Never stop."""
  44. def __call__(self, retry_state: "RetryCallState") -> bool:
  45. return False
  46. stop_never = _stop_never()
  47. class stop_when_event_set(stop_base):
  48. """Stop when the given event is set."""
  49. def __init__(self, event: "threading.Event") -> None:
  50. self.event = event
  51. def __call__(self, retry_state: "RetryCallState") -> bool:
  52. return self.event.is_set()
  53. class stop_after_attempt(stop_base):
  54. """Stop when the previous attempt >= max_attempt."""
  55. def __init__(self, max_attempt_number: int) -> None:
  56. self.max_attempt_number = max_attempt_number
  57. def __call__(self, retry_state: "RetryCallState") -> bool:
  58. return retry_state.attempt_number >= self.max_attempt_number
  59. class stop_after_delay(stop_base):
  60. """Stop when the time from the first attempt >= limit."""
  61. def __init__(self, max_delay: float) -> None:
  62. self.max_delay = max_delay
  63. def __call__(self, retry_state: "RetryCallState") -> bool:
  64. return retry_state.seconds_since_start >= self.max_delay