|
3 | 3 | import traceback, signal as _signal
|
4 | 4 | from threading import Event
|
5 | 5 | from time import monotonic, sleep
|
6 |
| -from typing import Callable, Dict, Any, Iterator, Iterable, Optional, Union, cast |
| 6 | +from typing import Callable, Dict, Iterator, Iterable, Optional, Union, Any, cast |
7 | 7 | # Third party modules
|
8 | 8 | # Local modules
|
9 |
| -from .abcs import T_Signal, T_Locker, T_Lock |
| 9 | +from .abcs import T_Signal, T_Locker, T_Lock, T |
10 | 10 | # Program
|
11 | 11 | class KillSignal(Exception): pass
|
12 | 12 |
|
13 |
| -class SignalIterator(Iterator[Any]): |
| 13 | +class SignalIterator(Iterator[T]): |
14 | 14 | __slots__ = ("event", "it", "checkDelay", "lastCheck")
|
15 | 15 | event:Event
|
16 |
| - it:Iterator[Any] |
| 16 | + it:Iterator[T] |
17 | 17 | checkDelay:float
|
18 | 18 | lastCheck:float
|
19 |
| - def __init__(self, event:Event, it:Iterable[Any], checkDelay:float=1.0): |
| 19 | + def __init__(self, event:Event, it:Iterable[T], checkDelay:float=1.0): |
20 | 20 | self.event = event
|
21 | 21 | self.it = it.__iter__()
|
22 | 22 | self.checkDelay = checkDelay
|
23 | 23 | self.lastCheck = monotonic()
|
24 |
| - def __iter__(self) -> Iterator[Any]: |
| 24 | + def __iter__(self) -> Iterator[T]: |
25 | 25 | return self
|
26 |
| - def __next__(self) -> Any: |
| 26 | + def __next__(self) -> T: |
27 | 27 | m = monotonic()
|
28 | 28 | if m-self.lastCheck > self.checkDelay:
|
29 | 29 | self.lastCheck = m
|
@@ -56,8 +56,9 @@ def locked(self) -> bool:
|
56 | 56 | self.lock.release()
|
57 | 57 | return False
|
58 | 58 | return True
|
59 |
| - def __enter__(self) -> Any: |
| 59 | + def __enter__(self) -> ExtendedLocker: |
60 | 60 | self.lock.acquire()
|
| 61 | + return self |
61 | 62 | def __exit__(self, type:Any, value:Any, traceback:Any) -> Any:
|
62 | 63 | self.lock.release()
|
63 | 64 |
|
@@ -119,7 +120,7 @@ def signalSoftKill(self, *args:Any, **kwargs:Any) -> None:
|
119 | 120 | def signalHardKill(self, *args:Any, **kwargs:Any) -> None:
|
120 | 121 | if isinstance(Signal._handler, Signal):
|
121 | 122 | return Signal._handler._signalHardKill(*args, **kwargs)
|
122 |
| - def iter(self, it:Iterable[Any], checkDelay:float=1.0) -> Iterable[Any]: |
| 123 | + def iter(self, it:Iterable[T], checkDelay:float=1.0) -> Iterable[T]: |
123 | 124 | if isinstance(Signal._handler, Signal):
|
124 | 125 | return Signal._handler._iter(it, checkDelay, self._force)
|
125 | 126 | return it
|
@@ -201,7 +202,7 @@ def _sleep(self, seconds:Union[int, float], raiseOnKill:bool=False, force:bool=T
|
201 | 202 | if (self.eHard if force else self.eSoft).wait(float(seconds)) and raiseOnKill:
|
202 | 203 | raise KillSignal
|
203 | 204 | return None
|
204 |
| - def _iter(self, it:Iterable[Any], checkDelay:float=1.0, force:bool=True) -> Iterator[Any]: |
| 205 | + def _iter(self, it:Iterable[T], checkDelay:float=1.0, force:bool=True) -> Iterator[T]: |
205 | 206 | return SignalIterator(self.eHard if force else self.eSoft, it, checkDelay)
|
206 | 207 | def _warpLock(self, lock:Any, force:bool=True) -> T_Locker:
|
207 | 208 | return SignalLocker(self.eHard if force else self.eSoft, cast(T_Lock, lock))
|
|
0 commit comments