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