-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy path__init__.py
71 lines (45 loc) · 1.98 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import sys
from importlib.metadata import PackageNotFoundError, version
from uuid import uuid4
from viam.gen.common.v1.common_pb2 import ResourceName as _ResourceName
from .logging import getLogger as _getLogger
##############
# VERSIONING #
##############
try:
__version__ = version("viam-sdk")
except PackageNotFoundError:
pass
##########################################################
# #################### TASK PREFIX ##################### #
# ###### Used to give each task a unique identifier #### #
# so that tasks spawned by Viam can be canceled on close #
##########################################################
_TASK_PREFIX = uuid4().hex
##########################################################
# ################### LOG EXCEPTIONS ################### #
# Change the default exception handler to log exceptions #
# ############### prior to raising them ################ #
##########################################################
def _log_exceptions(exctype, value, traceback):
_LOGGER = _getLogger(__name__)
_LOGGER.error("[ERROR] Uncaught exception", exc_info=(exctype, value, traceback))
sys.__excepthook__(exctype, value, traceback)
sys.excepthook = _log_exceptions
##################
# MONKEY PATCHES #
##################
def _rname_str(self: _ResourceName) -> str:
return f"{self.namespace}:{self.type}:{self.subtype}/{self.name}"
_ResourceName.__str__ = _rname_str
def _rname_repr(self: _ResourceName) -> str:
return f"<viam.proto.common.ResourceName {str(self)} at {hex(id(self))}>"
_ResourceName.__repr__ = _rname_repr
def _rname_hash(self: _ResourceName) -> int:
return hash(str(self))
_ResourceName.__hash__ = _rname_hash # type: ignore
def _rname_eq(self: _ResourceName, other: object) -> bool:
if isinstance(other, _ResourceName):
return self.__hash__() == other.__hash__() # type: ignore
return False
_ResourceName.__eq__ = _rname_eq # type: ignore