busline 0.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,59 @@
1
+ import logging
2
+ from abc import ABC, abstractmethod
3
+ from uuid import uuid4
4
+
5
+ from src.event.event import Event
6
+ from src.eventbus_client.eventbus_connector import EventBusConnector
7
+
8
+
9
+ class Publisher(EventBusConnector, ABC):
10
+ """
11
+ Abstract class which can be implemented by your components which must be able to publish on eventbus
12
+
13
+ Author: Nicola Ricciardi
14
+ """
15
+
16
+ def __init__(self, publisher_id: str = str(uuid4())):
17
+ EventBusConnector.__init__(self, publisher_id)
18
+
19
+ @abstractmethod
20
+ async def _internal_publish(self, topic_name: str, event: Event, **kwargs):
21
+ """
22
+ Actual publish on topic the event
23
+
24
+ :param topic_name:
25
+ :param event:
26
+ :return:
27
+ """
28
+
29
+ async def publish(self, topic_name: str, event: Event, **kwargs):
30
+ """
31
+ Publish on topic the event
32
+
33
+ :param topic_name:
34
+ :param event:
35
+ :return:
36
+ """
37
+
38
+ logging.debug(f"{self._id} publishing on {topic_name}: {event}")
39
+ self.on_publishing(topic_name, event)
40
+ await self._internal_publish(topic_name, event, **kwargs)
41
+ self.on_published(topic_name, event)
42
+
43
+ def on_publishing(self, topic_name: str, event: Event):
44
+ """
45
+ Callback called on publishing start
46
+
47
+ :param topic_name:
48
+ :param event:
49
+ :return:
50
+ """
51
+
52
+ def on_published(self, topic_name: str, event: Event):
53
+ """
54
+ Callback called on publishing end
55
+
56
+ :param topic_name:
57
+ :param event:
58
+ :return:
59
+ """
File without changes
@@ -0,0 +1,19 @@
1
+ from typing import Callable
2
+ from src.event.event import Event
3
+ from src.eventbus_client.subscriber.event_listener import EventListener
4
+
5
+
6
+ class ClosureEventListener(EventListener):
7
+ """
8
+ Abstract event listener which use a pre-defined callback as `on_event`
9
+
10
+ Author: Nicola Ricciardi
11
+ """
12
+
13
+ def __init__(self, on_event_callback: Callable[[str, Event], None]):
14
+ EventListener.__init__(self)
15
+
16
+ self.__on_event_callback = on_event_callback
17
+
18
+ async def on_event(self, topic_name: str, event: Event, **kwargs):
19
+ self.__on_event_callback(topic_name, event, **kwargs)
@@ -0,0 +1,15 @@
1
+ from abc import ABC, abstractmethod
2
+ from src.event.event import Event
3
+
4
+
5
+ class EventListener(ABC):
6
+
7
+ @abstractmethod
8
+ async def on_event(self, topic_name: str, event: Event, **kwargs):
9
+ """
10
+ Callback called when new event arrives
11
+
12
+ :param topic_name:
13
+ :param event:
14
+ :return:
15
+ """
@@ -0,0 +1,17 @@
1
+ from typing import Callable
2
+ from src.event.event import Event
3
+ from src.eventbus.eventbus import EventBus
4
+ from src.eventbus_client.subscriber.closure_event_listener import ClosureEventListener
5
+ from src.eventbus_client.subscriber.local_eventbus_subscriber import LocalEventBusSubscriber
6
+
7
+
8
+ class LocalEventBusClosureSubscriber(LocalEventBusSubscriber, ClosureEventListener):
9
+ """
10
+ Subscriber which works with local eventbus, this class can be initialized and used stand-alone
11
+
12
+ Author: Nicola Ricciardi
13
+ """
14
+
15
+ def __init__(self, eventbus_instance: EventBus, on_event_callback: Callable[[str, Event], None]):
16
+ LocalEventBusSubscriber.__init__(self, eventbus_instance)
17
+ ClosureEventListener.__init__(self, on_event_callback)
@@ -0,0 +1,40 @@
1
+ from abc import ABC
2
+ from src.eventbus.eventbus import EventBus
3
+ from src.eventbus_client.exceptions import EventBusClientNotConnected
4
+ from src.eventbus_client.subscriber.subscriber import Subscriber
5
+
6
+
7
+ class LocalEventBusSubscriber(Subscriber, ABC):
8
+ """
9
+ Abstract subscriber which works with local eventbus without implementing `on_event` method
10
+
11
+ Author: Nicola Ricciardi
12
+ """
13
+
14
+ def __init__(self, eventbus_instance: EventBus):
15
+ Subscriber.__init__(self)
16
+
17
+ self._eventbus = eventbus_instance
18
+ self._connected = False
19
+
20
+ async def connect(self):
21
+ self._connected = True
22
+
23
+ async def disconnect(self):
24
+ self._connected = False
25
+
26
+ async def _internal_subscribe(self, topic_name: str, raise_if_not_connected: bool = False, **kwargs):
27
+ if raise_if_not_connected and not self._connected:
28
+ raise EventBusClientNotConnected()
29
+ else:
30
+ await self.connect()
31
+
32
+ self._eventbus.add_subscriber(topic_name, self)
33
+
34
+ async def _internal_unsubscribe(self, topic_name: str | None = None, raise_if_not_connected: bool = False, **kwargs):
35
+ if raise_if_not_connected and not self._connected:
36
+ raise EventBusClientNotConnected()
37
+ else:
38
+ await self.connect()
39
+
40
+ self._eventbus.remove_subscriber(self, topic_name)
@@ -0,0 +1,93 @@
1
+ from abc import ABC, abstractmethod
2
+ from uuid import uuid4
3
+
4
+ from src.event.event import Event
5
+ from src.eventbus_client.eventbus_connector import EventBusConnector
6
+ from src.eventbus_client.subscriber.event_listener import EventListener
7
+
8
+
9
+ class Subscriber(EventBusConnector, EventListener, ABC):
10
+ """
11
+ Abstract class which can be implemented by your components which must be able to subscribe on eventbus
12
+
13
+ Author: Nicola Ricciardi
14
+ """
15
+
16
+ def __init__(self, subscriber_id: str = str(uuid4())):
17
+ EventBusConnector.__init__(self, subscriber_id)
18
+ EventListener.__init__(self)
19
+
20
+
21
+ @abstractmethod
22
+ async def _internal_subscribe(self, topic_name: str, **kwargs):
23
+ """
24
+ Actual subscribe to topic
25
+
26
+ :param topic_name:
27
+ :return:
28
+ """
29
+
30
+ @abstractmethod
31
+ async def _internal_unsubscribe(self, topic_name: str | None = None, **kwargs):
32
+ """
33
+ Actual unsubscribe to topic
34
+
35
+ :param topic_name:
36
+ :return:
37
+ """
38
+
39
+ async def subscribe(self, topic_name: str, **kwargs):
40
+ """
41
+ Subscribe to topic
42
+
43
+ :param topic_name:
44
+ :return:
45
+ """
46
+
47
+ self.on_subscribing(topic_name)
48
+ await self._internal_subscribe(topic_name, **kwargs)
49
+ self.on_subscribed(topic_name)
50
+
51
+ async def unsubscribe(self, topic_name: str | None = None, **kwargs):
52
+ """
53
+ Unsubscribe to topic
54
+
55
+ :param topic_name:
56
+ :return:
57
+ """
58
+
59
+ self.on_unsubscribing(topic_name)
60
+ await self._internal_unsubscribe(topic_name, **kwargs)
61
+ self.on_unsubscribed(topic_name)
62
+
63
+ def on_subscribing(self, topic_name: str):
64
+ """
65
+ Callback called on subscribing
66
+
67
+ :param topic_name:
68
+ :return:
69
+ """
70
+
71
+ def on_subscribed(self, topic_name: str):
72
+ """
73
+ Callback called on subscribed
74
+
75
+ :param topic_name:
76
+ :return:
77
+ """
78
+
79
+ def on_unsubscribing(self, topic_name: str):
80
+ """
81
+ Callback called on unsubscribing
82
+
83
+ :param topic_name:
84
+ :return:
85
+ """
86
+
87
+ def on_unsubscribed(self, topic_name: str):
88
+ """
89
+ Callback called on unsubscribed
90
+
91
+ :param topic_name:
92
+ :return:
93
+ """