-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1092 from freakmaxi/master
Hook capability for event changes and deletions
- Loading branch information
Showing
11 changed files
with
230 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import json | ||
from enum import Enum | ||
from typing import Sequence | ||
|
||
from radicale import pathutils, utils | ||
|
||
INTERNAL_TYPES: Sequence[str] = ("none", "rabbitmq") | ||
|
||
|
||
def load(configuration): | ||
"""Load the storage module chosen in configuration.""" | ||
return utils.load_plugin( | ||
INTERNAL_TYPES, "hook", "Hook", BaseHook, 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, notification_item): | ||
"""Upload a new or replace an existing item.""" | ||
raise NotImplementedError | ||
|
||
|
||
class HookNotificationItemTypes(Enum): | ||
CPATCH = "cpatch" | ||
UPSERT = "upsert" | ||
DELETE = "delete" | ||
|
||
|
||
def _cleanup(path): | ||
sane_path = pathutils.strip_path(path) | ||
attributes = sane_path.split("/") if sane_path else [] | ||
|
||
if len(attributes) < 2: | ||
return "" | ||
return attributes[0] + "/" + attributes[1] | ||
|
||
|
||
class HookNotificationItem: | ||
|
||
def __init__(self, notification_item_type, path, content): | ||
self.type = notification_item_type.value | ||
self.point = _cleanup(path) | ||
self.content = content | ||
|
||
def to_json(self): | ||
return json.dumps( | ||
self, | ||
default=lambda o: o.__dict__, | ||
sort_keys=True, | ||
indent=4 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from radicale import hook | ||
|
||
|
||
class Hook(hook.BaseHook): | ||
def notify(self, notification_item): | ||
"""Notify nothing. Empty hook.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import pika | ||
from pika.exceptions import ChannelWrongStateError, StreamLostError | ||
|
||
from radicale import hook | ||
from radicale.hook import HookNotificationItem | ||
from radicale.log import logger | ||
|
||
|
||
class Hook(hook.BaseHook): | ||
|
||
def __init__(self, configuration): | ||
super().__init__(configuration) | ||
self._endpoint = configuration.get("hook", "rabbitmq_endpoint") | ||
self._topic = configuration.get("hook", "rabbitmq_topic") | ||
self._queue_type = configuration.get("hook", "rabbitmq_queue_type") | ||
self._encoding = configuration.get("encoding", "stock") | ||
|
||
self._make_connection_synced() | ||
self._make_declare_queue_synced() | ||
|
||
def _make_connection_synced(self): | ||
parameters = pika.URLParameters(self._endpoint) | ||
connection = pika.BlockingConnection(parameters) | ||
self._channel = connection.channel() | ||
|
||
def _make_declare_queue_synced(self): | ||
self._channel.queue_declare(queue=self._topic, durable=True, arguments={"x-queue-type": self._queue_type}) | ||
|
||
def notify(self, notification_item): | ||
if isinstance(notification_item, HookNotificationItem): | ||
self._notify(notification_item, True) | ||
|
||
def _notify(self, notification_item, recall): | ||
try: | ||
self._channel.basic_publish( | ||
exchange='', | ||
routing_key=self._topic, | ||
body=notification_item.to_json().encode( | ||
encoding=self._encoding | ||
) | ||
) | ||
except Exception as e: | ||
if (isinstance(e, ChannelWrongStateError) or | ||
isinstance(e, StreamLostError)) and recall: | ||
self._make_connection_synced() | ||
self._notify(notification_item, False) | ||
return | ||
logger.error("An exception occurred during " | ||
"publishing hook notification item: %s", | ||
e, exc_info=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters