_registration.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. from queue import SimpleQueue
  12. from typing import Optional, Set
  13. from uuid import uuid4
  14. from ._topic import _Topic
  15. from .event import EventEntityType, EventOperation
  16. from .registration_id import RegistrationId
  17. class _Registration:
  18. _ID_PREFIX = "REGISTRATION"
  19. __SEPARATOR = "_"
  20. def __init__(self) -> None:
  21. self.registration_id: RegistrationId = self._new_id()
  22. self.queue: SimpleQueue = SimpleQueue()
  23. self.topics: Set[_Topic] = set()
  24. @staticmethod
  25. def from_topic(
  26. entity_type: Optional[EventEntityType] = None,
  27. entity_id: Optional[str] = None,
  28. operation: Optional[EventOperation] = None,
  29. attribute_name: Optional[str] = None,
  30. ) -> "_Registration":
  31. reg = _Registration()
  32. reg.topics.add(_Topic(entity_type, entity_id, operation, attribute_name))
  33. return reg
  34. @staticmethod
  35. def _new_id() -> RegistrationId:
  36. """Generate a unique registration identifier."""
  37. return RegistrationId(_Registration.__SEPARATOR.join([_Registration._ID_PREFIX, str(uuid4())]))
  38. def __hash__(self) -> int:
  39. return hash(self.registration_id)
  40. def __eq__(self, other: object) -> bool:
  41. if not isinstance(other, _Registration):
  42. return False
  43. return self.registration_id == other.registration_id
  44. def __ne__(self, other: object) -> bool:
  45. return not self.__eq__(other)
  46. def __str__(self) -> str:
  47. return f"Registration ID: {self.registration_id}, Topics: {self.topics}"
  48. def __repr__(self) -> str:
  49. return self.__str__()
  50. def add_topic(
  51. self,
  52. entity_type: Optional[EventEntityType] = None,
  53. entity_id: Optional[str] = None,
  54. operation: Optional[EventOperation] = None,
  55. attribute_name: Optional[str] = None,
  56. ) -> None:
  57. """Add a topic to the registration."""
  58. self.topics.add(_Topic(entity_type, entity_id, operation, attribute_name))
  59. def remove_topic(
  60. self,
  61. entity_type: Optional[EventEntityType] = None,
  62. entity_id: Optional[str] = None,
  63. operation: Optional[EventOperation] = None,
  64. attribute_name: Optional[str] = None,
  65. ) -> None:
  66. """Remove a topic from the registration."""
  67. self.topics.remove(_Topic(entity_type, entity_id, operation, attribute_name))