|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | +from typing import cast |
| 5 | + |
| 6 | +from pendulum.datetime import DateTime |
| 7 | +from pendulum.utils._compat import PYPY |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from types import TracebackType |
| 11 | + |
| 12 | + |
| 13 | +class BaseTraveller: |
| 14 | + def __init__(self, datetime_class: type[DateTime] = DateTime) -> None: |
| 15 | + self._datetime_class: type[DateTime] = datetime_class |
| 16 | + |
| 17 | + def freeze(self: BaseTraveller) -> BaseTraveller: |
| 18 | + raise NotImplementedError() |
| 19 | + |
| 20 | + def travel_back(self: BaseTraveller) -> BaseTraveller: |
| 21 | + raise NotImplementedError() |
| 22 | + |
| 23 | + def travel( |
| 24 | + self, |
| 25 | + years: int = 0, |
| 26 | + months: int = 0, |
| 27 | + weeks: int = 0, |
| 28 | + days: int = 0, |
| 29 | + hours: int = 0, |
| 30 | + minutes: int = 0, |
| 31 | + seconds: int = 0, |
| 32 | + microseconds: int = 0, |
| 33 | + ) -> BaseTraveller: |
| 34 | + raise NotImplementedError() |
| 35 | + |
| 36 | + def travel_to(self, dt: DateTime) -> BaseTraveller: |
| 37 | + raise NotImplementedError() |
| 38 | + |
| 39 | + |
| 40 | +if not PYPY: |
| 41 | + import time_machine |
| 42 | + |
| 43 | + class Traveller(BaseTraveller): |
| 44 | + def __init__(self, datetime_class: type[DateTime] = DateTime) -> None: |
| 45 | + super().__init__(datetime_class) |
| 46 | + |
| 47 | + self._started: bool = False |
| 48 | + self._traveller: time_machine.travel | None = None |
| 49 | + self._coordinates: time_machine.Coordinates | None = None |
| 50 | + |
| 51 | + def freeze(self) -> Traveller: |
| 52 | + if self._started: |
| 53 | + cast(time_machine.Coordinates, self._coordinates).move_to( |
| 54 | + self._datetime_class.now(), tick=False |
| 55 | + ) |
| 56 | + else: |
| 57 | + self._start(freeze=True) |
| 58 | + |
| 59 | + return self |
| 60 | + |
| 61 | + def travel_back(self) -> Traveller: |
| 62 | + if not self._started: |
| 63 | + return self |
| 64 | + |
| 65 | + cast(time_machine.travel, self._traveller).stop() |
| 66 | + self._coordinates = None |
| 67 | + self._traveller = None |
| 68 | + self._started = False |
| 69 | + |
| 70 | + return self |
| 71 | + |
| 72 | + def travel( |
| 73 | + self, |
| 74 | + years: int = 0, |
| 75 | + months: int = 0, |
| 76 | + weeks: int = 0, |
| 77 | + days: int = 0, |
| 78 | + hours: int = 0, |
| 79 | + minutes: int = 0, |
| 80 | + seconds: int = 0, |
| 81 | + microseconds: int = 0, |
| 82 | + *, |
| 83 | + freeze: bool = False, |
| 84 | + ) -> Traveller: |
| 85 | + self._start(freeze=freeze) |
| 86 | + |
| 87 | + cast(time_machine.Coordinates, self._coordinates).move_to( |
| 88 | + self._datetime_class.now().add( |
| 89 | + years=years, |
| 90 | + months=months, |
| 91 | + weeks=weeks, |
| 92 | + days=days, |
| 93 | + hours=hours, |
| 94 | + minutes=minutes, |
| 95 | + seconds=seconds, |
| 96 | + microseconds=microseconds, |
| 97 | + ) |
| 98 | + ) |
| 99 | + |
| 100 | + return self |
| 101 | + |
| 102 | + def travel_to(self, dt: DateTime, *, freeze: bool = False) -> Traveller: |
| 103 | + self._start(freeze=freeze) |
| 104 | + |
| 105 | + cast(time_machine.Coordinates, self._coordinates).move_to(dt) |
| 106 | + |
| 107 | + return self |
| 108 | + |
| 109 | + def _start(self, freeze: bool = False) -> None: |
| 110 | + if self._started: |
| 111 | + return |
| 112 | + |
| 113 | + if not self._traveller: |
| 114 | + self._traveller = time_machine.travel( |
| 115 | + self._datetime_class.now(), tick=not freeze |
| 116 | + ) |
| 117 | + |
| 118 | + self._coordinates = self._traveller.start() |
| 119 | + |
| 120 | + self._started = True |
| 121 | + |
| 122 | + def __enter__(self) -> Traveller: |
| 123 | + self._start() |
| 124 | + |
| 125 | + return self |
| 126 | + |
| 127 | + def __exit__( |
| 128 | + self, |
| 129 | + exc_type: type[BaseException] | None, |
| 130 | + exc_val: BaseException | None, |
| 131 | + exc_tb: TracebackType, |
| 132 | + ) -> None: |
| 133 | + self.travel_back() |
| 134 | + |
| 135 | +else: |
| 136 | + |
| 137 | + class Traveller(BaseTraveller): # type: ignore[no-redef] |
| 138 | + |
| 139 | + ... |
0 commit comments