Skip to content

Commit 7f2b70c

Browse files
committed
fix types and rename class
1 parent 2ecdade commit 7f2b70c

File tree

3 files changed

+57
-47
lines changed

3 files changed

+57
-47
lines changed

wsrpc_aiohttp/websocket/abc.py

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import asyncio
2-
from abc import (
3-
ABC, abstractmethod, abstractclassmethod, abstractproperty,
4-
abstractstaticmethod
5-
)
2+
from abc import ABC, abstractmethod
63
from enum import IntEnum
74
from typing import Any, Mapping, Coroutine, Union, Callable, Dict, Tuple
85

@@ -16,7 +13,8 @@ class AbstractWebSocket(ABC):
1613
def __init__(self, request: Request):
1714
raise NotImplementedError(request)
1815

19-
@abstractclassmethod
16+
@classmethod
17+
@abstractmethod
2018
def configure(cls, keepalive_timeout: int,
2119
client_timeout: int,
2220
max_concurrent_requests: int) -> None:
@@ -55,7 +53,8 @@ async def authorize(self) -> bool:
5553
async def __handle_request(self) -> WebSocketResponse:
5654
raise NotImplementedError
5755

58-
@abstractclassmethod
56+
@classmethod
57+
@abstractmethod
5958
def broadcast(
6059
cls, func, callback=None, return_exceptions=True,
6160
**kwargs: Mapping[str, Any]
@@ -115,7 +114,7 @@ def __getattr__(self, item: str):
115114
EventListenerType = Callable[[Dict[str, Any]], Any]
116115

117116

118-
class AbstactWSRPC(ABC):
117+
class WSRPCBase(ABC):
119118
@abstractmethod
120119
def __init__(self, loop: asyncio.AbstractEventLoop = None,
121120
timeout: Union[int, float] = None):
@@ -138,28 +137,31 @@ async def handle_message(self, message: WSMessage):
138137
async def _on_message(self, msg: WSMessage):
139138
raise NotImplementedError
140139

141-
@abstractclassmethod
140+
@classmethod
141+
@abstractmethod
142142
def get_routes(cls) -> Mapping[str, "RouteType"]:
143143
raise NotImplementedError
144144

145145
@classmethod
146-
def get_clients(cls) -> Dict[str, "AbstactWSRPC"]:
146+
def get_clients(cls) -> Dict[str, "AbstractWSRPC"]:
147147
raise NotImplementedError
148148

149-
@abstractproperty
149+
@property
150+
@abstractmethod
150151
def routes(self) -> Dict[str, "RouteType"]:
151152
raise NotImplementedError
152153

153154
@property
154-
def clients(self) -> Dict[str, "AbstactWSRPC"]:
155+
def clients(self) -> Dict[str, "AbstractWSRPC"]:
155156
""" Property which contains the socket clients """
156157
raise NotImplementedError
157158

158159
@abstractmethod
159160
def prepare_args(self, args) -> Tuple[Tuple[Any, ...], Dict[str, Any]]:
160161
raise NotImplementedError
161162

162-
@abstractstaticmethod
163+
@staticmethod
164+
@abstractmethod
163165
def is_route(func) -> bool:
164166
raise NotImplementedError
165167

@@ -217,30 +219,6 @@ async def make_something(self, foo, bar):
217219
async def emit(self, event: Any) -> None:
218220
pass
219221

220-
@abstractclassmethod
221-
def add_route(cls, route: str,
222-
handler: Union[AbstractRoute, Callable]) -> None:
223-
""" Expose local function through RPC
224-
225-
:param route: Name which function will be aliased for this function.
226-
Remote side should call function by this name.
227-
:param handler: Function or Route class (classes based on
228-
:class:`wsrpc_aiohttp.WebSocketRoute`).
229-
For route classes the public methods will
230-
be registered automatically.
231-
232-
.. note::
233-
234-
Route classes might be initialized only once for the each
235-
socket instance.
236-
237-
In case the method of class will be called first,
238-
:func:`wsrpc_aiohttp.WebSocketRoute.init` will be called
239-
without params before callable method.
240-
241-
"""
242-
raise NotImplementedError
243-
244222
@abstractmethod
245223
def add_event_listener(self, func: EventListenerType) -> None:
246224
raise NotImplementedError
@@ -255,7 +233,8 @@ def remove_route(cls, route: str, fail=True):
255233

256234
raise NotImplementedError
257235

258-
@abstractproperty
236+
@property
237+
@abstractmethod
259238
def proxy(self) -> Proxy:
260239
""" Special property which allow run the remote functions
261240
by `dot` notation
@@ -272,8 +251,39 @@ def proxy(self) -> Proxy:
272251

273252

274253
RouteType = Union[
275-
Callable[[AbstactWSRPC, Any], Any],
276-
Callable[[AbstactWSRPC, Any], Coroutine[Any, None, Any]],
254+
Callable[[WSRPCBase, Any], Any],
255+
Callable[[WSRPCBase, Any], Coroutine[Any, None, Any]],
277256
AbstractRoute
278257
]
258+
259+
260+
class AbstractWSRPC(WSRPCBase, ABC):
261+
@classmethod
262+
@abstractmethod
263+
def add_route(cls, route: str, handler: RouteType) -> None:
264+
""" Expose local function through RPC
265+
266+
:param route: Name which function will be aliased for this function.
267+
Remote side should call function by this name.
268+
:param handler: Function or Route class (classes based on
269+
:class:`wsrpc_aiohttp.WebSocketRoute`).
270+
For route classes the public methods will
271+
be registered automatically.
272+
273+
.. note::
274+
275+
Route classes might be initialized only once for each
276+
socket instance.
277+
278+
In case the method of class will be called first,
279+
:func:`wsrpc_aiohttp.WebSocketRoute.init` will be called
280+
without params before callable method.
281+
282+
"""
283+
raise NotImplementedError
284+
285+
286+
# backward compatibility for typo
287+
# noinspection SpellCheckingInspection
288+
AbstactWSRPC = AbstractWSRPC
279289
FrameMappingItemType = Mapping[IntEnum, Callable[[WSMessage], Any]]

wsrpc_aiohttp/websocket/common.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from . import decorators
1212
from .abc import (
13-
Proxy, AbstactWSRPC, FrameMappingItemType, RouteType, EventListenerType
13+
Proxy, AbstractWSRPC, FrameMappingItemType, RouteType, EventListenerType
1414
)
1515
from .route import Route
1616
from .tools import Singleton, awaitable, loads
@@ -59,10 +59,10 @@ def __repr__(self):
5959

6060

6161
RouteCollectionType = t.DefaultDict[
62-
t.Type[AbstactWSRPC], t.Dict[str, RouteType]
62+
t.Type[AbstractWSRPC], t.Dict[str, RouteType]
6363
]
6464
ClientCollectionType = t.DefaultDict[
65-
t.Type[AbstactWSRPC], t.Dict[str, AbstactWSRPC]
65+
t.Type[AbstractWSRPC], t.Dict[str, AbstractWSRPC]
6666
]
6767
LocksCollectionType = t.DefaultDict[int, asyncio.Lock]
6868
FutureCollectionType = t.DefaultDict[int, asyncio.Future]
@@ -73,7 +73,7 @@ def _route_maker() -> t.Dict[str, RouteType]:
7373
return {"ping": ping} # type: ignore
7474

7575

76-
class WSRPCBase(AbstactWSRPC):
76+
class WSRPCBase(AbstractWSRPC):
7777
""" Common WSRPC abstraction """
7878

7979
_ROUTES = defaultdict(_route_maker) # type: RouteCollectionType
@@ -249,7 +249,7 @@ def get_routes(cls) -> t.Dict[str, RouteType]:
249249
return cls._ROUTES[cls]
250250

251251
@classmethod
252-
def get_clients(cls) -> t.Dict[str, AbstactWSRPC]:
252+
def get_clients(cls) -> t.Dict[str, AbstractWSRPC]:
253253
return cls._CLIENTS[cls]
254254

255255
@property
@@ -258,7 +258,7 @@ def routes(self) -> t.Dict[str, RouteType]:
258258
return self.get_routes()
259259

260260
@property
261-
def clients(self) -> t.Dict[str, AbstactWSRPC]:
261+
def clients(self) -> t.Dict[str, AbstractWSRPC]:
262262
""" Property which contains the socket clients """
263263
return self.get_clients()
264264

@@ -451,7 +451,7 @@ async def emit(self, event):
451451
await self._send(**event)
452452

453453
@classmethod
454-
def add_route(cls, route: str, handler: RouteType):
454+
def add_route(cls, route: str, handler: RouteType) -> None:
455455
""" Expose local function through RPC
456456
457457
:param route: Name which function will be aliased for this function.

wsrpc_aiohttp/websocket/route.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class RouteBase(AbstractRoute, metaclass=RouteMeta):
6363
def __init__(self, socket: AbstractWebSocket):
6464
super().__init__(socket)
6565
self.__socket = socket
66-
self.__loop = getattr(self.socket, "_loop", None)
66+
self.__loop = getattr(self.socket, "_loop") or asyncio.get_event_loop()
6767

6868
if self.__loop is None:
6969
self.__loop = asyncio.get_event_loop()

0 commit comments

Comments
 (0)