blobly 2026.10.1__py3-none-win_amd64.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.
blobly/__init__.py ADDED
@@ -0,0 +1,43 @@
1
+ """blobly — the Blobly Net engine in this Python process, through libblobly's C ABI (cmd/libblobly).
2
+
3
+ import blobly
4
+ with blobly.Project("projects/sim-demo.blobnet") as net:
5
+ can1 = net.channels.CAN1
6
+ net.on_message(can1, can1.messages.Response, lambda f: print("answer", can1.decode(f)))
7
+ net.on_timer(100, lambda: can1.messages.Request.send(ReqCode=5))
8
+ net.run(1000)
9
+
10
+ See docs/scripting_python.md."""
11
+ from ._lib import BloblyError
12
+ from .events import EventLoop
13
+ from .project import Channel, Channels, Frame, Message, Messages, Project, Signal, Signals
14
+
15
+ try:
16
+ from ._version import __version__ # written into a wheel by scripts/build_wheel.py, from v.mod
17
+ except ImportError: # a source checkout: version() says which engine it loaded
18
+ __version__ = "0+source"
19
+
20
+
21
+ def init():
22
+ """Loads the library. Call it on the main thread before any other thread calls in: bb.h
23
+ requires the library be loaded there (any first call on the main thread does the same)."""
24
+ from . import _lib
25
+ _lib.lib()
26
+
27
+
28
+ def version():
29
+ """libblobly's version (v.mod's)."""
30
+ from . import _lib
31
+ return _lib.text(_lib.lib().bb_version())
32
+
33
+
34
+ def wait_released(timeout_ms=None):
35
+ """Waits until the process is free for a start (True), or timeout_ms passes (False; None
36
+ waits for ever). A stop that returned False left a loop letting go of its wires."""
37
+ from . import _lib
38
+ from .project import _ms
39
+ return _lib.check(_lib.lib().bb_wait_released(_ms(timeout_ms))) == 0
40
+
41
+
42
+ __all__ = ["BloblyError", "Channel", "Channels", "EventLoop", "Frame", "Message", "Messages", "Project",
43
+ "Signal", "Signals", "init", "version", "wait_released"]
blobly/_lib.py ADDED
@@ -0,0 +1,208 @@
1
+ """_lib — libblobly loaded into this interpreter, every bb.h call declared once, and the two rules
2
+ bb.h states for a host: the library is loaded from the main thread, and a thread the host created
3
+ attaches before its first call and detaches before it ends. A thread that calls in is attached
4
+ on its first call and detached when it ends, so a user never has to."""
5
+ import collections
6
+ import ctypes
7
+ import os
8
+ import sys
9
+ import threading
10
+
11
+
12
+ class BloblyError(Exception):
13
+ """A call into libblobly refused: the message is bb_last_error's, the code bb.h's."""
14
+
15
+ def __init__(self, message, code=-1):
16
+ super().__init__(message)
17
+ self.code = code
18
+
19
+
20
+ BB_ERR = -1
21
+ BB_ERR_HANDLE = -2
22
+ BB_ERR_STATE = -3
23
+ # answers that are neither success nor an error (bb.h)
24
+ BB_NONE = 1 # bb_bus_recv: no frame in time; bb_decode: no message
25
+ BB_SPENT = 2 # bb_bus_recv(h, 0): no frame yet, records still queued behind the look's bound
26
+ BB_RELEASING = 3 # bb_stop / bb_wait_released: a loop is still letting go of its wires
27
+
28
+ # enum bb_kind and enum bb_health_source
29
+ KINDS = {0: "can", 1: "doip", 2: "someip"}
30
+ BB_HEALTH_SOURCE_NEEDS_READER = 2
31
+
32
+ # bb_health, as transport.health_name spells it (the Lua prelude's bus.health strings)
33
+ HEALTH_NAMES = {0: "", 1: "ok", 2: "error warning", 3: "ERROR-PASSIVE", 4: "BUS-OFF"}
34
+
35
+
36
+ class BbFrame(ctypes.Structure):
37
+ """bb_frame, field for field."""
38
+ _fields_ = [("id", ctypes.c_uint32), ("len", ctypes.c_uint8), ("ext", ctypes.c_uint8),
39
+ ("fd", ctypes.c_uint8), ("brs", ctypes.c_uint8), ("rtr", ctypes.c_uint8),
40
+ ("esi", ctypes.c_uint8), ("data", ctypes.c_uint8 * 64)]
41
+
42
+
43
+ class BbMessage(ctypes.Structure):
44
+ """bb_message, field for field."""
45
+ _fields_ = [("id", ctypes.c_uint32), ("ext", ctypes.c_uint8), ("j1939", ctypes.c_uint8),
46
+ ("dlc", ctypes.c_int32), ("signals", ctypes.c_int32)]
47
+
48
+
49
+ class BbSignal(ctypes.Structure):
50
+ """bb_signal, field for field."""
51
+ _fields_ = [("start_bit", ctypes.c_int32), ("length", ctypes.c_int32),
52
+ ("big_endian", ctypes.c_uint8), ("is_signed", ctypes.c_uint8),
53
+ ("multiplexor", ctypes.c_uint8), ("multiplexed", ctypes.c_uint8),
54
+ ("mux_value", ctypes.c_int32), ("labels", ctypes.c_int32),
55
+ ("factor", ctypes.c_double), ("offset", ctypes.c_double),
56
+ ("minimum", ctypes.c_double), ("maximum", ctypes.c_double)]
57
+
58
+
59
+ def _declare(lib):
60
+ """Every bb.h prototype, argtypes and restype. An int-returning call keeps ctypes' default."""
61
+ c_int, c_char_p, P = ctypes.c_int, ctypes.c_char_p, ctypes.POINTER
62
+ sigs = {
63
+ "bb_version": ([], c_char_p),
64
+ "bb_init": ([], c_int),
65
+ "bb_last_error": ([], c_char_p),
66
+ "bb_thread_attach": ([], c_int),
67
+ "bb_thread_detach": ([], c_int),
68
+ "bb_load": ([c_char_p], c_int),
69
+ "bb_start": ([], c_int),
70
+ "bb_stop": ([], c_int),
71
+ "bb_wait_released": ([c_int], c_int),
72
+ "bb_channel_count": ([], c_int),
73
+ "bb_channel_name": ([c_int], c_char_p),
74
+ "bb_channel_iface": ([c_int], c_char_p),
75
+ "bb_channel_kind": ([c_int], c_int),
76
+ "bb_channel_health_source": ([c_int], c_int),
77
+ "bb_bus_open": ([c_char_p], c_int),
78
+ "bb_bus_send": ([c_int, ctypes.POINTER(BbFrame)], c_int),
79
+ "bb_bus_recv": ([c_int, c_int, ctypes.POINTER(BbFrame)], c_int),
80
+ "bb_bus_health": ([c_int], c_int),
81
+ "bb_bus_close": ([c_int], c_int),
82
+ "bb_message_count": ([c_int], c_int),
83
+ "bb_message_info": ([c_int, c_int, P(BbMessage)], c_int),
84
+ "bb_message_name": ([c_int, c_int, c_char_p, c_int], c_int),
85
+ "bb_signal_info": ([c_int, c_int, c_int, P(BbSignal)], c_int),
86
+ "bb_signal_name": ([c_int, c_int, c_int, c_char_p, c_int], c_int),
87
+ "bb_signal_unit": ([c_int, c_int, c_int, c_char_p, c_int], c_int),
88
+ "bb_signal_label": ([c_int, c_int, c_int, c_int, P(ctypes.c_uint64), P(ctypes.c_double), c_char_p,
89
+ c_int], c_int),
90
+ "bb_encode": ([c_int, c_int, c_int, P(c_char_p), P(ctypes.c_double), P(BbFrame)], c_int),
91
+ "bb_decode": ([c_int, P(BbFrame), P(c_int), P(ctypes.c_double), P(ctypes.c_uint8), c_int], c_int),
92
+ }
93
+ for name, (args, res) in sigs.items():
94
+ fn = getattr(lib, name)
95
+ fn.argtypes = args
96
+ fn.restype = res
97
+
98
+
99
+ def _candidates():
100
+ """Where the library may be: BLOBLY_LIB, else the copy an installed wheel carries (_native/),
101
+ else beside this package, else the repository's build/ (a source checkout)."""
102
+ names = ["blobly.dll"] if sys.platform == "win32" else ["libblobly.so"]
103
+ env = os.environ.get("BLOBLY_LIB")
104
+ if env:
105
+ return [os.path.abspath(env)]
106
+ here = os.path.dirname(os.path.abspath(__file__))
107
+ repo_build = os.path.join(os.path.dirname(os.path.dirname(here)), "build")
108
+ return [os.path.join(d, n) for d in (os.path.join(here, "_native"), here, repo_build) for n in names]
109
+
110
+
111
+ _lock = threading.Lock()
112
+ _lib = None
113
+ loaded_path = None # which file lib() loaded
114
+ _tls = threading.local()
115
+ # the last detaches the finalizer did, as (thread ident, bb_thread_detach's answer): what a test
116
+ # reads to see that a thread was let go on itself; bounded, so a thread-per-job host keeps nothing
117
+ detached = collections.deque(maxlen=64)
118
+ # which Project's load the library holds now, and which load that was: a channel of another
119
+ # project, or of an earlier load of its own, would reach another row by its index; and which
120
+ # run, moved by each start and stop: a row's databases are the run's, so a message of an
121
+ # earlier run would reach another message by its index
122
+ state = {"owner": None, "gen": 0, "run": 0}
123
+ # bb.h: open, close, load, start and stop are not synchronised against each other, and ctypes
124
+ # releases the GIL inside a call, so every one of them (and the row-name reads a load replaces)
125
+ # runs under this lock, from whichever thread makes it
126
+ state_lock = threading.RLock()
127
+
128
+
129
+ class _Attachment:
130
+ """Held in the thread's local storage: CPython clears that storage on the ending thread itself,
131
+ so this finalizer runs there and detaches the thread it attached."""
132
+
133
+ def __init__(self, lib):
134
+ self.lib = lib
135
+
136
+ def __del__(self):
137
+ try:
138
+ detached.append((threading.get_ident(), self.lib.bb_thread_detach()))
139
+ except Exception: # the interpreter may be shutting down; nothing to report to
140
+ pass
141
+
142
+
143
+ def lib():
144
+ """The loaded library, bb_init done; refused from any thread but the main one the first time."""
145
+ global _lib, loaded_path
146
+ with _lock:
147
+ if _lib is None:
148
+ # the thread rule below rests on CPython clearing a thread's local storage ON that
149
+ # thread as it ends; a tracing collector (PyPy) may finalize later, or elsewhere
150
+ if sys.implementation.name != "cpython":
151
+ raise BloblyError("blobly needs CPython: it detaches a thread from the library "
152
+ "as CPython ends it, which " + sys.implementation.name +
153
+ " does not promise")
154
+ if threading.current_thread() is not threading.main_thread():
155
+ raise BloblyError("load blobly from the main thread first: bb.h requires the "
156
+ "library be loaded there (its collector takes the loading thread "
157
+ "for the main one)")
158
+ tried = []
159
+ for path in _candidates():
160
+ if os.path.exists(path):
161
+ loaded = ctypes.CDLL(path)
162
+ _declare(loaded)
163
+ if loaded.bb_init() != 0:
164
+ raise BloblyError(text(loaded.bb_last_error()))
165
+ _lib = loaded
166
+ loaded_path = path
167
+ break
168
+ tried.append(path)
169
+ if _lib is None:
170
+ raise BloblyError("libblobly not found (set BLOBLY_LIB, or build it with "
171
+ "scripts/build_libblobly.sh); looked at: " + ", ".join(tried))
172
+ attach()
173
+ return _lib
174
+
175
+
176
+ def attach():
177
+ """Attaches the calling thread on its first call in, unless it is the main thread."""
178
+ if threading.current_thread() is threading.main_thread() or getattr(_tls, "held", None):
179
+ return
180
+ if _lib.bb_thread_attach() != 0:
181
+ raise BloblyError(text(_lib.bb_last_error()))
182
+ _tls.held = _Attachment(_lib)
183
+
184
+
185
+ def text(b):
186
+ """A string from the library. Its messages are cut before a whole character past
187
+ BB_ERROR_MAX bytes, but a project's own names need not be UTF-8: an undecodable byte must not
188
+ hide an error behind a UnicodeDecodeError."""
189
+ return b.decode("utf-8", errors="replace") if b is not None else ""
190
+
191
+
192
+ def string(get, *at):
193
+ """A string a bb_* call copies into the caller's buffer (bb.h), read whole: the call answers
194
+ the string's length, so one that did not fit is asked for again with room for it."""
195
+ cap = 256
196
+ while True:
197
+ buf = ctypes.create_string_buffer(cap)
198
+ n = check(get(*at, buf, cap))
199
+ if n < cap:
200
+ return text(buf.raw[:n])
201
+ cap = n + 1
202
+
203
+
204
+ def check(rc):
205
+ """A bb_* return value: itself when >= 0, a BloblyError with bb_last_error's message otherwise."""
206
+ if rc < 0:
207
+ raise BloblyError(text(_lib.bb_last_error()), rc)
208
+ return rc
Binary file
Binary file
Binary file
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Marten Hildell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,16 @@
1
+
2
+ Copyright (c) 1994 by Xerox Corporation. All rights reserved.
3
+ Copyright (c) 1996 by Silicon Graphics. All rights reserved.
4
+ Copyright (c) 1998 by Fergus Henderson. All rights reserved.
5
+ Copyright (c) 2000-2009 by Hewlett-Packard Development Company.
6
+ All rights reserved.
7
+ Copyright (c) 2009-2025 Ivan Maidanski
8
+
9
+ THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
10
+ OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
11
+
12
+ Permission is hereby granted to use or copy this program
13
+ for any purpose, provided the above notices are retained on all copies.
14
+ Permission to modify the code and to distribute modified code is granted,
15
+ provided the above notices are retained, and a notice that the code was
16
+ modified is included with the above copyright notice.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Marten Hildell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,73 @@
1
+ GCC RUNTIME LIBRARY EXCEPTION
2
+
3
+ Version 3.1, 31 March 2009
4
+
5
+ Copyright (C) 2009 Free Software Foundation, Inc. <http://fsf.org/>
6
+
7
+ Everyone is permitted to copy and distribute verbatim copies of this
8
+ license document, but changing it is not allowed.
9
+
10
+ This GCC Runtime Library Exception ("Exception") is an additional
11
+ permission under section 7 of the GNU General Public License, version
12
+ 3 ("GPLv3"). It applies to a given file (the "Runtime Library") that
13
+ bears a notice placed by the copyright holder of the file stating that
14
+ the file is governed by GPLv3 along with this Exception.
15
+
16
+ When you use GCC to compile a program, GCC may combine portions of
17
+ certain GCC header files and runtime libraries with the compiled
18
+ program. The purpose of this Exception is to allow compilation of
19
+ non-GPL (including proprietary) programs to use, in this way, the
20
+ header files and runtime libraries covered by this Exception.
21
+
22
+ 0. Definitions.
23
+
24
+ A file is an "Independent Module" if it either requires the Runtime
25
+ Library for execution after a Compilation Process, or makes use of an
26
+ interface provided by the Runtime Library, but is not otherwise based
27
+ on the Runtime Library.
28
+
29
+ "GCC" means a version of the GNU Compiler Collection, with or without
30
+ modifications, governed by version 3 (or a specified later version) of
31
+ the GNU General Public License (GPL) with the option of using any
32
+ subsequent versions published by the FSF.
33
+
34
+ "GPL-compatible Software" is software whose conditions of propagation,
35
+ modification and use would permit combination with GCC in accord with
36
+ the license of GCC.
37
+
38
+ "Target Code" refers to output from any compiler for a real or virtual
39
+ target processor architecture, in executable form or suitable for
40
+ input to an assembler, loader, linker and/or execution
41
+ phase. Notwithstanding that, Target Code does not include data in any
42
+ format that is used as a compiler intermediate representation, or used
43
+ for producing a compiler intermediate representation.
44
+
45
+ The "Compilation Process" transforms code entirely represented in
46
+ non-intermediate languages designed for human-written code, and/or in
47
+ Java Virtual Machine byte code, into Target Code. Thus, for example,
48
+ use of source code generators and preprocessors need not be considered
49
+ part of the Compilation Process, since the Compilation Process can be
50
+ understood as starting with the output of the generators or
51
+ preprocessors.
52
+
53
+ A Compilation Process is "Eligible" if it is done using GCC, alone or
54
+ with other GPL-compatible software, or if it is done without using any
55
+ work based on GCC. For example, using non-GPL-compatible Software to
56
+ optimize any GCC intermediate representations would not qualify as an
57
+ Eligible Compilation Process.
58
+
59
+ 1. Grant of Additional Permission.
60
+
61
+ You have permission to propagate a work of Target Code formed by
62
+ combining the Runtime Library with Independent Modules, even if such
63
+ propagation would otherwise violate the terms of GPLv3, provided that
64
+ all Target Code was generated by Eligible Compilation Processes. You
65
+ may then convey such a combination under terms of your choice,
66
+ consistent with the licensing of the Independent Modules.
67
+
68
+ 2. No Weakening of GCC Copyleft.
69
+
70
+ The availability of this Exception does not imply any general
71
+ presumption that third-party software is unaffected by the copyleft
72
+ requirements of the license of GCC.
73
+