1
0
Fork 0
mirror of https://github.com/Kozea/Radicale.git synced 2025-09-15 20:36:55 +00:00

Addd hook capability

This commit is contained in:
Tuna Celik 2020-08-17 02:05:02 +02:00
parent 03e7e209da
commit 5253a464ab
7 changed files with 103 additions and 2 deletions

25
radicale/hook/__init__.py Normal file
View file

@ -0,0 +1,25 @@
from radicale import utils
INTERNAL_TYPES = ("none", "rabbitmq")
def load(configuration):
"""Load the storage module chosen in configuration."""
return utils.load_plugin(
INTERNAL_TYPES, "hook", "Hook", configuration)
class BaseHook:
def __init__(self, configuration):
"""Initialize BaseHook.
``configuration`` see ``radicale.config`` module.
The ``configuration`` must not change during the lifetime of
this object, it is kept as an internal reference.
"""
self.configuration = configuration
def notify(self, content):
"""Upload a new or replace an existing item."""
raise NotImplementedError

6
radicale/hook/none.py Normal file
View file

@ -0,0 +1,6 @@
from radicale import hook
class Hook(hook.BaseHook):
def notify(self, content):
"""Notify nothing. Empty hook."""

View file

@ -0,0 +1,47 @@
import pika
import json
from radicale import hook
from enum import Enum
class Hook(hook.BaseHook):
def __init__(self, configuration):
super().__init__(configuration)
endpoint = configuration.get("hook", "rabbitmq_endpoint")
self.topic = configuration.get("hook", "rabbitmq_topic")
self.encoding = configuration.get("encoding", "stock")
self._make_connection_synced(endpoint)
self._make_declare_queue_synced(self.topic)
def _make_connection_synced(self, endpoint):
parameters = pika.URLParameters(endpoint)
self.connection = pika.BlockingConnection(parameters)
self.channel = self.connection.channel()
def _make_declare_queue_synced(self, topic):
self.channel.queue_declare(queue=topic)
def notify(self, content):
if not isinstance(content, QueueItem):
return
self.channel.basic_publish(exchange='',
routing_key=self.topic,
body=content.to_json().encode(encoding=self.encoding))
class QueueItemTypes(Enum):
UPSERT = "upsert"
DELETE = "delete"
class QueueItem:
def __init__(self, queue_item_type, content):
self.type = queue_item_type.value
self.content = content
def to_json(self):
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)