ntprop 0.1.0__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.
ntprop-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,17 @@
1
+ Metadata-Version: 2.4
2
+ Name: ntprop
3
+ Version: 0.1.0
4
+ Summary: A way to create transparent NetworkTables variables
5
+ License: MIT
6
+ Author: Kellen Watt
7
+ Author-email: kellen@wattsoft.dev
8
+ Requires-Python: >=3.13
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.13
12
+ Classifier: Programming Language :: Python :: 3.14
13
+ Requires-Dist: pyntcore (>=2026.2.2,<2027.0.0)
14
+ Description-Content-Type: text/markdown
15
+
16
+ # NTProp
17
+
ntprop-0.1.0/README.md ADDED
@@ -0,0 +1 @@
1
+ # NTProp
@@ -0,0 +1 @@
1
+ from .core import NTPropertyHost, StringProperty, NumberProperty, BooleanProperty
@@ -0,0 +1,139 @@
1
+ from typing import Any, ClassVar, Optional
2
+ import ntcore
3
+
4
+ class NTProperty:
5
+ nt_instance: ClassVar[Optional[ntcore.NetworkTableInstance]] = None
6
+
7
+ def __init__(self):
8
+ if NTProperty.nt_instance is None:
9
+ NTProperty.nt_instance = ntcore.NetworkTableInstance.getDefault()
10
+ NTProperty.nt_instance.setServer("localhost", 0)
11
+ NTProperty.nt_instance.startClient4("Reflection")
12
+
13
+ def get(self) -> Any:
14
+ raise NotImplemented
15
+
16
+ def set(self, value: Any):
17
+ raise NotImplemented
18
+
19
+
20
+ class NTPropertyHost:
21
+ def __setattr__(self, name, value):
22
+ if isinstance(name, NTProperty):
23
+ self.__dict__[name].set(value)
24
+ return value
25
+ else:
26
+ return super().__setattr__(name, value)
27
+
28
+ def __getattribute__(self, name):
29
+ if hasattr(self, name) and isinstance(self.__dict__[name], NTProperty):
30
+ return self.__dict__[name].get()
31
+ return super().__getattribute__(name)
32
+
33
+
34
+ class NumberProperty(NTProperty):
35
+ def __init__(self, name: str, default: float | int = 0.0, readonly = False):
36
+ super().__init__()
37
+ self.default = default
38
+ self.cached = self.default
39
+ topic = NTProperty.nt_instance.getDoubleTopic(name)
40
+ self.sub = topic.subscribe(default)
41
+ if not readonly:
42
+ self.pub = topic.publish()
43
+ else:
44
+ self.pub = None
45
+
46
+ def update(evt: ntcore.Event):
47
+ value = value.getDouble()
48
+ self.cached = value
49
+
50
+ self.value_listener = NTProperty.nt_instance.addListener(self.sub, ntcore.EventFlags.kValueAll, update)
51
+
52
+ def __del__(self):
53
+ NTProperty.nt_instance.remove_listener(self.value_listener)
54
+ self.sub.close()
55
+ if self.pub is not None:
56
+ self.pub.close()
57
+
58
+ def get(self) -> float:
59
+ return self.cached
60
+
61
+ def set(self, value: float | int):
62
+ if value is not float and value is not int:
63
+ raise ValueError("Can not assign non-numeric to NumberProperty")
64
+ self.cached = value
65
+ self.pub.set(value)
66
+
67
+
68
+ class BooleanProperty(NTProperty):
69
+ def __init__(self, name: str, default: bool = False, readonly = False, strict: bool = False):
70
+ super().__init__()
71
+ self.default = default
72
+ self.cached = self.default
73
+ self.strict = strict
74
+ topic = NTProperty.nt_instance.getBooleanTopic(name)
75
+ self.sub = topic.subscribe(default)
76
+ if not readonly:
77
+ self.pub = topic.publish()
78
+ else:
79
+ self.pub = None
80
+
81
+ def update(evt: ntcore.Event):
82
+ value = value.getBoolean()
83
+ self.cached = value
84
+
85
+ self.value_listener = NTProperty.nt_instance.addListener(self.sub, ntcore.EventFlags.kValueAll, update)
86
+
87
+ def __del__(self):
88
+ NTProperty.nt_instance.remove_listener(self.value_listener)
89
+ self.sub.close()
90
+ if self.pub is not None:
91
+ self.pub.close()
92
+
93
+ def get(self) -> bool:
94
+ return self.cached
95
+
96
+ def set(self, value: bool):
97
+ if self.strict and value is not bool:
98
+ raise ValueError("Can not assign non-boolean to BooleanProperty in strict mode")
99
+
100
+ value = bool(value)
101
+ self.cached = value
102
+ self.pub.set(value)
103
+
104
+
105
+ class StringProperty(NTProperty):
106
+ def __init__(self, name: str, default: bool, readonly = False, strict: bool = False):
107
+ super().__init__()
108
+ self.default = default
109
+ self.cached = self.default
110
+ self.strict = strict
111
+ topic = NTProperty.nt_instance.getStringTopic(name)
112
+ self.sub = topic.subscribe(default)
113
+ if not readonly:
114
+ self.pub = topic.publish()
115
+ else:
116
+ self.pub = None
117
+
118
+ def update(evt: ntcore.Event):
119
+ value = value.getString()
120
+ self.cached = value
121
+
122
+ self.value_listener = NTProperty.nt_instance.addListener(self.sub, ntcore.EventFlags.kValueAll, update)
123
+
124
+ def __del__(self):
125
+ NTProperty.nt_instance.remove_listener(self.value_listener)
126
+ self.sub.close()
127
+ if self.pub is not None:
128
+ self.pub.close()
129
+
130
+ def get(self) -> str:
131
+ return self.cached
132
+
133
+ def set(self, value: str):
134
+ if self.strict and value is not str:
135
+ raise ValueError("Can not assign non-string to StringProperty in strict mode")
136
+
137
+ value = str(value)
138
+ self.cached = value
139
+ self.pub.set(value)
@@ -0,0 +1,18 @@
1
+ [project]
2
+ name = "ntprop"
3
+ version = "0.1.0"
4
+ description = "A way to create transparent NetworkTables variables"
5
+ authors = [
6
+ {name = "Kellen Watt",email = "kellen@wattsoft.dev"}
7
+ ]
8
+ license = {text = "MIT"}
9
+ readme = "README.md"
10
+ requires-python = ">=3.13"
11
+ dependencies = [
12
+ "pyntcore (>=2026.2.2,<2027.0.0)"
13
+ ]
14
+
15
+
16
+ [build-system]
17
+ requires = ["poetry-core>=2.0.0,<3.0.0"]
18
+ build-backend = "poetry.core.masonry.api"