__init__.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import json
  2. from enum import Enum
  3. from radicale import pathutils, utils
  4. INTERNAL_TYPES = ("none", "rabbitmq")
  5. def load(configuration):
  6. """Load the storage module chosen in configuration."""
  7. return utils.load_plugin(
  8. INTERNAL_TYPES, "hook", "Hook", configuration)
  9. class BaseHook:
  10. def __init__(self, configuration):
  11. """Initialize BaseHook.
  12. ``configuration`` see ``radicale.config`` module.
  13. The ``configuration`` must not change during the lifetime of
  14. this object, it is kept as an internal reference.
  15. """
  16. self.configuration = configuration
  17. def notify(self, notification_item):
  18. """Upload a new or replace an existing item."""
  19. raise NotImplementedError
  20. class HookNotificationItemTypes(Enum):
  21. UPSERT = "upsert"
  22. DELETE = "delete"
  23. def _cleanup(path):
  24. sane_path = pathutils.strip_path(path)
  25. attributes = sane_path.split("/") if sane_path else []
  26. if len(attributes) < 2:
  27. return ""
  28. return attributes[0] + "/" + attributes[1]
  29. class HookNotificationItem:
  30. def __init__(self, notification_item_type, path, content):
  31. self.type = notification_item_type.value
  32. self.point = _cleanup(path)
  33. self.content = content
  34. def to_json(self):
  35. return json.dumps(
  36. self,
  37. default=lambda o: o.__dict__,
  38. sort_keys=True,
  39. indent=4
  40. )