sideview 0.4.0__py3-none-macosx_12_0_universal2.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.
- sideview/__init__.py +10 -0
- sideview/_backend.py +522 -0
- sideview/libsideview_native.dylib +0 -0
- sideview/py.typed +1 -0
- sideview/widget.py +681 -0
- sideview-0.4.0.dist-info/METADATA +359 -0
- sideview-0.4.0.dist-info/RECORD +9 -0
- sideview-0.4.0.dist-info/WHEEL +5 -0
- sideview-0.4.0.dist-info/licenses/LICENSE +21 -0
sideview/__init__.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
2
|
+
|
|
3
|
+
from .widget import NativeWebView, NativeWebViewError
|
|
4
|
+
|
|
5
|
+
try:
|
|
6
|
+
__version__ = version("sideview")
|
|
7
|
+
except PackageNotFoundError:
|
|
8
|
+
__version__ = "0+unknown"
|
|
9
|
+
|
|
10
|
+
__all__ = ["NativeWebView", "NativeWebViewError", "__version__"]
|
sideview/_backend.py
ADDED
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import ctypes
|
|
4
|
+
import os
|
|
5
|
+
import platform
|
|
6
|
+
from collections.abc import Callable
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from importlib.metadata import PackageNotFoundError, distribution
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class NativeWebViewError(RuntimeError):
|
|
13
|
+
"""Raised when the native webview backend cannot be loaded or used."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
EventCallback = Callable[[int, str], None]
|
|
17
|
+
PolicyCallback = Callable[[int, str], bool]
|
|
18
|
+
CaptureCallback = Callable[[int, bool, bytes, str], None]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass(slots=True)
|
|
22
|
+
class NativeOptions:
|
|
23
|
+
user_data_folder: str | None = None
|
|
24
|
+
runtime_path: str | None = None
|
|
25
|
+
session_id: str | None = None
|
|
26
|
+
transparent: bool = False
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class _NativeOptionsW(ctypes.Structure):
|
|
30
|
+
_fields_ = [
|
|
31
|
+
("user_data_folder", ctypes.c_void_p),
|
|
32
|
+
("runtime_path", ctypes.c_void_p),
|
|
33
|
+
("session_id", ctypes.c_void_p),
|
|
34
|
+
("transparent", ctypes.c_int),
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class _NativeOptionsUtf8(ctypes.Structure):
|
|
39
|
+
_fields_ = [
|
|
40
|
+
("user_data_folder", ctypes.c_void_p),
|
|
41
|
+
("runtime_path", ctypes.c_void_p),
|
|
42
|
+
("session_id", ctypes.c_void_p),
|
|
43
|
+
("transparent", ctypes.c_int),
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class NativeCookie:
|
|
48
|
+
def __init__(
|
|
49
|
+
self,
|
|
50
|
+
*,
|
|
51
|
+
name: str,
|
|
52
|
+
value: str,
|
|
53
|
+
domain: str,
|
|
54
|
+
path: str = "/",
|
|
55
|
+
expires: float = 0,
|
|
56
|
+
secure: bool = False,
|
|
57
|
+
http_only: bool = False,
|
|
58
|
+
same_site: str = "lax",
|
|
59
|
+
) -> None:
|
|
60
|
+
self.name = name
|
|
61
|
+
self.value = value
|
|
62
|
+
self.domain = domain
|
|
63
|
+
self.path = path
|
|
64
|
+
self.expires = expires
|
|
65
|
+
self.secure = secure
|
|
66
|
+
self.http_only = http_only
|
|
67
|
+
self.same_site = same_site
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class _NativeCookieW(ctypes.Structure):
|
|
71
|
+
_fields_ = [
|
|
72
|
+
("name", ctypes.c_void_p),
|
|
73
|
+
("value", ctypes.c_void_p),
|
|
74
|
+
("domain", ctypes.c_void_p),
|
|
75
|
+
("path", ctypes.c_void_p),
|
|
76
|
+
("expires", ctypes.c_double),
|
|
77
|
+
("secure", ctypes.c_int),
|
|
78
|
+
("http_only", ctypes.c_int),
|
|
79
|
+
("same_site", ctypes.c_int),
|
|
80
|
+
]
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class _NativeCookieUtf8(ctypes.Structure):
|
|
84
|
+
_fields_ = _NativeCookieW._fields_
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class NativeBackend:
|
|
88
|
+
EVENT_READY = 1
|
|
89
|
+
EVENT_NAVIGATION_STARTED = 2
|
|
90
|
+
EVENT_NAVIGATION_FINISHED = 3
|
|
91
|
+
EVENT_NAVIGATION_FAILED = 4
|
|
92
|
+
EVENT_TITLE_CHANGED = 5
|
|
93
|
+
EVENT_DOWNLOAD_REQUESTED = 6
|
|
94
|
+
EVENT_NEW_WINDOW_REQUESTED = 7
|
|
95
|
+
EVENT_SCRIPT_MESSAGE = 8
|
|
96
|
+
EVENT_ZOOM_FACTOR_CHANGED = 9
|
|
97
|
+
EVENT_ZOOM_FACTOR_REQUESTED = 10
|
|
98
|
+
|
|
99
|
+
def __init__(self) -> None:
|
|
100
|
+
self._system = platform.system()
|
|
101
|
+
library_path = self._resolve_library()
|
|
102
|
+
try:
|
|
103
|
+
self._lib = ctypes.CDLL(str(library_path))
|
|
104
|
+
except OSError as exc:
|
|
105
|
+
hint = ""
|
|
106
|
+
if self._system == "Linux":
|
|
107
|
+
hint = (
|
|
108
|
+
" Install the WebKitGTK 4.1 and GTK 3 runtime libraries for your distribution."
|
|
109
|
+
)
|
|
110
|
+
raise NativeWebViewError(
|
|
111
|
+
f"Failed to load native webview library {library_path}: {exc}.{hint}"
|
|
112
|
+
) from exc
|
|
113
|
+
self._callback_type = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p)
|
|
114
|
+
self._policy_callback_type = ctypes.CFUNCTYPE(
|
|
115
|
+
ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p
|
|
116
|
+
)
|
|
117
|
+
self._capture_callback_type = ctypes.CFUNCTYPE(
|
|
118
|
+
None,
|
|
119
|
+
ctypes.c_void_p,
|
|
120
|
+
ctypes.c_int,
|
|
121
|
+
ctypes.c_int,
|
|
122
|
+
ctypes.POINTER(ctypes.c_uint8),
|
|
123
|
+
ctypes.c_size_t,
|
|
124
|
+
ctypes.c_void_p,
|
|
125
|
+
)
|
|
126
|
+
self._callbacks: dict[int, ctypes._CFuncPtr] = {}
|
|
127
|
+
self._policy_callbacks: dict[int, ctypes._CFuncPtr] = {}
|
|
128
|
+
self._capture_callbacks: dict[int, ctypes._CFuncPtr] = {}
|
|
129
|
+
self._configure_signatures()
|
|
130
|
+
|
|
131
|
+
@property
|
|
132
|
+
def uses_foreign_window(self) -> bool:
|
|
133
|
+
return self._system == "Linux"
|
|
134
|
+
|
|
135
|
+
def create(self, parent_handle: int, options: NativeOptions, callback: EventCallback) -> int:
|
|
136
|
+
native_options, keepalive = self._build_options(options)
|
|
137
|
+
handle = self._lib.nwv_create(ctypes.c_void_p(parent_handle), ctypes.byref(native_options))
|
|
138
|
+
if not handle:
|
|
139
|
+
raise NativeWebViewError("Native webview creation failed.")
|
|
140
|
+
|
|
141
|
+
def trampoline(_user_data: int, event_type: int, message_ptr: int) -> None:
|
|
142
|
+
callback(event_type, self._decode_message(message_ptr))
|
|
143
|
+
|
|
144
|
+
c_callback = self._callback_type(trampoline)
|
|
145
|
+
self._callbacks[int(handle)] = c_callback
|
|
146
|
+
self._lib.nwv_set_event_callback(handle, c_callback, None)
|
|
147
|
+
self._keepalive = keepalive
|
|
148
|
+
return int(handle)
|
|
149
|
+
|
|
150
|
+
def set_policy_callback(self, handle: int, callback: PolicyCallback | None) -> None:
|
|
151
|
+
if not handle:
|
|
152
|
+
return
|
|
153
|
+
|
|
154
|
+
if callback is None:
|
|
155
|
+
self._policy_callbacks.pop(int(handle), None)
|
|
156
|
+
self._lib.nwv_set_policy_callback(
|
|
157
|
+
ctypes.c_void_p(handle), self._policy_callback_type(), None
|
|
158
|
+
)
|
|
159
|
+
return
|
|
160
|
+
|
|
161
|
+
def trampoline(_user_data: int, event_type: int, message_ptr: int) -> int:
|
|
162
|
+
return 1 if callback(event_type, self._decode_message(message_ptr)) else 0
|
|
163
|
+
|
|
164
|
+
c_callback = self._policy_callback_type(trampoline)
|
|
165
|
+
self._policy_callbacks[int(handle)] = c_callback
|
|
166
|
+
self._lib.nwv_set_policy_callback(ctypes.c_void_p(handle), c_callback, None)
|
|
167
|
+
|
|
168
|
+
def set_capture_callback(self, handle: int, callback: CaptureCallback | None) -> None:
|
|
169
|
+
if not handle:
|
|
170
|
+
return
|
|
171
|
+
|
|
172
|
+
if callback is None:
|
|
173
|
+
self._capture_callbacks.pop(int(handle), None)
|
|
174
|
+
self._lib.nwv_set_capture_callback(
|
|
175
|
+
ctypes.c_void_p(handle), self._capture_callback_type(), None
|
|
176
|
+
)
|
|
177
|
+
return
|
|
178
|
+
|
|
179
|
+
def trampoline(
|
|
180
|
+
_user_data: int,
|
|
181
|
+
request_id: int,
|
|
182
|
+
success: int,
|
|
183
|
+
data_ptr: ctypes.POINTER(ctypes.c_uint8),
|
|
184
|
+
size: int,
|
|
185
|
+
error_ptr: int,
|
|
186
|
+
) -> None:
|
|
187
|
+
data = ctypes.string_at(data_ptr, size) if data_ptr and size else b""
|
|
188
|
+
callback(request_id, bool(success), data, self._decode_message(error_ptr))
|
|
189
|
+
|
|
190
|
+
c_callback = self._capture_callback_type(trampoline)
|
|
191
|
+
self._capture_callbacks[int(handle)] = c_callback
|
|
192
|
+
self._lib.nwv_set_capture_callback(ctypes.c_void_p(handle), c_callback, None)
|
|
193
|
+
|
|
194
|
+
def destroy(self, handle: int) -> None:
|
|
195
|
+
if not handle:
|
|
196
|
+
return
|
|
197
|
+
self._callbacks.pop(int(handle), None)
|
|
198
|
+
self._policy_callbacks.pop(int(handle), None)
|
|
199
|
+
self._capture_callbacks.pop(int(handle), None)
|
|
200
|
+
self._lib.nwv_destroy(ctypes.c_void_p(handle))
|
|
201
|
+
|
|
202
|
+
def native_view(self, handle: int) -> int:
|
|
203
|
+
if not self.uses_foreign_window or not handle:
|
|
204
|
+
return 0
|
|
205
|
+
return int(self._lib.nwv_get_native_view(ctypes.c_void_p(handle)))
|
|
206
|
+
|
|
207
|
+
def resize(self, handle: int, width: int, height: int) -> None:
|
|
208
|
+
if handle:
|
|
209
|
+
self._lib.nwv_resize(ctypes.c_void_p(handle), int(width), int(height))
|
|
210
|
+
|
|
211
|
+
def navigate(self, handle: int, url: str) -> bool:
|
|
212
|
+
return self._call_text("nwv_navigate", handle, url)
|
|
213
|
+
|
|
214
|
+
def set_html(self, handle: int, html: str, base_url: str | None = None) -> bool:
|
|
215
|
+
if self._system == "Windows":
|
|
216
|
+
html_value = ctypes.c_wchar_p(html)
|
|
217
|
+
base_value = ctypes.c_wchar_p(base_url) if base_url else None
|
|
218
|
+
else:
|
|
219
|
+
html_value = ctypes.c_char_p(html.encode("utf-8"))
|
|
220
|
+
base_value = ctypes.c_char_p(base_url.encode("utf-8")) if base_url else None
|
|
221
|
+
result = self._lib.nwv_set_html(
|
|
222
|
+
ctypes.c_void_p(handle),
|
|
223
|
+
ctypes.cast(html_value, ctypes.c_void_p),
|
|
224
|
+
ctypes.cast(base_value, ctypes.c_void_p) if base_value else None,
|
|
225
|
+
)
|
|
226
|
+
return bool(result)
|
|
227
|
+
|
|
228
|
+
def reload(self, handle: int) -> bool:
|
|
229
|
+
return bool(self._lib.nwv_reload(ctypes.c_void_p(handle)))
|
|
230
|
+
|
|
231
|
+
def go_back(self, handle: int) -> bool:
|
|
232
|
+
return bool(self._lib.nwv_go_back(ctypes.c_void_p(handle)))
|
|
233
|
+
|
|
234
|
+
def go_forward(self, handle: int) -> bool:
|
|
235
|
+
return bool(self._lib.nwv_go_forward(ctypes.c_void_p(handle)))
|
|
236
|
+
|
|
237
|
+
def eval_js(self, handle: int, script: str) -> bool:
|
|
238
|
+
return self._call_text("nwv_eval_js", handle, script)
|
|
239
|
+
|
|
240
|
+
def add_document_script(self, handle: int, script: str) -> bool:
|
|
241
|
+
return self._call_text("nwv_add_document_script", handle, script)
|
|
242
|
+
|
|
243
|
+
def set_default_context_menu_enabled(self, handle: int, enabled: bool) -> bool:
|
|
244
|
+
result = self._lib.nwv_set_default_context_menu_enabled(
|
|
245
|
+
ctypes.c_void_p(handle), int(enabled)
|
|
246
|
+
)
|
|
247
|
+
return bool(result)
|
|
248
|
+
|
|
249
|
+
def set_devtools_enabled(self, handle: int, enabled: bool) -> bool:
|
|
250
|
+
result = self._lib.nwv_set_devtools_enabled(ctypes.c_void_p(handle), int(enabled))
|
|
251
|
+
return bool(result)
|
|
252
|
+
|
|
253
|
+
def capture_png(
|
|
254
|
+
self, handle: int, request_id: int, x: int, y: int, width: int, height: int
|
|
255
|
+
) -> bool:
|
|
256
|
+
result = self._lib.nwv_capture_png(
|
|
257
|
+
ctypes.c_void_p(handle),
|
|
258
|
+
int(request_id),
|
|
259
|
+
int(x),
|
|
260
|
+
int(y),
|
|
261
|
+
int(width),
|
|
262
|
+
int(height),
|
|
263
|
+
)
|
|
264
|
+
return bool(result)
|
|
265
|
+
|
|
266
|
+
def capture_jpeg(self, handle: int, request_id: int) -> bool:
|
|
267
|
+
result = self._lib.nwv_capture_jpeg(ctypes.c_void_p(handle), int(request_id))
|
|
268
|
+
return bool(result)
|
|
269
|
+
|
|
270
|
+
def start_frame_stream(
|
|
271
|
+
self,
|
|
272
|
+
handle: int,
|
|
273
|
+
quality: int,
|
|
274
|
+
max_width: int,
|
|
275
|
+
max_height: int,
|
|
276
|
+
every_nth_frame: int,
|
|
277
|
+
) -> bool:
|
|
278
|
+
result = self._lib.nwv_start_frame_stream(
|
|
279
|
+
ctypes.c_void_p(handle),
|
|
280
|
+
int(quality),
|
|
281
|
+
int(max_width),
|
|
282
|
+
int(max_height),
|
|
283
|
+
int(every_nth_frame),
|
|
284
|
+
)
|
|
285
|
+
return bool(result)
|
|
286
|
+
|
|
287
|
+
def stop_frame_stream(self, handle: int) -> bool:
|
|
288
|
+
result = self._lib.nwv_stop_frame_stream(ctypes.c_void_p(handle))
|
|
289
|
+
return bool(result)
|
|
290
|
+
|
|
291
|
+
def set_cookie(self, handle: int, cookie: NativeCookie) -> bool:
|
|
292
|
+
native_cookie, _keepalive = self._build_cookie(cookie)
|
|
293
|
+
result = self._lib.nwv_set_cookie(ctypes.c_void_p(handle), ctypes.byref(native_cookie))
|
|
294
|
+
return bool(result)
|
|
295
|
+
|
|
296
|
+
def clear_cookies(self, handle: int) -> bool:
|
|
297
|
+
return bool(self._lib.nwv_clear_cookies(ctypes.c_void_p(handle)))
|
|
298
|
+
|
|
299
|
+
def can_go_back(self, handle: int) -> bool:
|
|
300
|
+
return bool(self._lib.nwv_can_go_back(ctypes.c_void_p(handle)))
|
|
301
|
+
|
|
302
|
+
def can_go_forward(self, handle: int) -> bool:
|
|
303
|
+
return bool(self._lib.nwv_can_go_forward(ctypes.c_void_p(handle)))
|
|
304
|
+
|
|
305
|
+
def set_zoom_factor(self, handle: int, factor: float) -> bool:
|
|
306
|
+
return bool(
|
|
307
|
+
self._lib.nwv_set_zoom_factor(
|
|
308
|
+
ctypes.c_void_p(handle),
|
|
309
|
+
ctypes.c_double(factor),
|
|
310
|
+
)
|
|
311
|
+
)
|
|
312
|
+
|
|
313
|
+
def get_zoom_factor(self, handle: int) -> float:
|
|
314
|
+
return float(self._lib.nwv_get_zoom_factor(ctypes.c_void_p(handle)))
|
|
315
|
+
|
|
316
|
+
def _configure_signatures(self) -> None:
|
|
317
|
+
self._lib.nwv_create.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
|
|
318
|
+
self._lib.nwv_create.restype = ctypes.c_void_p
|
|
319
|
+
if self.uses_foreign_window:
|
|
320
|
+
self._lib.nwv_get_native_view.argtypes = [ctypes.c_void_p]
|
|
321
|
+
self._lib.nwv_get_native_view.restype = ctypes.c_size_t
|
|
322
|
+
self._lib.nwv_destroy.argtypes = [ctypes.c_void_p]
|
|
323
|
+
self._lib.nwv_set_event_callback.argtypes = [
|
|
324
|
+
ctypes.c_void_p,
|
|
325
|
+
self._callback_type,
|
|
326
|
+
ctypes.c_void_p,
|
|
327
|
+
]
|
|
328
|
+
self._lib.nwv_set_policy_callback.argtypes = [
|
|
329
|
+
ctypes.c_void_p,
|
|
330
|
+
self._policy_callback_type,
|
|
331
|
+
ctypes.c_void_p,
|
|
332
|
+
]
|
|
333
|
+
self._lib.nwv_set_capture_callback.argtypes = [
|
|
334
|
+
ctypes.c_void_p,
|
|
335
|
+
self._capture_callback_type,
|
|
336
|
+
ctypes.c_void_p,
|
|
337
|
+
]
|
|
338
|
+
self._lib.nwv_resize.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
|
|
339
|
+
self._lib.nwv_navigate.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
|
|
340
|
+
self._lib.nwv_set_html.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p]
|
|
341
|
+
self._lib.nwv_eval_js.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
|
|
342
|
+
self._lib.nwv_add_document_script.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
|
|
343
|
+
self._lib.nwv_set_default_context_menu_enabled.argtypes = [ctypes.c_void_p, ctypes.c_int]
|
|
344
|
+
self._lib.nwv_set_devtools_enabled.argtypes = [ctypes.c_void_p, ctypes.c_int]
|
|
345
|
+
self._lib.nwv_capture_png.argtypes = [
|
|
346
|
+
ctypes.c_void_p,
|
|
347
|
+
ctypes.c_int,
|
|
348
|
+
ctypes.c_int,
|
|
349
|
+
ctypes.c_int,
|
|
350
|
+
ctypes.c_int,
|
|
351
|
+
ctypes.c_int,
|
|
352
|
+
]
|
|
353
|
+
self._lib.nwv_capture_jpeg.argtypes = [ctypes.c_void_p, ctypes.c_int]
|
|
354
|
+
self._lib.nwv_start_frame_stream.argtypes = [
|
|
355
|
+
ctypes.c_void_p,
|
|
356
|
+
ctypes.c_int,
|
|
357
|
+
ctypes.c_int,
|
|
358
|
+
ctypes.c_int,
|
|
359
|
+
ctypes.c_int,
|
|
360
|
+
]
|
|
361
|
+
self._lib.nwv_stop_frame_stream.argtypes = [ctypes.c_void_p]
|
|
362
|
+
self._lib.nwv_set_cookie.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
|
|
363
|
+
self._lib.nwv_set_zoom_factor.argtypes = [ctypes.c_void_p, ctypes.c_double]
|
|
364
|
+
self._lib.nwv_get_zoom_factor.argtypes = [ctypes.c_void_p]
|
|
365
|
+
self._lib.nwv_get_zoom_factor.restype = ctypes.c_double
|
|
366
|
+
|
|
367
|
+
for name in (
|
|
368
|
+
"nwv_navigate",
|
|
369
|
+
"nwv_set_html",
|
|
370
|
+
"nwv_reload",
|
|
371
|
+
"nwv_go_back",
|
|
372
|
+
"nwv_go_forward",
|
|
373
|
+
"nwv_eval_js",
|
|
374
|
+
"nwv_add_document_script",
|
|
375
|
+
"nwv_set_default_context_menu_enabled",
|
|
376
|
+
"nwv_set_devtools_enabled",
|
|
377
|
+
"nwv_capture_png",
|
|
378
|
+
"nwv_capture_jpeg",
|
|
379
|
+
"nwv_start_frame_stream",
|
|
380
|
+
"nwv_stop_frame_stream",
|
|
381
|
+
"nwv_set_cookie",
|
|
382
|
+
"nwv_clear_cookies",
|
|
383
|
+
"nwv_can_go_back",
|
|
384
|
+
"nwv_can_go_forward",
|
|
385
|
+
"nwv_set_zoom_factor",
|
|
386
|
+
):
|
|
387
|
+
getattr(self._lib, name).restype = ctypes.c_int
|
|
388
|
+
|
|
389
|
+
def _resolve_library(self) -> Path:
|
|
390
|
+
configured = os.environ.get("SIDEVIEW_NATIVE_LIB")
|
|
391
|
+
if configured:
|
|
392
|
+
path = Path(configured)
|
|
393
|
+
if path.exists():
|
|
394
|
+
return path
|
|
395
|
+
raise NativeWebViewError(f"SIDEVIEW_NATIVE_LIB points to a missing file: {path}")
|
|
396
|
+
|
|
397
|
+
package_dirs = [Path(__file__).resolve().parent]
|
|
398
|
+
try:
|
|
399
|
+
installed_package_dir = Path(distribution("sideview").locate_file("sideview"))
|
|
400
|
+
except PackageNotFoundError:
|
|
401
|
+
pass
|
|
402
|
+
else:
|
|
403
|
+
if installed_package_dir not in package_dirs:
|
|
404
|
+
package_dirs.append(installed_package_dir)
|
|
405
|
+
|
|
406
|
+
if self._system == "Windows":
|
|
407
|
+
library_names = ["sideview_native.dll"]
|
|
408
|
+
elif self._system == "Darwin":
|
|
409
|
+
library_names = ["libsideview_native.dylib", "sideview_native.dylib"]
|
|
410
|
+
elif self._system == "Linux":
|
|
411
|
+
library_names = ["libsideview_native.so", "sideview_native.so"]
|
|
412
|
+
else:
|
|
413
|
+
raise NativeWebViewError("SideView supports Windows, macOS, and Linux only.")
|
|
414
|
+
|
|
415
|
+
candidates = [
|
|
416
|
+
package_dir / library_name
|
|
417
|
+
for package_dir in package_dirs
|
|
418
|
+
for library_name in library_names
|
|
419
|
+
]
|
|
420
|
+
for candidate in candidates:
|
|
421
|
+
if candidate.exists():
|
|
422
|
+
return candidate
|
|
423
|
+
|
|
424
|
+
names = ", ".join(str(candidate) for candidate in candidates)
|
|
425
|
+
raise NativeWebViewError(
|
|
426
|
+
"Native webview library was not found. Expected one of "
|
|
427
|
+
f"{names}, or set SIDEVIEW_NATIVE_LIB."
|
|
428
|
+
)
|
|
429
|
+
|
|
430
|
+
def _build_options(self, options: NativeOptions) -> tuple[ctypes.Structure, list[object]]:
|
|
431
|
+
keepalive: list[object] = []
|
|
432
|
+
if self._system == "Windows":
|
|
433
|
+
user_data = (
|
|
434
|
+
ctypes.c_wchar_p(options.user_data_folder) if options.user_data_folder else None
|
|
435
|
+
)
|
|
436
|
+
runtime_path = ctypes.c_wchar_p(options.runtime_path) if options.runtime_path else None
|
|
437
|
+
session_id = ctypes.c_wchar_p(options.session_id) if options.session_id else None
|
|
438
|
+
keepalive.extend(
|
|
439
|
+
value for value in (user_data, runtime_path, session_id) if value is not None
|
|
440
|
+
)
|
|
441
|
+
native = _NativeOptionsW(
|
|
442
|
+
ctypes.cast(user_data, ctypes.c_void_p).value if user_data else None,
|
|
443
|
+
ctypes.cast(runtime_path, ctypes.c_void_p).value if runtime_path else None,
|
|
444
|
+
ctypes.cast(session_id, ctypes.c_void_p).value if session_id else None,
|
|
445
|
+
int(options.transparent),
|
|
446
|
+
)
|
|
447
|
+
else:
|
|
448
|
+
user_data = (
|
|
449
|
+
ctypes.c_char_p(options.user_data_folder.encode("utf-8"))
|
|
450
|
+
if options.user_data_folder
|
|
451
|
+
else None
|
|
452
|
+
)
|
|
453
|
+
runtime_path = (
|
|
454
|
+
ctypes.c_char_p(options.runtime_path.encode("utf-8"))
|
|
455
|
+
if options.runtime_path
|
|
456
|
+
else None
|
|
457
|
+
)
|
|
458
|
+
session_id = (
|
|
459
|
+
ctypes.c_char_p(options.session_id.encode("utf-8")) if options.session_id else None
|
|
460
|
+
)
|
|
461
|
+
keepalive.extend(
|
|
462
|
+
value for value in (user_data, runtime_path, session_id) if value is not None
|
|
463
|
+
)
|
|
464
|
+
native = _NativeOptionsUtf8(
|
|
465
|
+
ctypes.cast(user_data, ctypes.c_void_p).value if user_data else None,
|
|
466
|
+
ctypes.cast(runtime_path, ctypes.c_void_p).value if runtime_path else None,
|
|
467
|
+
ctypes.cast(session_id, ctypes.c_void_p).value if session_id else None,
|
|
468
|
+
int(options.transparent),
|
|
469
|
+
)
|
|
470
|
+
return native, keepalive
|
|
471
|
+
|
|
472
|
+
def _build_cookie(self, cookie: NativeCookie) -> tuple[ctypes.Structure, list[object]]:
|
|
473
|
+
same_site_map = {"none": 0, "lax": 1, "strict": 2}
|
|
474
|
+
same_site = same_site_map.get(cookie.same_site.lower(), 1)
|
|
475
|
+
keepalive: list[object] = []
|
|
476
|
+
|
|
477
|
+
if self._system == "Windows":
|
|
478
|
+
values = [
|
|
479
|
+
ctypes.c_wchar_p(cookie.name),
|
|
480
|
+
ctypes.c_wchar_p(cookie.value),
|
|
481
|
+
ctypes.c_wchar_p(cookie.domain),
|
|
482
|
+
ctypes.c_wchar_p(cookie.path or "/"),
|
|
483
|
+
]
|
|
484
|
+
keepalive.extend(values)
|
|
485
|
+
args = [ctypes.cast(value, ctypes.c_void_p).value for value in values]
|
|
486
|
+
args.extend(
|
|
487
|
+
[float(cookie.expires or 0), int(cookie.secure), int(cookie.http_only), same_site]
|
|
488
|
+
)
|
|
489
|
+
native = _NativeCookieW(*args)
|
|
490
|
+
else:
|
|
491
|
+
values = [
|
|
492
|
+
ctypes.c_char_p(cookie.name.encode("utf-8")),
|
|
493
|
+
ctypes.c_char_p(cookie.value.encode("utf-8")),
|
|
494
|
+
ctypes.c_char_p(cookie.domain.encode("utf-8")),
|
|
495
|
+
ctypes.c_char_p((cookie.path or "/").encode("utf-8")),
|
|
496
|
+
]
|
|
497
|
+
keepalive.extend(values)
|
|
498
|
+
args = [ctypes.cast(value, ctypes.c_void_p).value for value in values]
|
|
499
|
+
args.extend(
|
|
500
|
+
[float(cookie.expires or 0), int(cookie.secure), int(cookie.http_only), same_site]
|
|
501
|
+
)
|
|
502
|
+
native = _NativeCookieUtf8(*args)
|
|
503
|
+
|
|
504
|
+
return native, keepalive
|
|
505
|
+
|
|
506
|
+
def _decode_message(self, message_ptr: int) -> str:
|
|
507
|
+
if not message_ptr:
|
|
508
|
+
return ""
|
|
509
|
+
if self._system == "Windows":
|
|
510
|
+
return ctypes.wstring_at(message_ptr)
|
|
511
|
+
return ctypes.string_at(message_ptr).decode("utf-8", errors="replace")
|
|
512
|
+
|
|
513
|
+
def _call_text(self, function_name: str, handle: int, text: str) -> bool:
|
|
514
|
+
if self._system == "Windows":
|
|
515
|
+
value = ctypes.c_wchar_p(text)
|
|
516
|
+
else:
|
|
517
|
+
value = ctypes.c_char_p(text.encode("utf-8"))
|
|
518
|
+
result = getattr(self._lib, function_name)(
|
|
519
|
+
ctypes.c_void_p(handle),
|
|
520
|
+
ctypes.cast(value, ctypes.c_void_p),
|
|
521
|
+
)
|
|
522
|
+
return bool(result)
|
|
Binary file
|
sideview/py.typed
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|