Fix cyclic import and lint

This commit is contained in:
NIC619
2019-07-29 12:42:13 +08:00
parent a4a0d79f6d
commit 2d4e23cfe2
13 changed files with 87 additions and 48 deletions

View File

@ -1,9 +1,13 @@
from abc import ABC, abstractmethod
from abc import (
ABC,
abstractmethod,
)
from typing import (
Any,
Callable,
Coroutine,
Sequence,
TYPE_CHECKING,
)
from multiaddr import Multiaddr
@ -11,9 +15,14 @@ from multiaddr import Multiaddr
from libp2p.peer.id import ID
from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn
from .notifee_interface import INotifee
from .stream.net_stream import NetStream
if TYPE_CHECKING:
from .notifee_interface import INotifee
StreamHandlerFn = Callable[[NetStream], Coroutine[Any, Any, None]]
class INetwork(ABC):
@ -34,7 +43,7 @@ class INetwork(ABC):
"""
@abstractmethod
def set_stream_handler(self, protocol_id: str, stream_handler: Callable[[NetStream], Coroutine[Any, Any, None]]) -> bool:
def set_stream_handler(self, protocol_id: str, stream_handler: StreamHandlerFn) -> bool:
"""
:param protocol_id: protocol id used on stream
:param stream_handler: a stream handler instance
@ -42,7 +51,9 @@ class INetwork(ABC):
"""
@abstractmethod
def new_stream(self, peer_id: ID, protocol_ids: Sequence[str]) -> Coroutine[Any, Any, NetStream]:
def new_stream(self,
peer_id: ID,
protocol_ids: Sequence[str]) -> Coroutine[Any, Any, NetStream]:
"""
:param peer_id: peer_id of destination
:param protocol_ids: available protocol ids to use for stream
@ -57,7 +68,7 @@ class INetwork(ABC):
"""
@abstractmethod
def notify(self, notifee: INotifee) -> bool:
def notify(self, notifee: 'INotifee') -> bool:
"""
:param notifee: object implementing Notifee interface
:return: true if notifee registered successfully, false otherwise

View File

@ -1,52 +1,58 @@
from abc import ABC, abstractmethod
from abc import (
ABC,
abstractmethod,
)
from typing import TYPE_CHECKING
from multiaddr import Multiaddr
from libp2p.network.network_interface import INetwork
from libp2p.stream_muxer.muxed_connection_interface import IMuxedConn
from libp2p.network.stream.net_stream_interface import INetStream
if TYPE_CHECKING:
from .network_interface import INetwork
class INotifee(ABC):
@abstractmethod
async def opened_stream(self, network: INetwork, stream: INetStream) -> None:
async def opened_stream(self, network: 'INetwork', stream: INetStream) -> None:
"""
:param network: network the stream was opened on
:param stream: stream that was opened
"""
@abstractmethod
async def closed_stream(self, network: INetwork, stream: INetStream) -> None:
async def closed_stream(self, network: 'INetwork', stream: INetStream) -> None:
"""
:param network: network the stream was closed on
:param stream: stream that was closed
"""
@abstractmethod
async def connected(self, network: INetwork, conn: IMuxedConn) -> None:
async def connected(self, network: 'INetwork', conn: IMuxedConn) -> None:
"""
:param network: network the connection was opened on
:param conn: connection that was opened
"""
@abstractmethod
async def disconnected(self, network: INetwork, conn: IMuxedConn) -> None:
async def disconnected(self, network: 'INetwork', conn: IMuxedConn) -> None:
"""
:param network: network the connection was closed on
:param conn: connection that was closed
"""
@abstractmethod
async def listen(self, network: INetwork, multiaddr: Multiaddr) -> None:
async def listen(self, network: 'INetwork', multiaddr: Multiaddr) -> None:
"""
:param network: network the listener is listening on
:param multiaddr: multiaddress listener is listening on
"""
@abstractmethod
async def listen_close(self, network: INetwork, multiaddr: Multiaddr) -> None:
async def listen_close(self, network: 'INetwork', multiaddr: Multiaddr) -> None:
"""
:param network: network the connection was opened on
:param multiaddr: multiaddress listener is no longer listening on

View File

@ -31,6 +31,9 @@ from .connection.raw_connection import RawConnection
from .stream.net_stream import NetStream
StreamHandlerFn = Callable[[NetStream], Coroutine[Any, Any, None]]
class Swarm(INetwork):
# pylint: disable=too-many-instance-attributes,cell-var-from-loop,too-many-arguments
@ -76,7 +79,7 @@ class Swarm(INetwork):
def get_peer_id(self) -> ID:
return self.self_id
def set_stream_handler(self, protocol_id: str, stream_handler: Callable[[NetStream], Coroutine[Any, Any, None]]) -> bool:
def set_stream_handler(self, protocol_id: str, stream_handler: StreamHandlerFn) -> bool:
"""
:param protocol_id: protocol id used on stream
:param stream_handler: a stream handler instance
@ -150,7 +153,10 @@ class Swarm(INetwork):
muxed_stream = await muxed_conn.open_stream(protocol_ids[0], multiaddr)
# Perform protocol muxing to determine protocol to use
selected_protocol = await self.multiselect_client.select_one_of(list(protocol_ids), muxed_stream)
selected_protocol = await self.multiselect_client.select_one_of(
list(protocol_ids),
muxed_stream,
)
# Create a net stream with the selected protocol
net_stream = NetStream(muxed_stream)
@ -180,7 +186,8 @@ class Swarm(INetwork):
if str(multiaddr) in self.listeners:
return True
async def conn_handler(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
async def conn_handler(reader: asyncio.StreamReader,
writer: asyncio.StreamWriter) -> None:
# Read in first message (should be peer_id of initiator) and ack
peer_id = id_b58_decode((await reader.read(1024)).decode())