ntprop 0.2.2__tar.gz → 0.2.4__tar.gz
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.
|
@@ -17,7 +17,8 @@ class NTProperty:
|
|
|
17
17
|
self.bindings = []
|
|
18
18
|
self.path = name
|
|
19
19
|
self.default = default
|
|
20
|
-
self.
|
|
20
|
+
self._update(self.default, remote=False)
|
|
21
|
+
# self.cached = self.default
|
|
21
22
|
topic = getattr(NTProperty.nt_instance, f"get{type(self).ty}Topic")(name)
|
|
22
23
|
self.sub = topic.subscribe(default)
|
|
23
24
|
if not readonly or force_publish:
|
|
@@ -30,31 +31,40 @@ class NTProperty:
|
|
|
30
31
|
|
|
31
32
|
def update(evt: ntcore.Event):
|
|
32
33
|
value = getattr(evt.data.value, f"get{type(self).ty}")()
|
|
33
|
-
self.
|
|
34
|
+
self._update(value, remote=True)
|
|
34
35
|
|
|
35
36
|
self.value_listener = NTProperty.nt_instance.addListener(self.sub, ntcore.EventFlags.kValueAll, update)
|
|
36
37
|
|
|
37
38
|
def __del__(self):
|
|
38
|
-
NTProperty.nt_instance
|
|
39
|
+
if NTProperty.nt_instance is None:
|
|
40
|
+
# Shouldn't every happen, but just to be safe
|
|
41
|
+
return
|
|
42
|
+
NTProperty.nt_instance.removeListener(self.value_listener)
|
|
39
43
|
self.sub.close()
|
|
40
44
|
if self.pub is not None:
|
|
41
45
|
self.pub.close()
|
|
42
46
|
|
|
43
|
-
|
|
44
|
-
def cached(self) -> Any:
|
|
45
|
-
return self._cached
|
|
46
|
-
|
|
47
|
-
@cached.setter
|
|
48
|
-
def cached(self, value: Any):
|
|
49
|
-
for binding in self.bindings:
|
|
50
|
-
binding(value)
|
|
47
|
+
def _update(self, value: Any, remote: bool=False):
|
|
51
48
|
self._cached = value
|
|
52
|
-
|
|
53
|
-
|
|
49
|
+
for binding in self.bindings:
|
|
50
|
+
binding(value, remote)
|
|
51
|
+
# @property
|
|
52
|
+
# def cached(self) -> Any:
|
|
53
|
+
# return self._cached
|
|
54
|
+
#
|
|
55
|
+
# @cached.setter
|
|
56
|
+
# def cached(self, value: Any):
|
|
57
|
+
# for binding in self.bindings:
|
|
58
|
+
# binding(value)
|
|
59
|
+
# self._cached = value
|
|
60
|
+
|
|
61
|
+
def bind(self, callback: Callable[[Any, bool], None]):
|
|
54
62
|
self.bindings.append(callback)
|
|
55
63
|
|
|
56
64
|
def get(self) -> Any:
|
|
57
|
-
|
|
65
|
+
"""Returns the current value of the property. The base implementation is sufficient for basic behaviour, however, the
|
|
66
|
+
type signature returns an `Any`. Override the method and return `self._cached` if this is undesirable"""
|
|
67
|
+
return self._cached
|
|
58
68
|
|
|
59
69
|
def set(self, value: Any):
|
|
60
70
|
raise NotImplementedError()
|
|
@@ -66,7 +76,7 @@ class NTPropertyHost:
|
|
|
66
76
|
if not hasattr(self, "_ntdict"):
|
|
67
77
|
self._ntdict = {}
|
|
68
78
|
self._ntdict[value.path] = value
|
|
69
|
-
super().__setattr__(name, value)
|
|
79
|
+
return super().__setattr__(name, value)
|
|
70
80
|
elif isinstance(self.__dict__.get(name), NTProperty):
|
|
71
81
|
self.__dict__[name].set(value)
|
|
72
82
|
return value
|
|
@@ -78,7 +88,7 @@ class NTPropertyHost:
|
|
|
78
88
|
return super().__getattribute__(name).get()
|
|
79
89
|
return super().__getattribute__(name)
|
|
80
90
|
|
|
81
|
-
def bind(self, **kwargs: Callable[[Any], None]):
|
|
91
|
+
def bind(self, **kwargs: Callable[[Any, bool], None]):
|
|
82
92
|
if not hasattr(self, "_ntdict"):
|
|
83
93
|
return
|
|
84
94
|
for name, callback in kwargs.items():
|
|
@@ -92,12 +102,12 @@ class NumberProperty(NTProperty, ty="Double"):
|
|
|
92
102
|
super().__init__(name, default, readonly, force_publish)
|
|
93
103
|
|
|
94
104
|
def get(self) -> float:
|
|
95
|
-
return self.
|
|
105
|
+
return self._cached
|
|
96
106
|
|
|
97
107
|
def set(self, value: float | int):
|
|
98
108
|
if type(value) is not float and type(value) is not int:
|
|
99
109
|
raise ValueError("Can not assign non-numeric to NumberProperty")
|
|
100
|
-
self.
|
|
110
|
+
self._update(value)
|
|
101
111
|
self.pub.set(value)
|
|
102
112
|
|
|
103
113
|
|
|
@@ -107,14 +117,14 @@ class BooleanProperty(NTProperty, ty="Boolean"):
|
|
|
107
117
|
self.strict = strict
|
|
108
118
|
|
|
109
119
|
def get(self) -> bool:
|
|
110
|
-
return self.
|
|
120
|
+
return self._cached
|
|
111
121
|
|
|
112
122
|
def set(self, value: bool):
|
|
113
123
|
if self.strict and type(value) is not bool:
|
|
114
124
|
raise ValueError("Can not assign non-boolean to BooleanProperty in strict mode")
|
|
115
125
|
|
|
116
126
|
value = bool(value)
|
|
117
|
-
self.
|
|
127
|
+
self._update(value)
|
|
118
128
|
self.pub.set(value)
|
|
119
129
|
|
|
120
130
|
|
|
@@ -124,12 +134,12 @@ class StringProperty(NTProperty, ty="String"):
|
|
|
124
134
|
self.strict = strict
|
|
125
135
|
|
|
126
136
|
def get(self) -> str:
|
|
127
|
-
return self.
|
|
137
|
+
return self._cached
|
|
128
138
|
|
|
129
139
|
def set(self, value: str):
|
|
130
140
|
if self.strict and type(value) is not str:
|
|
131
141
|
raise ValueError("Can not assign non-string to StringProperty in strict mode")
|
|
132
142
|
|
|
133
143
|
value = str(value)
|
|
134
|
-
self.
|
|
144
|
+
self._update(value)
|
|
135
145
|
self.pub.set(value)
|
|
File without changes
|
|
File without changes
|