Run black over repo

This commit is contained in:
Alex Stokes
2019-07-31 15:00:12 -07:00
parent a2133d8c7c
commit 0ae9840928
69 changed files with 791 additions and 1095 deletions

View File

@ -1,6 +1 @@
HEADER_TAGS = {
"NEW_STREAM": 0,
"MESSAGE": 2,
"CLOSE": 4,
"RESET": 6
}
HEADER_TAGS = {"NEW_STREAM": 0, "MESSAGE": 2, "CLOSE": 4, "RESET": 6}

View File

@ -158,7 +158,9 @@ class Mplex(IMuxedConn):
try:
header = await decode_uvarint_from_stream(self.raw_conn.reader, timeout)
length = await decode_uvarint_from_stream(self.raw_conn.reader, timeout)
message = await asyncio.wait_for(self.raw_conn.reader.read(length), timeout=timeout)
message = await asyncio.wait_for(
self.raw_conn.reader.read(length), timeout=timeout
)
except asyncio.TimeoutError:
return None, None, None

View File

@ -40,7 +40,8 @@ class MplexStream(IMuxedStream):
:return: number of bytes written
"""
return await self.mplex_conn.send_message(
get_flag(self.initiator, "MESSAGE"), data, self.stream_id)
get_flag(self.initiator, "MESSAGE"), data, self.stream_id
)
async def close(self):
"""
@ -50,7 +51,9 @@ class MplexStream(IMuxedStream):
"""
# TODO error handling with timeout
# TODO understand better how mutexes are used from go repo
await self.mplex_conn.send_message(get_flag(self.initiator, "CLOSE"), None, self.stream_id)
await self.mplex_conn.send_message(
get_flag(self.initiator, "CLOSE"), None, self.stream_id
)
remote_lock = ""
async with self.stream_lock:
@ -79,7 +82,8 @@ class MplexStream(IMuxedStream):
if not self.remote_closed:
await self.mplex_conn.send_message(
get_flag(self.initiator, "RESET"), None, self.stream_id)
get_flag(self.initiator, "RESET"), None, self.stream_id
)
self.local_closed = True
self.remote_closed = True

View File

@ -5,14 +5,14 @@ from .constants import HEADER_TAGS
def encode_uvarint(number):
"""Pack `number` into varint bytes"""
buf = b''
buf = b""
while True:
towrite = number & 0x7f
towrite = number & 0x7F
number >>= 7
if number:
buf += bytes((towrite | 0x80, ))
buf += bytes((towrite | 0x80,))
else:
buf += bytes((towrite, ))
buf += bytes((towrite,))
break
return buf
@ -22,7 +22,7 @@ def decode_uvarint(buff, index):
result = 0
while True:
i = buff[index]
result |= (i & 0x7f) << shift
result |= (i & 0x7F) << shift
shift += 7
if not i & 0x80:
break
@ -30,19 +30,21 @@ def decode_uvarint(buff, index):
return result, index + 1
async def decode_uvarint_from_stream(reader, timeout):
shift = 0
result = 0
while True:
byte = await asyncio.wait_for(reader.read(1), timeout=timeout)
i = struct.unpack('>H', b'\x00' + byte)[0]
result |= (i & 0x7f) << shift
i = struct.unpack(">H", b"\x00" + byte)[0]
result |= (i & 0x7F) << shift
shift += 7
if not i & 0x80:
break
return result
def get_flag(initiator, action):
"""
get header flag based on action for mplex