__init__.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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", BaseHook, 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. CPATCH = "cpatch"
  22. UPSERT = "upsert"
  23. DELETE = "delete"
  24. def _cleanup(path):
  25. sane_path = pathutils.strip_path(path)
  26. attributes = sane_path.split("/") if sane_path else []
  27. if len(attributes) < 2:
  28. return ""
  29. return attributes[0] + "/" + attributes[1]
  30. class HookNotificationItem:
  31. def __init__(self, notification_item_type, path, content):
  32. self.type = notification_item_type.value
  33. self.point = _cleanup(path)
  34. self.content = content
  35. def to_json(self):
  36. return json.dumps(
  37. self,
  38. default=lambda o: o.__dict__,
  39. sort_keys=True,
  40. indent=4
  41. )