reason.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright 2021-2024 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 typing import Dict, Set
  12. class Reason:
  13. def __init__(self, entity_id: str) -> None:
  14. self.entity_id: str = entity_id
  15. self._reasons: Dict[str, Set[str]] = {}
  16. def _add_reason(self, entity_id: str, reason: str) -> "Reason":
  17. if entity_id not in self._reasons:
  18. self._reasons[entity_id] = set()
  19. self._reasons[entity_id].add(reason)
  20. return self
  21. def _remove_reason(self, entity_id: str, reason: str) -> "Reason":
  22. if entity_id in self._reasons and reason in self._reasons[entity_id]:
  23. self._reasons[entity_id].remove(reason)
  24. if len(self._reasons[entity_id]) == 0:
  25. del self._reasons[entity_id]
  26. return self
  27. def _entity_id_exists_in_reason(self, entity_id: str) -> bool:
  28. return entity_id in self._reasons
  29. def __bool__(self) -> bool:
  30. return len(self._reasons) == 0
  31. @property
  32. def reasons(self) -> str:
  33. return "; ".join("; ".join(reason) for reason in self._reasons.values()) + "." if self._reasons else ""