foxglove-sdk 0.16.5__cp310-cp310-musllinux_1_2_x86_64.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,166 @@
1
+ """
2
+ This module contains the definitions of the well-known Foxglove schemas for logging.
3
+
4
+ Log messages to a corresponding channel type from :py:mod:`foxglove.channels`.
5
+
6
+ Note that the schema classes are currently immutable and do not expose
7
+ getters and setters for their fields. This is a limitation we plan to address in the future.
8
+ """
9
+
10
+ # Generated by https://github.com/foxglove/foxglove-sdk
11
+
12
+ from typing import Union
13
+
14
+ from foxglove._foxglove_py.schemas import (
15
+ ArrowPrimitive,
16
+ CameraCalibration,
17
+ CircleAnnotation,
18
+ Color,
19
+ CompressedImage,
20
+ CompressedVideo,
21
+ CubePrimitive,
22
+ CylinderPrimitive,
23
+ Duration,
24
+ FrameTransform,
25
+ FrameTransforms,
26
+ GeoJson,
27
+ Grid,
28
+ ImageAnnotations,
29
+ KeyValuePair,
30
+ LaserScan,
31
+ LinePrimitive,
32
+ LinePrimitiveLineType,
33
+ LocationFix,
34
+ LocationFixes,
35
+ LocationFixPositionCovarianceType,
36
+ Log,
37
+ LogLevel,
38
+ ModelPrimitive,
39
+ PackedElementField,
40
+ PackedElementFieldNumericType,
41
+ Point2,
42
+ Point3,
43
+ Point3InFrame,
44
+ PointCloud,
45
+ PointsAnnotation,
46
+ PointsAnnotationType,
47
+ Pose,
48
+ PoseInFrame,
49
+ PosesInFrame,
50
+ Quaternion,
51
+ RawAudio,
52
+ RawImage,
53
+ SceneEntity,
54
+ SceneEntityDeletion,
55
+ SceneEntityDeletionType,
56
+ SceneUpdate,
57
+ SpherePrimitive,
58
+ TextAnnotation,
59
+ TextPrimitive,
60
+ Timestamp,
61
+ TriangleListPrimitive,
62
+ Vector2,
63
+ Vector3,
64
+ VoxelGrid,
65
+ )
66
+
67
+ FoxgloveSchema = Union[
68
+ ArrowPrimitive,
69
+ CameraCalibration,
70
+ CircleAnnotation,
71
+ Color,
72
+ CompressedImage,
73
+ CompressedVideo,
74
+ CubePrimitive,
75
+ CylinderPrimitive,
76
+ Duration,
77
+ FrameTransform,
78
+ FrameTransforms,
79
+ GeoJson,
80
+ Grid,
81
+ ImageAnnotations,
82
+ KeyValuePair,
83
+ LaserScan,
84
+ LinePrimitive,
85
+ LocationFix,
86
+ LocationFixes,
87
+ Log,
88
+ ModelPrimitive,
89
+ PackedElementField,
90
+ Point2,
91
+ Point3,
92
+ Point3InFrame,
93
+ PointCloud,
94
+ PointsAnnotation,
95
+ Pose,
96
+ PoseInFrame,
97
+ PosesInFrame,
98
+ Quaternion,
99
+ RawAudio,
100
+ RawImage,
101
+ SceneEntity,
102
+ SceneEntityDeletion,
103
+ SceneUpdate,
104
+ SpherePrimitive,
105
+ TextAnnotation,
106
+ TextPrimitive,
107
+ Timestamp,
108
+ TriangleListPrimitive,
109
+ Vector2,
110
+ Vector3,
111
+ VoxelGrid,
112
+ ]
113
+
114
+ __all__ = [
115
+ "FoxgloveSchema",
116
+ "ArrowPrimitive",
117
+ "CameraCalibration",
118
+ "CircleAnnotation",
119
+ "Color",
120
+ "CompressedImage",
121
+ "CompressedVideo",
122
+ "CubePrimitive",
123
+ "CylinderPrimitive",
124
+ "Duration",
125
+ "FrameTransform",
126
+ "FrameTransforms",
127
+ "GeoJson",
128
+ "Grid",
129
+ "ImageAnnotations",
130
+ "KeyValuePair",
131
+ "LaserScan",
132
+ "LinePrimitive",
133
+ "LinePrimitiveLineType",
134
+ "LocationFix",
135
+ "LocationFixPositionCovarianceType",
136
+ "LocationFixes",
137
+ "Log",
138
+ "LogLevel",
139
+ "ModelPrimitive",
140
+ "PackedElementField",
141
+ "PackedElementFieldNumericType",
142
+ "Point2",
143
+ "Point3",
144
+ "Point3InFrame",
145
+ "PointCloud",
146
+ "PointsAnnotation",
147
+ "PointsAnnotationType",
148
+ "Pose",
149
+ "PoseInFrame",
150
+ "PosesInFrame",
151
+ "Quaternion",
152
+ "RawAudio",
153
+ "RawImage",
154
+ "SceneEntity",
155
+ "SceneEntityDeletion",
156
+ "SceneEntityDeletionType",
157
+ "SceneUpdate",
158
+ "SpherePrimitive",
159
+ "TextAnnotation",
160
+ "TextPrimitive",
161
+ "Timestamp",
162
+ "TriangleListPrimitive",
163
+ "Vector2",
164
+ "Vector3",
165
+ "VoxelGrid",
166
+ ]
File without changes
@@ -0,0 +1,243 @@
1
+ import json
2
+ import logging
3
+ import random
4
+
5
+ import pytest
6
+ from foxglove import Channel, Context, Schema
7
+ from foxglove.channels import LogChannel
8
+ from foxglove.schemas import Log
9
+
10
+
11
+ @pytest.fixture
12
+ def new_topic() -> str:
13
+ return f"/{random.random()}"
14
+
15
+
16
+ def test_warns_on_duplicate_topics(caplog: pytest.LogCaptureFixture) -> None:
17
+ schema = {"type": "object"}
18
+ c1 = Channel("test-duplicate", schema=schema)
19
+ c2 = Channel("test-duplicate", schema=schema)
20
+ assert c1.id() == c2.id()
21
+
22
+ with caplog.at_level(logging.WARNING):
23
+ # Same topic, different schema
24
+ c3 = Channel(
25
+ "test-duplicate",
26
+ schema={
27
+ "type": "object",
28
+ "additionalProperties": False,
29
+ },
30
+ )
31
+ assert c1.id() != c3.id()
32
+
33
+ assert len(caplog.records) == 1
34
+ for _, _, message in caplog.record_tuples:
35
+ assert (
36
+ "Channel with topic test-duplicate already exists in this context"
37
+ in message
38
+ )
39
+
40
+
41
+ def test_does_not_warn_on_duplicate_topics_in_contexts(
42
+ caplog: pytest.LogCaptureFixture,
43
+ ) -> None:
44
+ ctx1 = Context()
45
+ ctx2 = Context()
46
+
47
+ _ = Channel("test-duplicate", context=ctx1)
48
+
49
+ with caplog.at_level(logging.WARNING):
50
+ Channel("test-duplicate", context=ctx2)
51
+
52
+ assert len(caplog.records) == 0
53
+
54
+
55
+ def test_requires_an_object_schema(new_topic: str) -> None:
56
+ schema = {"type": "array"}
57
+ with pytest.raises(ValueError, match="Only object schemas are supported"):
58
+ Channel(new_topic, schema=schema)
59
+
60
+
61
+ def test_log_dict_on_json_channel(new_topic: str) -> None:
62
+ json_schema = {"type": "object", "additionalProperties": True}
63
+ channel = Channel(new_topic, schema=json_schema)
64
+
65
+ assert channel.message_encoding == "json"
66
+
67
+ schema = channel.schema()
68
+ assert schema is not None
69
+ assert schema.encoding == "jsonschema"
70
+ assert json.loads(schema.data) == json_schema
71
+
72
+ channel.log({"test": "test"})
73
+
74
+
75
+ def test_log_dict_on_schemaless_channel(new_topic: str) -> None:
76
+ channel = Channel(new_topic)
77
+ assert channel.message_encoding == "json"
78
+
79
+ schema = channel.schema()
80
+ assert schema is not None
81
+ assert schema.encoding == "jsonschema"
82
+ assert schema.data == b""
83
+
84
+ channel.log({"test": "test"})
85
+
86
+
87
+ def test_log_dict_with_empty_schema(new_topic: str) -> None:
88
+ channel = Channel(new_topic, schema={})
89
+ assert channel.message_encoding == "json"
90
+
91
+ schema = channel.schema()
92
+ assert schema is not None
93
+ assert schema.encoding == "jsonschema"
94
+ assert schema.data == b""
95
+
96
+ channel.log({"test": "test"})
97
+
98
+
99
+ def test_log_dict_on_schemaless_json_channel(new_topic: str) -> None:
100
+ channel = Channel(
101
+ new_topic,
102
+ message_encoding="json",
103
+ )
104
+ assert channel.message_encoding == "json"
105
+
106
+ schema = channel.schema()
107
+ assert schema is not None
108
+ assert schema.encoding == "jsonschema"
109
+ assert schema.data == b""
110
+
111
+ channel.log({"test": "test"})
112
+
113
+
114
+ def test_log_must_serialize_on_protobuf_channel(new_topic: str) -> None:
115
+ schema = Schema(
116
+ name="my_schema",
117
+ encoding="protobuf",
118
+ data=b"\x01",
119
+ )
120
+ channel = Channel(
121
+ new_topic,
122
+ message_encoding="protobuf",
123
+ schema=schema,
124
+ )
125
+
126
+ assert channel.message_encoding == "protobuf"
127
+ assert channel.schema() == schema
128
+
129
+ with pytest.raises(TypeError, match="Unsupported message type"):
130
+ channel.log({"test": "test"})
131
+
132
+ channel.log(b"\x01")
133
+
134
+
135
+ def test_channel_attributes(new_topic: str) -> None:
136
+ channel = Channel(new_topic, message_encoding="json")
137
+ assert channel.topic() == new_topic
138
+ assert channel.message_encoding == "json"
139
+ assert channel.schema() is not None
140
+ assert channel.metadata() == {}
141
+ assert not channel.has_sinks()
142
+
143
+
144
+ def test_typed_channel_attributes(new_topic: str) -> None:
145
+ channel = LogChannel(new_topic)
146
+ assert channel.topic() == new_topic
147
+ assert channel.message_encoding == "protobuf"
148
+ assert channel.schema() == Log.get_schema()
149
+ assert channel.metadata() == {}
150
+ assert not channel.has_sinks()
151
+
152
+
153
+ def test_channel_metadata(new_topic: str) -> None:
154
+ channel = Channel(new_topic, metadata={"foo": "bar"})
155
+ assert channel.metadata() == {"foo": "bar"}
156
+
157
+
158
+ def test_channel_metadata_mistyped(new_topic: str) -> None:
159
+ with pytest.raises(TypeError, match="argument 'metadata'"):
160
+ Channel(new_topic, metadata={"1": 1}) # type: ignore
161
+
162
+
163
+ def test_typed_channel_metadata(new_topic: str) -> None:
164
+ channel = LogChannel(new_topic, metadata={"foo": "bar"})
165
+ assert channel.metadata() == {"foo": "bar"}
166
+ channel = LogChannel(new_topic, context=Context(), metadata={"foo": "baz"})
167
+ assert channel.metadata() == {"foo": "baz"}
168
+
169
+
170
+ def test_typed_channel_metadata_mistyped(new_topic: str) -> None:
171
+ with pytest.raises(TypeError, match="argument 'metadata'"):
172
+ LogChannel(new_topic, metadata={"1": 1}) # type: ignore
173
+
174
+
175
+ def test_closed_channel_log(new_topic: str, caplog: pytest.LogCaptureFixture) -> None:
176
+ channel = Channel(new_topic, schema={"type": "object"})
177
+ channel.close()
178
+ with caplog.at_level(logging.WARNING):
179
+ channel.log(b"\x01")
180
+
181
+ assert len(caplog.records) == 1
182
+ for log_name, _, message in caplog.record_tuples:
183
+ assert log_name == "foxglove.channel.raw_channel"
184
+ assert message == f"Cannot log on closed channel for {new_topic}"
185
+
186
+
187
+ def test_close_typed_channel(new_topic: str, caplog: pytest.LogCaptureFixture) -> None:
188
+ channel = LogChannel(new_topic)
189
+ channel.close()
190
+ with caplog.at_level(logging.WARNING):
191
+ channel.log(Log())
192
+
193
+ assert len(caplog.records) == 1
194
+ for log_name, _, message in caplog.record_tuples:
195
+ assert log_name == "foxglove.channel.raw_channel"
196
+ assert message == f"Cannot log on closed channel for {new_topic}"
197
+
198
+
199
+ def test_typed_channel_requires_kwargs_after_message(new_topic: str) -> None:
200
+ channel = LogChannel(new_topic)
201
+
202
+ channel.log(Log(), log_time=0)
203
+
204
+ with pytest.raises(
205
+ TypeError,
206
+ match="takes 1 positional arguments but 2 were given",
207
+ ):
208
+ channel.log(Log(), 0) # type: ignore
209
+
210
+
211
+ def test_generates_names_for_schemas(new_topic: str) -> None:
212
+ ch_1 = Channel(
213
+ new_topic + "-1",
214
+ schema={"type": "object", "properties": {"foo": {"type": "string"}}},
215
+ )
216
+ ch_2 = Channel(
217
+ new_topic + "-2",
218
+ schema={"type": "object", "additionalProperties": True},
219
+ )
220
+ # Same schema will have the same name
221
+ ch_3 = Channel(
222
+ new_topic + "-3",
223
+ schema={"type": "object", "additionalProperties": True},
224
+ )
225
+
226
+ assert ch_1.schema_name() != ch_2.schema_name()
227
+ assert ch_2.schema_name() == ch_3.schema_name()
228
+
229
+
230
+ def test_exposes_unique_channel_ids(new_topic: str) -> None:
231
+ ch_1 = Channel(new_topic + "-1")
232
+ ch_2 = Channel(new_topic + "-2")
233
+ ch_3 = LogChannel(new_topic + "-3")
234
+
235
+ assert ch_1.id() > 0
236
+ assert ch_1.id() < ch_2.id()
237
+ assert ch_2.id() < ch_3.id()
238
+
239
+
240
+ def test_log_message_to_specific_sink(new_topic: str) -> None:
241
+ ctx = Context()
242
+ ch = Channel(new_topic, context=ctx)
243
+ ch.log("test", sink_id=1)
@@ -0,0 +1,10 @@
1
+ from foxglove import Context
2
+
3
+
4
+ def test_default_context_is_singleton() -> None:
5
+ assert Context.default() is Context.default()
6
+
7
+
8
+ def test_context_is_distinct() -> None:
9
+ assert Context() is not Context.default()
10
+ assert Context() is not Context()
@@ -0,0 +1,62 @@
1
+ import logging
2
+ import os
3
+ import subprocess
4
+ import sys
5
+
6
+ import pytest
7
+ from foxglove import set_log_level
8
+
9
+
10
+ def test_set_log_level_accepts_string_or_int() -> None:
11
+ set_log_level("DEBUG")
12
+ set_log_level(logging.DEBUG)
13
+ with pytest.raises(ValueError):
14
+ set_log_level("debug")
15
+
16
+
17
+ def test_set_log_level_clamps_illegal_values() -> None:
18
+ set_log_level(-1)
19
+ set_log_level(2**64)
20
+
21
+
22
+ def test_logging_config_with_env() -> None:
23
+ # Run a script in a child process so logger can be re-initialized from env.
24
+ test_script = """
25
+ import logging
26
+ import foxglove
27
+
28
+ logging.basicConfig(level=logging.DEBUG)
29
+
30
+ server = foxglove.start_server(port=0)
31
+ server.stop()
32
+
33
+ print("test_init_with_env_complete")
34
+ """
35
+
36
+ # Default: unset
37
+ env = os.environ.copy()
38
+ env["FOXGLOVE_LOG_LEVEL"] = ""
39
+
40
+ result = subprocess.run(
41
+ [sys.executable, "-c", test_script],
42
+ env=env,
43
+ capture_output=True,
44
+ text=True,
45
+ timeout=5,
46
+ )
47
+ assert "test_init_with_env_complete" in result.stdout
48
+ assert "Started server" in result.stderr
49
+
50
+ # Quiet the WS server logging
51
+ env = os.environ.copy()
52
+ env["FOXGLOVE_LOG_LEVEL"] = "debug,foxglove::websocket::server=warn"
53
+
54
+ result = subprocess.run(
55
+ [sys.executable, "-c", test_script],
56
+ env=env,
57
+ capture_output=True,
58
+ text=True,
59
+ timeout=5,
60
+ )
61
+ assert "test_init_with_env_complete" in result.stdout
62
+ assert "Started server" not in result.stderr