py-libp2p/peer/peerdata.py

40 lines
901 B
Python
Raw Normal View History

2018-10-21 05:11:44 +08:00
from .peerdata_interface import IPeerData
class PeerData(IPeerData):
2018-10-22 03:15:53 +08:00
def __init__(self):
self.metadata = {}
2018-10-21 05:11:44 +08:00
self.protocols = []
self.addrs = []
def get_protocols(self):
2018-10-22 03:15:53 +08:00
return self.protocols
2018-10-21 05:11:44 +08:00
def add_protocols(self, protocols):
self.protocols.extend(protocols)
def set_protocols(self, protocols):
self.protocols = protocols
2018-10-21 05:11:44 +08:00
def add_addrs(self, addrs):
2018-10-22 03:15:53 +08:00
self.addrs.extend(addrs)
2018-10-21 05:11:44 +08:00
def get_addrs(self):
2018-10-22 03:15:53 +08:00
return self.addrs
2018-10-21 05:11:44 +08:00
def clear_addrs(self):
2018-10-22 03:15:53 +08:00
self.addrs = []
2018-10-21 05:11:44 +08:00
def put_metadata(self, key, val):
2018-10-22 03:15:53 +08:00
self.metadata[key] = val
2018-10-21 05:11:44 +08:00
def get_metadata(self, key):
2018-10-22 03:15:53 +08:00
if key in self.metadata:
2018-10-30 07:03:19 +08:00
return self.metadata[key]
2018-10-29 20:13:39 +08:00
else:
2018-10-30 07:03:19 +08:00
raise PeerDataError("key not found")
class PeerDataError(KeyError):
"""Raised when a key is not found in peer metadata"""
pass