21 @brief Base error raised by the Python Nyx binding.
26 @brief Raised when the C shared library cannot be loaded or used.
33c_void_p = ctypes.c_void_p
34c_char_p = ctypes.c_char_p
38c_ulong = ctypes.c_ulong
41c_uint8 = ctypes.c_uint8
43c_uint32 = ctypes.c_uint32
44c_int32 = ctypes.c_int32
45c_uint64 = ctypes.c_uint64
46c_int64 = ctypes.c_int64
47c_size_t = ctypes.c_size_t
49c_float = ctypes.c_float
50c_double = ctypes.c_double
58 @brief JSON object types.
71class nyx_object_t(ctypes.Structure):
82 (
'callback', c_void_p),
88nyx_object_p = ctypes.POINTER(nyx_object_t)
93class nyx_dict_t(ctypes.Structure):
99 (
'base', nyx_object_t),
106nyx_dict_p = ctypes.POINTER(nyx_dict_t)
111class nyx_dict_iter_t(ctypes.Structure):
123nyx_dict_iter_p = ctypes.POINTER(nyx_dict_iter_t)
128class nyx_list_t(ctypes.Structure):
134 (
'base', nyx_object_t),
141nyx_list_p = ctypes.POINTER(nyx_list_t)
146class nyx_list_iter_t(ctypes.Structure):
158nyx_list_iter_p = ctypes.POINTER(nyx_list_iter_t)
162nyx_callback_int_t = ctypes.CFUNCTYPE(
170nyx_callback_uint_t = ctypes.CFUNCTYPE(
178nyx_callback_long_t = ctypes.CFUNCTYPE(
186nyx_callback_ulong_t = ctypes.CFUNCTYPE(
194nyx_callback_double_t = ctypes.CFUNCTYPE(
202nyx_callback_str_t = ctypes.CFUNCTYPE(
210nyx_callback_buffer_t = ctypes.CFUNCTYPE(
218nyx_callback_vector_t = ctypes.CFUNCTYPE(
227class nyx_opts_t(ctypes.Structure):
236 (
'message', c_char_p),
237 (
'timeout', c_double),
242class nyx_node_t(ctypes.Structure):
251nyx_node_p = ctypes.POINTER(nyx_node_t)
255nyx_timer_callback_t = ctypes.CFUNCTYPE(
262nyx_mqtt_handler_t = ctypes.CFUNCTYPE(
278def as_bytes(value: str | bytes, *, allow_none: bool =
True) -> bytes:
283def as_bytes(value: None, *, allow_none: typing.Literal[
True] =
True) ->
None:
288def as_bytes(value: None, *, allow_none: typing.Literal[
False]) -> typing.NoReturn:
293def as_bytes(value: str | bytes |
None, *, allow_none: typing.Literal[
False]) -> bytes:
298def as_bytes(value: str | bytes |
None, *, allow_none: typing.Literal[
True] =
True) -> bytes |
None:
303def as_bytes(value: str | bytes |
None, *, allow_none: bool) -> bytes |
None:
311 raise TypeError(
'None is not allowed here')
317 if isinstance(value, str):
319 return value.encode(
'utf-8')
321 if isinstance(value, bytes):
327 raise TypeError(f
'expected str or bytes, got {type(value).__name__}')
331def as_opts(opts: dict[str, typing.Any] |
None) -> nyx_opts_t |
None:
338 as_bytes(opts.get(
'group'), allow_none =
True),
339 as_bytes(opts.get(
'label'), allow_none =
True),
340 as_bytes(opts.get(
'hints'), allow_none =
True),
341 as_bytes(opts.get(
'message'), allow_none =
True),
342 float(opts.get(
'timeout', 0.0)),
347def check_ptr(ptr: int | c_void_p |
None, what: str =
'C object') -> c_void_p:
351 if isinstance(ptr, c_void_p):
364 raise TypeError(f
'{what} is NULL')
368def take_bytes(ptr: int | c_void_p |
None, size: int) -> bytes:
370 ptr = check_ptr(ptr,
'C buffer')
373 return ctypes.string_at(ptr, size)
375 lib.nyx_memory_free(ptr)
379def take_string(ptr: int | c_void_p |
None, size: int |
None =
None) -> str:
381 ptr = check_ptr(ptr,
'C string')
385 return ctypes.string_at(ptr).decode(
'utf-8')
387 return ctypes.string_at(ptr, size).decode(
'utf-8')
389 lib.nyx_memory_free(ptr)
395def _load_library() -> ctypes.CDLL:
400 names = (
'libnyx-node.dll',
'nyx-node.dll')
402 elif sys.platform ==
'darwin':
403 names = (
'libnyx-node.dylib',
'nyx-node.dylib')
405 pathlib.Path(
'/usr/local/lib'),
406 pathlib.Path(
'/usr/lib'),
407 pathlib.Path(
'/lib'),
410 names = (
'libnyx-node.so',
'nyx-node.so')
412 pathlib.Path(
'/usr/local/lib'),
413 pathlib.Path(
'/usr/lib'),
414 pathlib.Path(
'/lib'),
420 *(str(directory / name)
for directory
in directories
for name
in names),
426 errors: list[str] = []
428 for candidate
in dict.fromkeys(filter(
None, candidates)):
430 if not os.path.dirname(candidate)
or pathlib.Path(candidate).exists():
434 return ctypes.CDLL(candidate)
436 except OSError
as exc:
438 errors.append(f
'{candidate}: {exc}')
442 message =
'Unable to load libnyx-node. Install it in a system library directory.'
444 details =
'\n'.join(errors)
448 message += f
'\nTried:\n{details}'
452 raise NyxLibraryError(message)
462def _bind(name: str, restype, argtypes: typing.Sequence[object]) ->
None:
466 argtypes = list(argtypes)
472 func = getattr(lib, name)
474 func.argtypes = argtypes
475 func.restype = restype
477 except AttributeError
as e:
479 raise NyxLibraryError(f
'Missing C symbol: {name}')
from e
485_bind(
'nyx_set_log_level',
None, [c_int])
491_bind(
'nyx_memory_initialize',
None, [])
492_bind(
'nyx_memory_finalize', c_bool, [])
494_bind(
'nyx_memory_free', c_size_t, [c_void_p])
496_bind(
'nyx_string_ndup', c_void_p, [c_void_p, c_size_t])
497_bind(
'nyx_buffer_ndup', c_void_p, [c_void_p, c_size_t])
503_bind(
'nyx_hash', ctypes.c_uint32, [c_size_t, c_void_p, c_uint32])
505_bind(
'nyx_generate_mac_addr',
None, [c_uint8 * 6, c_uint8, c_uint8, c_char_p])
507_bind(
'nyx_base64_encode', c_void_p, [ctypes.POINTER(c_size_t), c_size_t, c_void_p])
508_bind(
'nyx_base64_decode', c_void_p, [ctypes.POINTER(c_size_t), c_size_t, c_void_p])
514_bind(
'nyx_object_parse', c_void_p, [c_char_p])
515_bind(
'nyx_object_ref', c_void_p, [c_void_p])
516_bind(
'nyx_object_unref', c_void_p, [c_void_p])
517_bind(
'nyx_object_get_type', c_int32, [c_void_p])
518_bind(
'nyx_object_notify', c_bool, [c_void_p])
519_bind(
'nyx_object_to_string', c_void_p, [c_void_p])
520_bind(
'nyx_object_to_cstring', c_void_p, [c_void_p])
526_bind(
'nyx_xmldoc_parse', c_void_p, [c_char_p])
527_bind(
'nyx_xmldoc_free',
None, [c_void_p])
528_bind(
'nyx_xmldoc_to_string', c_void_p, [c_void_p])
534_bind(
'nyx_null_new', c_void_p, [])
536_bind(
'nyx_boolean_new', c_void_p, [])
537_bind(
'nyx_boolean_get', c_bool, [c_void_p])
538_bind(
'nyx_boolean_set', c_bool, [c_void_p, c_bool])
540_bind(
'nyx_string_new', c_void_p, [])
541_bind(
'nyx_string_get', c_char_p, [c_void_p])
542_bind(
'nyx_string_set', c_bool, [c_void_p, c_char_p, c_bool])
544_bind(
'nyx_number_new', c_void_p, [])
545_bind(
'nyx_number_get', c_double, [c_void_p])
546_bind(
'nyx_number_set', c_bool, [c_void_p, c_double])
548_bind(
'nyx_dict_new', c_void_p, [])
549_bind(
'nyx_dict_clear',
None, [c_void_p])
550_bind(
'nyx_dict_del',
None, [c_void_p, c_char_p])
551_bind(
'nyx_dict_iterate', c_bool, [nyx_dict_iter_p, ctypes.POINTER(c_char_p), ctypes.POINTER(c_void_p)])
552_bind(
'nyx_dict_get', c_void_p, [c_void_p, c_char_p])
553_bind(
'nyx_dict_set', c_bool, [c_void_p, c_char_p, c_void_p])
554_bind(
'nyx_dict_size', c_size_t, [c_void_p])
556_bind(
'nyx_list_new', c_void_p, [])
557_bind(
'nyx_list_clear',
None, [c_void_p])
558_bind(
'nyx_list_del',
None, [c_void_p, c_size_t])
559_bind(
'nyx_list_iterate', c_bool, [nyx_list_iter_p, ctypes.POINTER(c_size_t), ctypes.POINTER(c_void_p)])
560_bind(
'nyx_list_get', c_void_p, [c_void_p, c_size_t])
561_bind(
'nyx_list_set', c_bool, [c_void_p, c_size_t, c_void_p])
562_bind(
'nyx_list_size', c_size_t, [c_void_p])
568_bind(
'nyx_message_new', c_void_p, [c_char_p, c_char_p])
572_bind(
'nyx_del_property_new', c_void_p, [c_char_p, c_char_p, c_char_p])
576_bind(
'nyx_number_prop_new_int', c_void_p, [c_char_p, c_char_p, c_char_p, c_int32, c_int32, c_int32, c_int32])
577_bind(
'nyx_number_prop_new_uint', c_void_p, [c_char_p, c_char_p, c_char_p, c_uint32, c_uint32, c_uint32, c_uint32])
578_bind(
'nyx_number_prop_new_long', c_void_p, [c_char_p, c_char_p, c_char_p, c_int64, c_int64, c_int64, c_int64])
579_bind(
'nyx_number_prop_new_ulong', c_void_p, [c_char_p, c_char_p, c_char_p, c_uint64, c_uint64, c_uint64, c_uint64])
580_bind(
'nyx_number_prop_new_double', c_void_p, [c_char_p, c_char_p, c_char_p, c_double, c_double, c_double, c_double])
582_bind(
'nyx_number_prop_set_int', c_bool, [c_void_p, c_int32])
583_bind(
'nyx_number_prop_set_uint', c_bool, [c_void_p, c_uint32])
584_bind(
'nyx_number_prop_set_long', c_bool, [c_void_p, c_int64])
585_bind(
'nyx_number_prop_set_ulong', c_bool, [c_void_p, c_uint64])
586_bind(
'nyx_number_prop_set_double', c_bool, [c_void_p, c_double])
588_bind(
'nyx_number_prop_get_int', c_int32, [c_void_p])
589_bind(
'nyx_number_prop_get_uint', c_uint32, [c_void_p])
590_bind(
'nyx_number_prop_get_long', c_int64, [c_void_p])
591_bind(
'nyx_number_prop_get_ulong', c_uint64, [c_void_p])
592_bind(
'nyx_number_prop_get_double', c_double, [c_void_p])
594_bind(
'nyx_number_vector_new', c_void_p, [c_char_p, c_char_p, c_int, c_int, ctypes.POINTER(nyx_dict_p), ctypes.POINTER(nyx_opts_t)])
598_bind(
"nyx_text_prop_new", c_void_p, [c_char_p, c_char_p, c_char_p, c_bool])
599_bind(
"nyx_text_vector_new", c_void_p, [c_char_p, c_char_p, c_int, c_int, ctypes.POINTER(nyx_dict_p), ctypes.POINTER(nyx_opts_t)])
603_bind(
"nyx_light_prop_new", c_void_p, [c_char_p, c_char_p, c_int])
604_bind(
"nyx_light_vector_new", c_void_p, [c_char_p, c_char_p, c_int, ctypes.POINTER(nyx_dict_p), ctypes.POINTER(nyx_opts_t)])
608_bind(
"nyx_switch_prop_new", c_void_p, [c_char_p, c_char_p, c_int])
609_bind(
"nyx_switch_vector_new", c_void_p, [c_char_p, c_char_p, c_int, c_int, c_int, ctypes.POINTER(nyx_dict_p), ctypes.POINTER(nyx_opts_t)])
613_bind(
"nyx_blob_prop_new", c_void_p, [c_char_p, c_char_p, c_char_p, c_size_t, c_void_p, c_bool])
614_bind(
"nyx_blob_vector_new", c_void_p, [c_char_p, c_char_p, c_int, c_int, ctypes.POINTER(nyx_dict_p), ctypes.POINTER(nyx_opts_t)])
618_bind(
"nyx_stream_prop_new", c_void_p, [c_char_p, c_char_p])
619_bind(
"nyx_stream_vector_new", c_void_p, [c_char_p, c_char_p, c_int, ctypes.POINTER(nyx_dict_p), ctypes.POINTER(nyx_opts_t)])
621_bind(
'nyx_stream_pub', c_bool, [c_void_p, c_size_t, ctypes.POINTER(c_size_t), ctypes.POINTER(c_void_p)])
627_bind(
'nyx_node_initialize', nyx_node_p, [c_char_p, ctypes.POINTER(nyx_dict_p), c_char_p, c_char_p, c_char_p, c_char_p, c_char_p, nyx_mqtt_handler_t, c_uint32, c_bool])
628_bind(
'nyx_node_finalize',
None, [nyx_node_p, c_bool])
630_bind(
'nyx_node_add_timer',
None, [nyx_node_p, c_uint32, nyx_timer_callback_t, c_void_p])
632_bind(
'nyx_node_poll',
None, [nyx_node_p, c_uint32])
634_bind(
'nyx_node_enable',
None, [nyx_node_p, c_char_p, c_char_p, c_char_p])
635_bind(
'nyx_node_disable',
None, [nyx_node_p, c_char_p, c_char_p, c_char_p])
637_bind(
'nyx_node_send_message',
None, [nyx_node_p, c_char_p, c_char_p])
638_bind(
'nyx_node_send_del_property',
None, [nyx_node_p, c_char_p, c_char_p, c_char_p])
640_bind(
'nyx_mqtt_sub',
None, [c_void_p, c_char_p, c_int])
641_bind(
'nyx_mqtt_pub',
None, [c_void_p, c_char_p, c_size_t, c_void_p, c_int])
643_bind(
'nyx_nss_pub',
None, [c_void_p, c_char_p, c_char_p, c_size_t, ctypes.POINTER(c_uint32), ctypes.POINTER(c_size_t), ctypes.POINTER(c_void_p)])
Raised when the C shared library cannot be loaded or used.
Struct describing the options for INDI / Nyx vectors.