aboutsummaryrefslogtreecommitdiff
path: root/tools/cru-py/cru/excp.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/cru-py/cru/excp.py')
-rw-r--r--tools/cru-py/cru/excp.py93
1 files changed, 47 insertions, 46 deletions
diff --git a/tools/cru-py/cru/excp.py b/tools/cru-py/cru/excp.py
index 6734140..358ad90 100644
--- a/tools/cru-py/cru/excp.py
+++ b/tools/cru-py/cru/excp.py
@@ -1,13 +1,14 @@
from typing import Any
-from .attr import CruAttrDefRegistry, CRU_ATTR_USE_DEFAULT, CruAttr, CruAttrTable
-from .util import CanBeList
+from .attr import CruAttrDefRegistry, CruAttr, CruAttrTable
+from .util import CRU_NOT_FOUND, CruList, CRU_USE_DEFAULT
CRU_EXCEPTION_ATTR_DEF_REGISTRY = CruAttrDefRegistry()
-_REGISTRY = CRU_EXCEPTION_ATTR_DEF_REGISTRY
-class CruException(Exception, CruAttrTable):
+class CruException(Exception):
+ ATTR_REGISTRY = CRU_EXCEPTION_ATTR_DEF_REGISTRY
+
MESSAGE_KEY = "message"
INNER_KEY = "inner"
INTERNAL_KEY = "internal"
@@ -16,82 +17,81 @@ class CruException(Exception, CruAttrTable):
PATH_KEY = "path"
TYPE_KEY = "type_"
- _REGISTRY.make_builder(MESSAGE_KEY, "Message describing the exception.").with_constraint(True, str,
- "").build()
- _REGISTRY.make_builder("inner", "Inner exception.").with_constraint(True, Exception,
- auto_list=True).build()
- _REGISTRY.make_builder("internal",
- "True if the exception is caused by wrong internal logic. False if it is caused by user's wrong input.").with_constraint(
+ ATTR_REGISTRY.make_builder(MESSAGE_KEY, "Message describing the exception.").with_constraint(True, str,
+ "").build()
+ ATTR_REGISTRY.make_builder(INNER_KEY, "Inner exception.").with_constraint(True, Exception,
+ auto_list=True).build()
+ ATTR_REGISTRY.make_builder(INTERNAL_KEY,
+ "True if the exception is caused by wrong internal logic. False if it is caused by user's wrong input.").with_constraint(
True, bool, False).build()
- _REGISTRY.make_builder(NAME_KEY, "Name of the object that causes the exception.").with_types(str).build()
- _REGISTRY.make_builder(VALUE_KEY, "Value that causes the exception.").build()
- _REGISTRY.make_builder(PATH_KEY, "Path that causes the exception.").with_types(str).build()
- _REGISTRY.make_builder(TYPE_KEY, "Python type related to the exception.").with_types(type).build()
+ ATTR_REGISTRY.make_builder(NAME_KEY, "Name of the object that causes the exception.").with_types(str).build()
+ ATTR_REGISTRY.make_builder(VALUE_KEY, "Value that causes the exception.").build()
+ ATTR_REGISTRY.make_builder(PATH_KEY, "Path that causes the exception.").with_types(str).build()
+ ATTR_REGISTRY.make_builder(TYPE_KEY, "Python type related to the exception.").with_types(type).build()
- # TODO: CONTINUE HERE TOMORROW!
def __init__(self, message: str, *args,
- init_attrs: dict[str, Any] | None = None,
- attrs: dict[str, Any] | None = None, **kwargs) -> None:
+ init_attrs: list[CruAttr] | dict[str, Any] | None = None,
+ attrs: list[CruAttr] | dict[str, Any] | None = None, **kwargs) -> None:
super().__init__(message, *args)
- self._attrs = {
- CruException.MESSAGE_KEY: message,
- }
+ self._attrs: CruAttrTable = CruAttrTable(self.ATTR_REGISTRY)
+
+ self._attrs.add_value(CruException.MESSAGE_KEY, message)
if init_attrs is not None:
- self._attrs.update(init_attrs)
+ self._attrs.extend_with(init_attrs, True)
if attrs is not None:
- self._attrs.update(attrs)
- self._attrs.update(kwargs)
+ self._attrs.extend_with(attrs, True)
+ self._attrs.extend_with(dict(kwargs), True)
+
+ @property
+ def attrs(self) -> CruAttrTable:
+ return self._attrs
+
+ def get_attr(self, name: str) -> Any:
+ return self._attrs.get_value_or(name, None)
@property
def message(self) -> str:
- return self[CruException.MESSAGE_KEY]
+ return self.get_attr(CruException.MESSAGE_KEY)
@property
def internal(self) -> bool:
- return self[CruException.INTERNAL_KEY]
+ return self.get_attr(CruException.INTERNAL_KEY)
@property
def inner(self) -> list[Exception]:
- return self[CruException.INNER_KEY]
+ return self.get_attr(CruException.INNER_KEY)
@property
def name(self) -> str | None:
- return self[CruException.NAME_KEY]
+ return self.get_attr(CruException.NAME_KEY)
@property
def value(self) -> Any | None:
- return self[CruException.VALUE_KEY]
+ return self.get_attr(CruException.VALUE_KEY)
@property
def path(self) -> str | None:
- return self[CruException.PATH_KEY]
+ return self.get_attr(CruException.PATH_KEY)
@property
- def type(self) -> type | None:
- return self[CruException.TYPE_KEY]
+ def type_(self) -> type | None:
+ return self.get_attr(CruException.TYPE_KEY)
def _get_attr_list_recursive(self, name: str, depth: int, max_depth: int, l: list[Any]):
if 0 < max_depth < depth + 1:
return
- a = self._attrs.get(name, None)
- if a is not None:
+ a = self._attrs.get_or(name)
+ if a is not CRU_NOT_FOUND:
l.append(a)
for i in self.inner:
if isinstance(i, CruException):
i._get_attr_list_recursive(name, depth + 1, max_depth, l)
- def get_attr_list_recursive(self, name: str, /, max_depth: int = -1) -> list[Any]:
+ def get_attr_list_recursive(self, name: str, /, max_depth: int = -1, des: type = CruList) -> list[Any]:
l = []
self._get_attr_list_recursive(name, 0, max_depth, l)
- return l
-
- def get_optional_attr(self, name: str, max_depth: int = -1) -> Any | None:
- l = self.get_attr_list_recursive(name, max_depth)
- return l[0] if len(l) > 0 else None
-
- def __getitem__(self, name: str) -> Any | None:
- return self.get_optional_attr(name)
+ return des(l)
class CruInternalLogicError(CruException):
@@ -102,14 +102,15 @@ class CruInternalLogicError(CruException):
class UserFriendlyException(CruException):
USER_MESSAGE_KEY = "user_message"
- CRU_EXCEPTION_ATTR_DEF_REGISTRY.register(
- CruExceptionAttrDef(USER_MESSAGE_KEY, "Message describing the exception, but with user-friendly language."))
+ CruException.ATTR_REGISTRY.make_builder(USER_MESSAGE_KEY,
+ "Message describing the exception, but with user-friendly language.").with_types(
+ str).build()
- def __init__(self, message: str, user_message: str | None = None, *args, **kwargs) -> None:
+ def __init__(self, message: str, user_message: str | None = CRU_USE_DEFAULT, *args, **kwargs) -> None:
if user_message is None:
user_message = message
super().__init__(message, *args, init_attrs={UserFriendlyException.USER_MESSAGE_KEY: user_message}, **kwargs)
@property
def user_message(self) -> str:
- return self[UserFriendlyException.USER_MESSAGE_KEY]
+ return self.get_attr(UserFriendlyException.USER_MESSAGE_KEY)