omextra 0.0.0.dev472__py3-none-any.whl → 0.0.0.dev492__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,49 @@
1
+ # ruff: noqa: UP007
2
+ # @omlish-lite
3
+ import abc
4
+ import dataclasses as dc
5
+ import typing as ta
6
+
7
+ from omlish.lite.abstract import Abstract
8
+
9
+
10
+ T = ta.TypeVar('T')
11
+
12
+ YamlErrorOr = ta.Union['YamlError', T] # ta.TypeAlias
13
+
14
+
15
+ ##
16
+
17
+
18
+ class YamlError(Abstract):
19
+ @property
20
+ @abc.abstractmethod
21
+ def message(self) -> str:
22
+ raise NotImplementedError
23
+
24
+
25
+ class EofYamlError(YamlError):
26
+ @property
27
+ def message(self) -> str:
28
+ return 'eof'
29
+
30
+
31
+ @dc.dataclass(frozen=True)
32
+ class GenericYamlError(YamlError):
33
+ obj: ta.Union[str, Exception]
34
+
35
+ @property
36
+ def message(self) -> str:
37
+ if isinstance(self.obj, str):
38
+ return self.obj
39
+ else:
40
+ return str(self.obj)
41
+
42
+
43
+ def yaml_error(obj: ta.Union[YamlError, str, Exception]) -> YamlError:
44
+ if isinstance(obj, YamlError):
45
+ return obj
46
+ elif isinstance(obj, (str, Exception)):
47
+ return GenericYamlError(obj)
48
+ else:
49
+ raise TypeError(obj)