1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Fix herald optional dependancy

This commit is contained in:
Steffo 2020-09-16 02:13:24 +02:00
parent 55b74aa668
commit 2f8db3b35a

View file

@ -11,9 +11,14 @@ from sqlalchemy.schema import Table
import royalnet.alchemy as ra import royalnet.alchemy as ra
import royalnet.backpack.tables as rbt import royalnet.backpack.tables as rbt
import royalnet.commands as rc import royalnet.commands as rc
import royalnet.herald as rh
import royalnet.utils as ru import royalnet.utils as ru
try:
import royalnet.herald as rh
except ImportError:
rh = None
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -88,7 +93,7 @@ class Serf(abc.ABC):
self.init_alchemy(alchemy_cfg, tables) self.init_alchemy(alchemy_cfg, tables)
log.info(f"Alchemy: {self.alchemy}") log.info(f"Alchemy: {self.alchemy}")
self.herald: Optional[rh.Link] = None self.herald: Optional["rh.Link"] = None
"""The :class:`Link` object connecting the :class:`Serf` to the rest of the Herald network.""" """The :class:`Link` object connecting the :class:`Serf` to the rest of the Herald network."""
self.herald_task: Optional[aio.Task] = None self.herald_task: Optional[aio.Task] = None
@ -120,7 +125,7 @@ class Serf(abc.ABC):
log.info(f"Events: {len(self.events)} events") log.info(f"Events: {len(self.events)} events")
log.info(f"Commands: {len(self.commands)} commands") log.info(f"Commands: {len(self.commands)} commands")
if rh.Link is None: if rh is None:
log.info("Herald: not installed") log.info("Herald: not installed")
elif not herald_cfg["enabled"]: elif not herald_cfg["enabled"]:
log.info("Herald: disabled") log.info("Herald: disabled")
@ -143,8 +148,8 @@ class Serf(abc.ABC):
:class:`royalherald.Response`.""" :class:`royalherald.Response`."""
if self.herald is None: if self.herald is None:
raise rc.UnsupportedError("`royalherald` is not enabled on this serf.") raise rc.UnsupportedError("`royalherald` is not enabled on this serf.")
request: rh.Request = rh.Request(handler=event_name, data=kwargs) request: "rh.Request" = rh.Request(handler=event_name, data=kwargs)
response: rh.Response = await self.herald.request(destination=destination, request=request) response: "rh.Response" = await self.herald.request(destination=destination, request=request)
if isinstance(response, rh.ResponseFailure): if isinstance(response, rh.ResponseFailure):
if response.name == "no_event": if response.name == "no_event":
raise rc.ProgramError(f"There is no event named {event_name} in {destination}.") raise rc.ProgramError(f"There is no event named {event_name} in {destination}.")
@ -211,7 +216,7 @@ class Serf(abc.ABC):
def init_herald(self, herald_cfg: rc.ConfigDict): def init_herald(self, herald_cfg: rc.ConfigDict):
"""Create a :class:`Link` and bind :class:`Event`.""" """Create a :class:`Link` and bind :class:`Event`."""
herald_cfg["name"] = self.interface_name herald_cfg["name"] = self.interface_name
self.herald: rh.Link = rh.Link(rh.Config.from_config(**herald_cfg), self.network_handler) self.herald: "rh.Link" = rh.Link(rh.Config.from_config(**herald_cfg), self.network_handler)
def register_events(self, events: List[Type[rc.HeraldEvent]], pack_cfg: rc.ConfigDict): def register_events(self, events: List[Type[rc.HeraldEvent]], pack_cfg: rc.ConfigDict):
for SelectedEvent in events: for SelectedEvent in events:
@ -230,7 +235,7 @@ class Serf(abc.ABC):
log.debug(f"Registering: {SelectedEvent.__qualname__} -> {SelectedEvent.name}") log.debug(f"Registering: {SelectedEvent.__qualname__} -> {SelectedEvent.name}")
self.events[SelectedEvent.name] = event self.events[SelectedEvent.name] = event
async def network_handler(self, message: Union[rh.Request, rh.Broadcast]) -> rh.Response: async def network_handler(self, message: Union["rh.Request", "rh.Broadcast"]) -> "rh.Response":
try: try:
event: rc.HeraldEvent = self.events[message.handler] event: rc.HeraldEvent = self.events[message.handler]
except KeyError: except KeyError: