mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-03-23 21:51:28 +00:00
Add automatic docstring formatter and apply
This commit is contained in:
@ -2,12 +2,12 @@ from libp2p.exceptions import BaseLibp2pError
|
||||
|
||||
|
||||
class MultiselectCommunicatorError(BaseLibp2pError):
|
||||
"""Raised when an error occurs during read/write via communicator"""
|
||||
"""Raised when an error occurs during read/write via communicator."""
|
||||
|
||||
|
||||
class MultiselectError(BaseLibp2pError):
|
||||
"""Raised when an error occurs in multiselect process"""
|
||||
"""Raised when an error occurs in multiselect process."""
|
||||
|
||||
|
||||
class MultiselectClientError(BaseLibp2pError):
|
||||
"""Raised when an error occurs in protocol selection process"""
|
||||
"""Raised when an error occurs in protocol selection process."""
|
||||
|
||||
@ -11,11 +11,9 @@ PROTOCOL_NOT_FOUND_MSG = "na"
|
||||
|
||||
|
||||
class Multiselect(IMultiselectMuxer):
|
||||
"""
|
||||
Multiselect module that is responsible for responding to
|
||||
a multiselect client and deciding on
|
||||
a specific protocol and handler pair to use for communication
|
||||
"""
|
||||
"""Multiselect module that is responsible for responding to a multiselect
|
||||
client and deciding on a specific protocol and handler pair to use for
|
||||
communication."""
|
||||
|
||||
handlers: Dict[TProtocol, StreamHandlerFn]
|
||||
|
||||
@ -23,8 +21,8 @@ class Multiselect(IMultiselectMuxer):
|
||||
self.handlers = {}
|
||||
|
||||
def add_handler(self, protocol: TProtocol, handler: StreamHandlerFn) -> None:
|
||||
"""
|
||||
Store the handler with the given protocol
|
||||
"""Store the handler with the given protocol.
|
||||
|
||||
:param protocol: protocol name
|
||||
:param handler: handler function
|
||||
"""
|
||||
@ -33,8 +31,8 @@ class Multiselect(IMultiselectMuxer):
|
||||
async def negotiate(
|
||||
self, communicator: IMultiselectCommunicator
|
||||
) -> Tuple[TProtocol, StreamHandlerFn]:
|
||||
"""
|
||||
Negotiate performs protocol selection
|
||||
"""Negotiate performs protocol selection.
|
||||
|
||||
:param stream: stream to negotiate on
|
||||
:return: selected protocol name, handler function
|
||||
:raise MultiselectError: raised when negotiation failed
|
||||
@ -65,8 +63,8 @@ class Multiselect(IMultiselectMuxer):
|
||||
raise MultiselectError(error)
|
||||
|
||||
async def handshake(self, communicator: IMultiselectCommunicator) -> None:
|
||||
"""
|
||||
Perform handshake to agree on multiselect protocol
|
||||
"""Perform handshake to agree on multiselect protocol.
|
||||
|
||||
:param communicator: communicator to use
|
||||
:raise MultiselectError: raised when handshake failed
|
||||
"""
|
||||
@ -88,8 +86,8 @@ class Multiselect(IMultiselectMuxer):
|
||||
|
||||
|
||||
def validate_handshake(handshake_contents: str) -> bool:
|
||||
"""
|
||||
Determine if handshake is valid and should be confirmed
|
||||
"""Determine if handshake is valid and should be confirmed.
|
||||
|
||||
:param handshake_contents: contents of handshake message
|
||||
:return: true if handshake is complete, false otherwise
|
||||
"""
|
||||
|
||||
@ -11,15 +11,13 @@ PROTOCOL_NOT_FOUND_MSG = "na"
|
||||
|
||||
|
||||
class MultiselectClient(IMultiselectClient):
|
||||
"""
|
||||
Client for communicating with receiver's multiselect
|
||||
module in order to select a protocol id to communicate over
|
||||
"""
|
||||
"""Client for communicating with receiver's multiselect module in order to
|
||||
select a protocol id to communicate over."""
|
||||
|
||||
async def handshake(self, communicator: IMultiselectCommunicator) -> None:
|
||||
"""
|
||||
Ensure that the client and multiselect
|
||||
are both using the same multiselect protocol
|
||||
"""Ensure that the client and multiselect are both using the same
|
||||
multiselect protocol.
|
||||
|
||||
:param stream: stream to communicate with multiselect over
|
||||
:raise MultiselectClientError: raised when handshake failed
|
||||
"""
|
||||
@ -39,10 +37,10 @@ class MultiselectClient(IMultiselectClient):
|
||||
async def select_one_of(
|
||||
self, protocols: Sequence[TProtocol], communicator: IMultiselectCommunicator
|
||||
) -> TProtocol:
|
||||
"""
|
||||
For each protocol, send message to multiselect selecting protocol
|
||||
"""For each protocol, send message to multiselect selecting protocol
|
||||
and fail if multiselect does not return same protocol. Returns first
|
||||
protocol that multiselect agrees on (i.e. that multiselect selects)
|
||||
|
||||
:param protocol: protocol to select
|
||||
:param stream: stream to communicate with multiselect over
|
||||
:return: selected protocol
|
||||
@ -62,8 +60,8 @@ class MultiselectClient(IMultiselectClient):
|
||||
async def try_select(
|
||||
self, communicator: IMultiselectCommunicator, protocol: TProtocol
|
||||
) -> TProtocol:
|
||||
"""
|
||||
Try to select the given protocol or raise exception if fails
|
||||
"""Try to select the given protocol or raise exception if fails.
|
||||
|
||||
:param communicator: communicator to use to communicate with counterparty
|
||||
:param protocol: protocol to select
|
||||
:raise MultiselectClientError: raised when protocol negotiation failed
|
||||
@ -87,8 +85,8 @@ class MultiselectClient(IMultiselectClient):
|
||||
|
||||
|
||||
def validate_handshake(handshake_contents: str) -> bool:
|
||||
"""
|
||||
Determine if handshake is valid and should be confirmed
|
||||
"""Determine if handshake is valid and should be confirmed.
|
||||
|
||||
:param handshake_contents: contents of handshake message
|
||||
:return: true if handshake is complete, false otherwise
|
||||
"""
|
||||
|
||||
@ -8,15 +8,13 @@ from libp2p.typing import TProtocol
|
||||
|
||||
|
||||
class IMultiselectClient(ABC):
|
||||
"""
|
||||
Client for communicating with receiver's multiselect
|
||||
module in order to select a protocol id to communicate over
|
||||
"""
|
||||
"""Client for communicating with receiver's multiselect module in order to
|
||||
select a protocol id to communicate over."""
|
||||
|
||||
async def handshake(self, communicator: IMultiselectCommunicator) -> None:
|
||||
"""
|
||||
Ensure that the client and multiselect
|
||||
are both using the same multiselect protocol
|
||||
"""Ensure that the client and multiselect are both using the same
|
||||
multiselect protocol.
|
||||
|
||||
:param stream: stream to communicate with multiselect over
|
||||
:raise Exception: multiselect protocol ID mismatch
|
||||
"""
|
||||
@ -25,10 +23,10 @@ class IMultiselectClient(ABC):
|
||||
async def select_one_of(
|
||||
self, protocols: Sequence[TProtocol], communicator: IMultiselectCommunicator
|
||||
) -> TProtocol:
|
||||
"""
|
||||
For each protocol, send message to multiselect selecting protocol
|
||||
"""For each protocol, send message to multiselect selecting protocol
|
||||
and fail if multiselect does not return same protocol. Returns first
|
||||
protocol that multiselect agrees on (i.e. that multiselect selects)
|
||||
|
||||
:param protocol: protocol to select
|
||||
:param stream: stream to communicate with multiselect over
|
||||
:return: selected protocol
|
||||
@ -37,8 +35,8 @@ class IMultiselectClient(ABC):
|
||||
async def try_select(
|
||||
self, communicator: IMultiselectCommunicator, protocol: TProtocol
|
||||
) -> TProtocol:
|
||||
"""
|
||||
Try to select the given protocol or raise exception if fails
|
||||
"""Try to select the given protocol or raise exception if fails.
|
||||
|
||||
:param communicator: communicator to use to communicate with counterparty
|
||||
:param protocol: protocol to select
|
||||
:raise Exception: error in protocol selection
|
||||
|
||||
@ -2,21 +2,17 @@ from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class IMultiselectCommunicator(ABC):
|
||||
"""
|
||||
Communicator helper class that ensures both the client
|
||||
and multistream module will follow the same multistream protocol,
|
||||
which is necessary for them to work
|
||||
"""
|
||||
"""Communicator helper class that ensures both the client and multistream
|
||||
module will follow the same multistream protocol, which is necessary for
|
||||
them to work."""
|
||||
|
||||
@abstractmethod
|
||||
async def write(self, msg_str: str) -> None:
|
||||
"""
|
||||
Write message to stream
|
||||
"""Write message to stream.
|
||||
|
||||
:param msg_str: message to write
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def read(self) -> str:
|
||||
"""
|
||||
Reads message from stream until EOF
|
||||
"""
|
||||
"""Reads message from stream until EOF."""
|
||||
|
||||
@ -7,18 +7,16 @@ from .multiselect_communicator_interface import IMultiselectCommunicator
|
||||
|
||||
|
||||
class IMultiselectMuxer(ABC):
|
||||
"""
|
||||
Multiselect module that is responsible for responding to
|
||||
a multiselect client and deciding on
|
||||
a specific protocol and handler pair to use for communication
|
||||
"""
|
||||
"""Multiselect module that is responsible for responding to a multiselect
|
||||
client and deciding on a specific protocol and handler pair to use for
|
||||
communication."""
|
||||
|
||||
handlers: Dict[TProtocol, StreamHandlerFn]
|
||||
|
||||
@abstractmethod
|
||||
def add_handler(self, protocol: TProtocol, handler: StreamHandlerFn) -> None:
|
||||
"""
|
||||
Store the handler with the given protocol
|
||||
"""Store the handler with the given protocol.
|
||||
|
||||
:param protocol: protocol name
|
||||
:param handler: handler function
|
||||
"""
|
||||
@ -27,8 +25,8 @@ class IMultiselectMuxer(ABC):
|
||||
async def negotiate(
|
||||
self, communicator: IMultiselectCommunicator
|
||||
) -> Tuple[TProtocol, StreamHandlerFn]:
|
||||
"""
|
||||
Negotiate performs protocol selection
|
||||
"""Negotiate performs protocol selection.
|
||||
|
||||
:param stream: stream to negotiate on
|
||||
:return: selected protocol name, handler function
|
||||
:raise Exception: negotiation failed exception
|
||||
|
||||
Reference in New Issue
Block a user