Skip to content

Commit e138298

Browse files
committed
style: apply black linting
1 parent a70bac5 commit e138298

File tree

4 files changed

+32
-28
lines changed

4 files changed

+32
-28
lines changed

binding/python/py/synlink/multiaddr.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
11
"""Re-export of the multiaddr library."""
22

3+
from typing import Union
34
from multiaddr import Multiaddr
4-
from multiaddr.protocols import (P_DNS, P_IP4, P_IP6, P_P2P, P_TCP, P_UDP,
5-
Protocol)
6-
7-
__all__ = ["Multiaddr", "Protocol", "is_valid_address"]
5+
from multiaddr.protocols import (
6+
P_DNS,
7+
P_IP4,
8+
P_IP6,
9+
P_P2P,
10+
P_TCP,
11+
P_UDP,
12+
Protocol,
13+
)
14+
from multiaddr.exceptions import (
15+
StringParseError,
16+
BinaryParseError,
17+
ProtocolExistsError,
18+
ProtocolNotFoundError,
19+
)
20+
21+
__all__ = [
22+
"Multiaddr",
23+
"Protocol",
24+
"is_valid_address",
25+
"StringParseError",
26+
"BinaryParseError",
27+
"ProtocolExistsError",
28+
"ProtocolNotFoundError",
29+
]
830

931
_PROTOCOL_CONFIG = {
1032
P_IP4: {"transports": [P_TCP, P_UDP], "overlays": [P_P2P]},
1133
P_IP6: {"transports": [P_TCP, P_UDP], "overlays": [P_P2P]},
1234
P_DNS: {"transports": [P_TCP], "overlays": [P_P2P]},
1335
}
1436

15-
1637
def _generate_supported_protocols():
1738
"""Generate all valid protocol combinations."""
1839
protocols = set()
@@ -28,11 +49,9 @@ def _generate_supported_protocols():
2849

2950
return protocols
3051

31-
3252
_SUPPORTED_PROTOCOLS = _generate_supported_protocols()
3353

34-
35-
def is_valid_address(addr: Multiaddr) -> bool:
54+
def is_valid_address(addr: Union[Multiaddr, str, bytes]) -> bool:
3655
"""
3756
Check if a multiaddr uses only supported protocols.
3857
@@ -46,5 +65,9 @@ def is_valid_address(addr: Multiaddr) -> bool:
4665
"/dns/example.com/tcp/443/p2p/QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N"
4766
```
4867
"""
49-
protocol_tuple = tuple(protocol.code for protocol in addr.protocols())
68+
address = addr
69+
if isinstance(addr, (str, bytes)):
70+
address = Multiaddr(addr)
71+
72+
protocol_tuple = tuple(protocol.code for protocol in address.protocols())
5073
return protocol_tuple in _SUPPORTED_PROTOCOLS

binding/python/py/synlink/peer_id.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
else:
1313
from typing_extensions import Self
1414

15-
1615
class PeerId(object):
1716
def __init__(self, data: Union[bytes, bytearray]):
1817
if isinstance(data, (bytearray,)):
@@ -21,50 +20,38 @@ def __init__(self, data: Union[bytes, bytearray]):
2120
self._id = data
2221
else:
2322
raise TypeError("only supported types; bytes, and bytearray(s).")
24-
2523
@cached_property
2624
def xor_id(self) -> int:
2725
return int(sha256_digest(self._id).hex(), 16)
28-
2926
@cached_property
3027
def base58(self) -> str:
3128
return base58.b58encode(self._id).decode()
32-
3329
def to_base58(self) -> str:
3430
return self.base58
35-
3631
@classmethod
3732
def from_base58(cls, b58_encoded_peer_id_str: str) -> Self:
3833
peer_id_bytes = base58.b58decode(b58_encoded_peer_id_str)
3934
pid = cls(peer_id_bytes)
4035
return pid
41-
4236
@classmethod
4337
def from_bytes(cls, data: bytes):
4438
return cls(data)
45-
4639
def to_bytes(self) -> bytes:
4740
return self._id
48-
4941
@classmethod
5042
def from_pubkey(cls, key: PublicKey) -> Self:
5143
serialized_key = key.to_bytes()
5244
algo = multihash.Func.sha2_256
5345
mh_digest = multihash.digest(serialized_key, algo)
5446
return cls(mh_digest.encode())
55-
5647
def __bytes__(self) -> bytes:
5748
return self.to_bytes()
58-
5949
def __hash__(self):
6050
return hash(self._id)
61-
6251
def __repr__(self):
6352
return f"<synlink.swarm.peer_id.PeerID {self.to_base58()}>"
64-
6553
def __str__(self):
6654
return self.to_base58()
67-
6855
def __eq__(self, other: object) -> bool:
6956
if isinstance(other, str):
7057
return self.to_base58() == other
@@ -75,7 +62,6 @@ def __eq__(self, other: object) -> bool:
7562
else:
7663
raise ValueError("Unsupported type for PeerID comparison.")
7764

78-
7965
def sha256_digest(data: Union[str, bytes]) -> bytes:
8066
if isinstance(data, str):
8167
data = data.encode("utf8")

binding/python/py/synlink/typing.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
__all__ = ["TProtocol"]
1111

12-
1312
@runtime_checkable
1413
class Serializable(Protocol):
1514
"""
@@ -32,11 +31,9 @@ class Serializable(Protocol):
3231
def to_bytes(self) -> bytes:
3332
"""serialize the object into bytes."""
3433
...
35-
3634
@classmethod
3735
def from_bytes(cls, data: bytes) -> Self:
3836
"""deserialize the object from bytes."""
3937
...
4038

41-
4239
TProtocol = NewType("TProtocol", str)

binding/python/py/synlink/utils.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
__all__ = ["_check_minimum_version", "_check_exact_version"]
55

6-
76
def _check_minimum_version(
87
major: int, minor: Optional[int] = 0, micro: Optional[int] = 0
98
):
@@ -12,7 +11,6 @@ def _check_minimum_version(
1211
required = (major, minor, micro)
1312
return current[:3] >= required
1413

15-
1614
def _check_exact_version(
1715
major: int, minor: Optional[int] = 0, micro: Optional[int] = 0
1816
):

0 commit comments

Comments
 (0)