-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_abc.py
More file actions
86 lines (60 loc) · 1.98 KB
/
Copy path_abc.py
File metadata and controls
86 lines (60 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Generic, TypeVar
if TYPE_CHECKING:
from types import TracebackType
from typing_extensions import Self
TValue = TypeVar("TValue")
class Result(Generic[TValue]):
"""Result wrapper."""
__slots__ = ("_value",)
def __init__(self, value: TValue) -> None:
self._value = value
def __repr__(self) -> str:
return f"Result({self._value!r})"
@property
def value(self) -> TValue:
"""Return the inner value."""
return self._value
class BaseLock(ABC):
@abstractmethod
def __enter__(self) -> Self: ...
@abstractmethod
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None: ...
class BaseAsyncLock(ABC):
@abstractmethod
async def __aenter__(self) -> Self: ...
@abstractmethod
async def __aexit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None: ...
TLock = TypeVar("TLock", bound=BaseLock)
TAsyncLock = TypeVar("TAsyncLock", bound=BaseAsyncLock)
class BaseStorage(ABC, Generic[TValue, TLock]):
@abstractmethod
def lock(self, key: str) -> TLock:
"""Return lock for the key."""
@abstractmethod
def get(self, key: str) -> Result[TValue] | None:
"""Get value by key."""
@abstractmethod
def set(self, key: str, value: TValue) -> None:
"""Set value by key."""
class BaseAsyncStorage(ABC, Generic[TValue, TAsyncLock]):
@abstractmethod
def lock(self, key: str) -> TAsyncLock:
"""Return lock for the key."""
@abstractmethod
async def aget(self, key: str) -> Result[TValue] | None:
"""Get value by key async."""
@abstractmethod
async def aset(self, key: str, value: TValue) -> None:
"""Set value by key async."""