mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
Merge pull request #331 from dmuhs/fix/docs-format
Add automatic docstring formatting
This commit is contained in:
@ -42,7 +42,8 @@ class Mplex(IMuxedConn):
|
||||
|
||||
def __init__(self, secured_conn: ISecureConn, peer_id: ID) -> None:
|
||||
"""
|
||||
create a new muxed connection
|
||||
create a new muxed connection.
|
||||
|
||||
:param secured_conn: an instance of ``ISecureConn``
|
||||
:param generic_protocol_handler: generic protocol handler
|
||||
for new muxed streams
|
||||
@ -72,9 +73,7 @@ class Mplex(IMuxedConn):
|
||||
return self.secured_conn.is_initiator
|
||||
|
||||
async def close(self) -> None:
|
||||
"""
|
||||
close the stream muxer and underlying secured connection
|
||||
"""
|
||||
"""close the stream muxer and underlying secured connection."""
|
||||
if self.event_shutting_down.is_set():
|
||||
return
|
||||
# Set the `event_shutting_down`, to allow graceful shutdown.
|
||||
@ -85,14 +84,16 @@ class Mplex(IMuxedConn):
|
||||
|
||||
def is_closed(self) -> bool:
|
||||
"""
|
||||
check connection is fully closed
|
||||
check connection is fully closed.
|
||||
|
||||
:return: true if successful
|
||||
"""
|
||||
return self.event_closed.is_set()
|
||||
|
||||
def _get_next_channel_id(self) -> int:
|
||||
"""
|
||||
Get next available stream id
|
||||
Get next available stream id.
|
||||
|
||||
:return: next available stream id for the connection
|
||||
"""
|
||||
next_id = self.next_channel_id
|
||||
@ -107,7 +108,8 @@ class Mplex(IMuxedConn):
|
||||
|
||||
async def open_stream(self) -> IMuxedStream:
|
||||
"""
|
||||
creates a new muxed_stream
|
||||
creates a new muxed_stream.
|
||||
|
||||
:return: a new ``MplexStream``
|
||||
"""
|
||||
channel_id = self._get_next_channel_id()
|
||||
@ -135,9 +137,7 @@ class Mplex(IMuxedConn):
|
||||
return task_coro.result()
|
||||
|
||||
async def accept_stream(self) -> IMuxedStream:
|
||||
"""
|
||||
accepts a muxed stream opened by the other end
|
||||
"""
|
||||
"""accepts a muxed stream opened by the other end."""
|
||||
return await self._wait_until_shutting_down_or_closed(
|
||||
self.new_stream_queue.get()
|
||||
)
|
||||
@ -146,7 +146,8 @@ class Mplex(IMuxedConn):
|
||||
self, flag: HeaderTags, data: Optional[bytes], stream_id: StreamID
|
||||
) -> int:
|
||||
"""
|
||||
sends a message over the connection
|
||||
sends a message over the connection.
|
||||
|
||||
:param header: header to use
|
||||
:param data: data to send in the message
|
||||
:param stream_id: stream the message is in
|
||||
@ -165,7 +166,8 @@ class Mplex(IMuxedConn):
|
||||
|
||||
async def write_to_stream(self, _bytes: bytes) -> int:
|
||||
"""
|
||||
writes a byte array to a secured connection
|
||||
writes a byte array to a secured connection.
|
||||
|
||||
:param _bytes: byte array to write
|
||||
:return: length written
|
||||
"""
|
||||
@ -173,9 +175,8 @@ class Mplex(IMuxedConn):
|
||||
return len(_bytes)
|
||||
|
||||
async def handle_incoming(self) -> None:
|
||||
"""
|
||||
Read a message off of the secured connection and add it to the corresponding message buffer
|
||||
"""
|
||||
"""Read a message off of the secured connection and add it to the
|
||||
corresponding message buffer."""
|
||||
|
||||
while True:
|
||||
try:
|
||||
@ -190,7 +191,8 @@ class Mplex(IMuxedConn):
|
||||
|
||||
async def read_message(self) -> Tuple[int, int, bytes]:
|
||||
"""
|
||||
Read a single message off of the secured connection
|
||||
Read a single message off of the secured connection.
|
||||
|
||||
:return: stream_id, flag, message contents
|
||||
"""
|
||||
|
||||
@ -217,6 +219,7 @@ class Mplex(IMuxedConn):
|
||||
async def _handle_incoming_message(self) -> None:
|
||||
"""
|
||||
Read and handle a new incoming message.
|
||||
|
||||
:raise MplexUnavailable: `Mplex` encounters fatal error or is shutting down.
|
||||
"""
|
||||
channel_id, flag, message = await self._wait_until_shutting_down_or_closed(
|
||||
|
||||
@ -35,7 +35,8 @@ class MplexStream(IMuxedStream):
|
||||
|
||||
def __init__(self, name: str, stream_id: StreamID, muxed_conn: "Mplex") -> None:
|
||||
"""
|
||||
create new MuxedStream in muxer
|
||||
create new MuxedStream in muxer.
|
||||
|
||||
:param stream_id: stream id of this stream
|
||||
:param muxed_conn: muxed connection of this muxed_stream
|
||||
"""
|
||||
@ -113,9 +114,10 @@ class MplexStream(IMuxedStream):
|
||||
|
||||
async def read(self, n: int = -1) -> bytes:
|
||||
"""
|
||||
Read up to n bytes. Read possibly returns fewer than `n` bytes,
|
||||
if there are not enough bytes in the Mplex buffer.
|
||||
If `n == -1`, read until EOF.
|
||||
Read up to n bytes. Read possibly returns fewer than `n` bytes, if
|
||||
there are not enough bytes in the Mplex buffer. If `n == -1`, read
|
||||
until EOF.
|
||||
|
||||
:param n: number of bytes to read
|
||||
:return: bytes actually read
|
||||
"""
|
||||
@ -142,7 +144,8 @@ class MplexStream(IMuxedStream):
|
||||
|
||||
async def write(self, data: bytes) -> int:
|
||||
"""
|
||||
write to stream
|
||||
write to stream.
|
||||
|
||||
:return: number of bytes written
|
||||
"""
|
||||
if self.event_local_closed.is_set():
|
||||
@ -155,10 +158,8 @@ class MplexStream(IMuxedStream):
|
||||
return await self.muxed_conn.send_message(flag, data, self.stream_id)
|
||||
|
||||
async def close(self) -> None:
|
||||
"""
|
||||
Closing a stream closes it for writing and closes the remote end for reading
|
||||
but allows writing in the other direction.
|
||||
"""
|
||||
"""Closing a stream closes it for writing and closes the remote end for
|
||||
reading but allows writing in the other direction."""
|
||||
# TODO error handling with timeout
|
||||
|
||||
async with self.close_lock:
|
||||
@ -182,10 +183,7 @@ class MplexStream(IMuxedStream):
|
||||
del self.muxed_conn.streams[self.stream_id]
|
||||
|
||||
async def reset(self) -> None:
|
||||
"""
|
||||
closes both ends of the stream
|
||||
tells this remote side to hang up
|
||||
"""
|
||||
"""closes both ends of the stream tells this remote side to hang up."""
|
||||
async with self.close_lock:
|
||||
# Both sides have been closed. No need to event_reset.
|
||||
if self.event_remote_closed.is_set() and self.event_local_closed.is_set():
|
||||
@ -218,7 +216,8 @@ class MplexStream(IMuxedStream):
|
||||
# TODO deadline not in use
|
||||
def set_deadline(self, ttl: int) -> bool:
|
||||
"""
|
||||
set deadline for muxed stream
|
||||
set deadline for muxed stream.
|
||||
|
||||
:return: True if successful
|
||||
"""
|
||||
self.read_deadline = ttl
|
||||
@ -227,7 +226,8 @@ class MplexStream(IMuxedStream):
|
||||
|
||||
def set_read_deadline(self, ttl: int) -> bool:
|
||||
"""
|
||||
set read deadline for muxed stream
|
||||
set read deadline for muxed stream.
|
||||
|
||||
:return: True if successful
|
||||
"""
|
||||
self.read_deadline = ttl
|
||||
@ -235,7 +235,8 @@ class MplexStream(IMuxedStream):
|
||||
|
||||
def set_write_deadline(self, ttl: int) -> bool:
|
||||
"""
|
||||
set write deadline for muxed stream
|
||||
set write deadline for muxed stream.
|
||||
|
||||
:return: True if successful
|
||||
"""
|
||||
self.write_deadline = ttl
|
||||
|
||||
Reference in New Issue
Block a user