|
1 | | -# Python WebUI v2.5.1 |
| 1 | +# Python WebUI v2.5.2 |
2 | 2 | # |
3 | 3 | # http://webui.me |
4 | 4 | # https://github.com/webui-dev/python-webui |
|
11 | 11 | # webui.py |
12 | 12 | from __future__ import annotations |
13 | 13 |
|
| 14 | +import array |
14 | 15 | import warnings |
15 | 16 | from typing import Any, Callable, Optional, TypeAlias |
16 | 17 | from ctypes import * |
@@ -209,29 +210,45 @@ def close_client(self) -> None: |
209 | 210 | _raw.webui_close_client(byref(self._c_event())) |
210 | 211 |
|
211 | 212 | # -- send_raw_client ---------------------------- |
212 | | - def send_raw_client(self, function: str, raw: Optional[int], size: int) -> None: |
| 213 | + def send_raw_client(self, function: str, data: Union[bytes, bytearray, memoryview, array.array]) -> None: |
213 | 214 | """Safely send raw data to the UI for a single client. |
214 | 215 |
|
215 | 216 | This function sends raw data to a JavaScript function in the UI. The JavaScript function must |
216 | 217 | be defined to accept the raw data, such as: `function myFunc(myData) {}`. |
217 | 218 |
|
218 | 219 | Args: |
219 | | - function (str): The name of the JavaScript function to receive the raw data, encoded in UTF-8. |
220 | | - raw (Optional[int]): The pointer to the raw data buffer. Must not be `None`. |
221 | | - size (int): The size of the raw data buffer in bytes. |
| 220 | + function (str): The name of the JavaScript function to receive the raw data. |
| 221 | + data (Union[bytes, bytearray, memoryview, array.array]): The raw data buffer. |
222 | 222 |
|
223 | 223 | Raises: |
224 | | - ValueError: If `raw` is `None`. |
| 224 | + ValueError: If `data` is `None` or empty. |
225 | 225 |
|
226 | 226 | Example: |
227 | | - e.send_raw_client("myJavaScriptFunc", my_buffer, 64) |
| 227 | + e.send_raw_client("myJavaScriptFunc", bytearray([0x01, 0x0A, 0xFF])) |
| 228 | + # Sends 3 bytes of raw data to the JavaScript function `myJavaScriptFunc`. |
228 | 229 | """ |
229 | | - if raw is None: |
230 | | - raise ValueError("Invalid Pointer: Cannot send a null pointer.") |
| 230 | + if data is None or len(data) == 0: |
| 231 | + raise ValueError("Data must not be None or empty.") |
| 232 | + |
| 233 | + # Ensure data is a memoryview for uniformity |
| 234 | + if not isinstance(data, memoryview): |
| 235 | + data = memoryview(data) |
| 236 | + |
| 237 | + # Ensure that data is a writable copy to obtain void pointer, |
| 238 | + # from_buffer will throw error if the buffer passed in is not writable. |
| 239 | + if data.readonly: |
| 240 | + data = memoryview(bytearray(data)) |
| 241 | + |
| 242 | + # Obtain a c_void_p pointer to the data buffer |
| 243 | + ptr = c_void_p(addressof(c_char.from_buffer(data))) |
| 244 | + |
| 245 | + # Determine the size of the data |
| 246 | + size = len(data) |
| 247 | + |
231 | 248 | _raw.webui_send_raw_client( |
232 | 249 | byref(self._c_event()), |
233 | 250 | c_char_p(function.encode("utf-8")), |
234 | | - c_void_p(raw), |
| 251 | + ptr, |
235 | 252 | c_size_t(size) |
236 | 253 | ) |
237 | 254 |
|
@@ -1024,33 +1041,50 @@ def set_icon(self, icon: str, icon_type: str) -> None: |
1024 | 1041 | _raw.webui_set_icon(c_size_t(self._window), icon.encode("utf-8"), icon_type.encode("utf-8")) |
1025 | 1042 |
|
1026 | 1043 | # -- send_raw ----------------------------------- |
1027 | | - def send_raw(self, function: str, raw: Optional[c_void_p], size: int) -> None: |
1028 | | - """Safely send raw data to the UI for all clients. |
| 1044 | + def send_raw(self, function: str, data: Union[bytes, bytearray, memoryview, array.array]) -> None: |
| 1045 | + """ |
| 1046 | + Safely send raw data to the UI for all clients. |
1029 | 1047 |
|
1030 | 1048 | This function sends a raw data buffer to a JavaScript function in the UI. |
1031 | 1049 | The JavaScript function should be capable of handling raw binary data. |
1032 | 1050 |
|
1033 | 1051 | Args: |
1034 | 1052 | function (str): The JavaScript function that will receive the raw data. |
1035 | | - raw (Optional[c_void_p]): A pointer to the raw data buffer. Must not be `None`. |
1036 | | - size (int): The size of the raw data in bytes. |
| 1053 | + data (Union[bytes, bytearray, memoryview, array.array]): The raw data buffer. |
1037 | 1054 |
|
1038 | 1055 | Raises: |
1039 | | - ValueError: If `raw` is `None`. |
| 1056 | + ValueError: If `data` is `None` or empty. |
1040 | 1057 |
|
1041 | 1058 | Returns: |
1042 | 1059 | None |
1043 | 1060 |
|
1044 | 1061 | Example: |
1045 | | - my_window.send_raw("myJavaScriptFunc", my_buffer, 64) |
1046 | | - # Sends 64 bytes of raw data to the JavaScript function `myJavaScriptFunc`. |
| 1062 | + my_window.send_raw("myJavaScriptFunc", bytearray([0x01, 0x0A, 0xFF])) |
| 1063 | + # Sends 3 bytes of raw data to the JavaScript function `myJavaScriptFunc`. |
1047 | 1064 | """ |
1048 | | - if raw is None: |
1049 | | - raise ValueError("Invalid pointer: Cannot send a null pointer.") |
| 1065 | + if data is None or len(data) == 0: |
| 1066 | + raise ValueError("Data must not be None or empty.") |
| 1067 | + |
| 1068 | + # Ensure data is a memoryview for uniformity |
| 1069 | + if not isinstance(data, memoryview): |
| 1070 | + data = memoryview(data) |
| 1071 | + |
| 1072 | + # Ensure that data is a writable copy to obtain void pointer, |
| 1073 | + # from_buffer will throw error if the buffer passed in is not writable. |
| 1074 | + if data.readonly: |
| 1075 | + data = memoryview(bytearray(data)) |
| 1076 | + |
| 1077 | + # Obtain a c_void_p pointer to the data buffer |
| 1078 | + ptr = c_void_p(addressof(c_char.from_buffer(data))) |
| 1079 | + |
| 1080 | + # Determine the size of the data |
| 1081 | + size = len(data) |
| 1082 | + |
| 1083 | + # Call the underlying function to send the data |
1050 | 1084 | _raw.webui_send_raw( |
1051 | 1085 | c_size_t(self._window), |
1052 | 1086 | c_char_p(function.encode("utf-8")), |
1053 | | - c_void_p(raw), # type: ignore |
| 1087 | + ptr, |
1054 | 1088 | c_size_t(size) |
1055 | 1089 | ) |
1056 | 1090 |
|
|
0 commit comments