pure-patchright 1.62.3__py3-none-any.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.
Files changed (142) hide show
  1. patchright/__init__.py +5 -0
  2. patchright/__main__.py +18 -0
  3. patchright/_impl/__init__.py +0 -0
  4. patchright/_impl/__pyinstaller/__init__.py +20 -0
  5. patchright/_impl/__pyinstaller/hook-playwright.async_api.py +3 -0
  6. patchright/_impl/__pyinstaller/hook-playwright.sync_api.py +3 -0
  7. patchright/_impl/_api_structures.py +343 -0
  8. patchright/_impl/_artifact.py +46 -0
  9. patchright/_impl/_assertions.py +1014 -0
  10. patchright/_impl/_async_base.py +88 -0
  11. patchright/_impl/_browser.py +267 -0
  12. patchright/_impl/_browser_context.py +759 -0
  13. patchright/_impl/_browser_type.py +336 -0
  14. patchright/_impl/_cdp_session.py +25 -0
  15. patchright/_impl/_clock.py +62 -0
  16. patchright/_impl/_connection.py +686 -0
  17. patchright/_impl/_console_message.py +83 -0
  18. patchright/_impl/_credentials.py +38 -0
  19. patchright/_impl/_debugger.py +37 -0
  20. patchright/_impl/_dialog.py +45 -0
  21. patchright/_impl/_disposable.py +71 -0
  22. patchright/_impl/_download.py +49 -0
  23. patchright/_impl/_driver.py +24 -0
  24. patchright/_impl/_element_handle.py +446 -0
  25. patchright/_impl/_errors.py +46 -0
  26. patchright/_impl/_event_context_manager.py +19 -0
  27. patchright/_impl/_fetch.py +592 -0
  28. patchright/_impl/_file_chooser.py +42 -0
  29. patchright/_impl/_form_data.py +19 -0
  30. patchright/_impl/_frame.py +959 -0
  31. patchright/_impl/_glob.py +47 -0
  32. patchright/_impl/_greenlets.py +35 -0
  33. patchright/_impl/_har_router.py +98 -0
  34. patchright/_impl/_helper.py +551 -0
  35. patchright/_impl/_impl_to_api_mapping.py +141 -0
  36. patchright/_impl/_input.py +85 -0
  37. patchright/_impl/_js_handle.py +297 -0
  38. patchright/_impl/_json_pipe.py +55 -0
  39. patchright/_impl/_local_utils.py +73 -0
  40. patchright/_impl/_locator.py +894 -0
  41. patchright/_impl/_map.py +18 -0
  42. patchright/_impl/_network.py +953 -0
  43. patchright/_impl/_object_factory.py +93 -0
  44. patchright/_impl/_page.py +1601 -0
  45. patchright/_impl/_path_utils.py +13 -0
  46. patchright/_impl/_playwright.py +43 -0
  47. patchright/_impl/_screencast.py +149 -0
  48. patchright/_impl/_selectors.py +48 -0
  49. patchright/_impl/_set_input_files_helpers.py +134 -0
  50. patchright/_impl/_str_utils.py +59 -0
  51. patchright/_impl/_stream.py +31 -0
  52. patchright/_impl/_sync_base.py +130 -0
  53. patchright/_impl/_tracing.py +243 -0
  54. patchright/_impl/_transport.py +151 -0
  55. patchright/_impl/_video.py +41 -0
  56. patchright/_impl/_waiter.py +168 -0
  57. patchright/_impl/_web_error.py +33 -0
  58. patchright/_impl/_web_storage.py +41 -0
  59. patchright/_impl/_writable_stream.py +26 -0
  60. patchright/_repo_version.py +1 -0
  61. patchright/async_api/__init__.py +234 -0
  62. patchright/async_api/_context_manager.py +38 -0
  63. patchright/async_api/_generated.py +23396 -0
  64. patchright/driver/README.md +1 -0
  65. patchright/driver/package/README.md +3 -0
  66. patchright/driver/package/ThirdPartyNotices.txt +13 -0
  67. patchright/driver/package/bin/install_media_pack.ps1 +5 -0
  68. patchright/driver/package/bin/install_webkit_wsl.ps1 +33 -0
  69. patchright/driver/package/bin/reinstall_chrome_beta_linux.sh +42 -0
  70. patchright/driver/package/bin/reinstall_chrome_beta_mac.sh +13 -0
  71. patchright/driver/package/bin/reinstall_chrome_beta_win.ps1 +24 -0
  72. patchright/driver/package/bin/reinstall_chrome_stable_linux.sh +42 -0
  73. patchright/driver/package/bin/reinstall_chrome_stable_mac.sh +12 -0
  74. patchright/driver/package/bin/reinstall_chrome_stable_win.ps1 +24 -0
  75. patchright/driver/package/bin/reinstall_msedge_beta_linux.sh +48 -0
  76. patchright/driver/package/bin/reinstall_msedge_beta_mac.sh +11 -0
  77. patchright/driver/package/bin/reinstall_msedge_beta_win.ps1 +23 -0
  78. patchright/driver/package/bin/reinstall_msedge_dev_linux.sh +48 -0
  79. patchright/driver/package/bin/reinstall_msedge_dev_mac.sh +11 -0
  80. patchright/driver/package/bin/reinstall_msedge_dev_win.ps1 +23 -0
  81. patchright/driver/package/bin/reinstall_msedge_stable_linux.sh +48 -0
  82. patchright/driver/package/bin/reinstall_msedge_stable_mac.sh +11 -0
  83. patchright/driver/package/bin/reinstall_msedge_stable_win.ps1 +24 -0
  84. patchright/driver/package/browsers.json +75 -0
  85. patchright/driver/package/cli.js +21 -0
  86. patchright/driver/package/index.js +17 -0
  87. patchright/driver/package/index.mjs +28 -0
  88. patchright/driver/package/lib/bootstrap.js +88 -0
  89. patchright/driver/package/lib/coreBundle.js +77498 -0
  90. patchright/driver/package/lib/entry/cliDaemon.js +5 -0
  91. patchright/driver/package/lib/entry/dashboardApp.js +3 -0
  92. patchright/driver/package/lib/entry/mcp.js +10 -0
  93. patchright/driver/package/lib/entry/oopBrowserDownload.js +3 -0
  94. patchright/driver/package/lib/package.js +50 -0
  95. patchright/driver/package/lib/server/chromium/appIcon.png +0 -0
  96. patchright/driver/package/lib/server/electron/loader.js +107 -0
  97. patchright/driver/package/lib/serverRegistry.js +7347 -0
  98. patchright/driver/package/lib/serverRegistry.js.LICENSE +354 -0
  99. patchright/driver/package/lib/tools/cli-client/channelSessions.js +141 -0
  100. patchright/driver/package/lib/tools/cli-client/cli.js +6 -0
  101. patchright/driver/package/lib/tools/cli-client/help.json +708 -0
  102. patchright/driver/package/lib/tools/cli-client/minimist.js +128 -0
  103. patchright/driver/package/lib/tools/cli-client/output.js +343 -0
  104. patchright/driver/package/lib/tools/cli-client/program.js +404 -0
  105. patchright/driver/package/lib/tools/cli-client/registry.js +176 -0
  106. patchright/driver/package/lib/tools/cli-client/session.js +258 -0
  107. patchright/driver/package/lib/tools/dashboard/appIcon.png +0 -0
  108. patchright/driver/package/lib/tools/skills/playwright-cli/SKILL.md +420 -0
  109. patchright/driver/package/lib/tools/skills/playwright-cli/references/element-attributes.md +23 -0
  110. patchright/driver/package/lib/tools/skills/playwright-cli/references/playwright-tests.md +39 -0
  111. patchright/driver/package/lib/tools/skills/playwright-cli/references/request-mocking.md +87 -0
  112. patchright/driver/package/lib/tools/skills/playwright-cli/references/running-code.md +241 -0
  113. patchright/driver/package/lib/tools/skills/playwright-cli/references/session-management.md +225 -0
  114. patchright/driver/package/lib/tools/skills/playwright-cli/references/storage-state.md +275 -0
  115. patchright/driver/package/lib/tools/skills/playwright-cli/references/test-generation.md +433 -0
  116. patchright/driver/package/lib/tools/skills/playwright-cli/references/tracing.md +139 -0
  117. patchright/driver/package/lib/tools/skills/playwright-cli/references/video-recording.md +143 -0
  118. patchright/driver/package/lib/tools/skills/playwright-component-testing/SKILL.md +143 -0
  119. patchright/driver/package/lib/tools/skills/playwright-component-testing/references/gallery-spec.md +144 -0
  120. patchright/driver/package/lib/tools/skills/playwright-component-testing/references/migration.md +85 -0
  121. patchright/driver/package/lib/tools/skills/playwright-component-testing/references/react.md +67 -0
  122. patchright/driver/package/lib/tools/skills/playwright-component-testing/references/vue.md +75 -0
  123. patchright/driver/package/lib/tools/skills/playwright-trace/SKILL.md +171 -0
  124. patchright/driver/package/lib/tools/utils/extension.js +101 -0
  125. patchright/driver/package/lib/tools/utils/socketConnection.js +108 -0
  126. patchright/driver/package/lib/utilsBundle.js +90764 -0
  127. patchright/driver/package/lib/utilsBundle.js.LICENSE +2179 -0
  128. patchright/driver/package/lib/webp_codec.LICENSE +173 -0
  129. patchright/driver/package/lib/webp_codec.wasm +0 -0
  130. patchright/driver/package/lib/xdg-open +1267 -0
  131. patchright/driver/package/package.json +34 -0
  132. patchright/py.typed +0 -0
  133. patchright/sync_api/__init__.py +234 -0
  134. patchright/sync_api/_context_manager.py +73 -0
  135. patchright/sync_api/_generated.py +23590 -0
  136. pure_patchright-1.62.3.dist-info/LICENSE +201 -0
  137. pure_patchright-1.62.3.dist-info/METADATA +96 -0
  138. pure_patchright-1.62.3.dist-info/RECORD +142 -0
  139. pure_patchright-1.62.3.dist-info/WHEEL +4 -0
  140. pure_patchright-1.62.3.dist-info/entry_points.txt +5 -0
  141. pure_patchright-1.62.3.dist-info/licenses/LICENSE +202 -0
  142. pure_patchright-1.62.3.dist-info/top_level.txt +1 -0
@@ -0,0 +1,551 @@
1
+ import asyncio
2
+ import datetime
3
+ import math
4
+ import os
5
+ import re
6
+ import time
7
+ import traceback
8
+ from pathlib import Path
9
+ from types import TracebackType
10
+ from typing import (
11
+ TYPE_CHECKING,
12
+ Any,
13
+ Awaitable,
14
+ Callable,
15
+ Dict,
16
+ List,
17
+ Literal,
18
+ Optional,
19
+ Pattern,
20
+ Set,
21
+ Tuple,
22
+ TypedDict,
23
+ TypeVar,
24
+ Union,
25
+ cast,
26
+ )
27
+ from urllib.parse import ParseResult, urljoin, urlparse, urlunparse
28
+ from patchright._impl._api_structures import NameValue
29
+ from patchright._impl._errors import (
30
+ Error,
31
+ TargetClosedError,
32
+ TimeoutError,
33
+ is_target_closed_error,
34
+ rewrite_error,
35
+ )
36
+ from patchright._impl._glob import glob_to_regex_pattern
37
+ from patchright._impl._greenlets import RouteGreenlet
38
+ from patchright._impl._str_utils import escape_regex_flags
39
+
40
+ if TYPE_CHECKING:
41
+ from patchright._impl._api_structures import HeadersArray
42
+ from patchright._impl._network import Request, Response, Route, WebSocketRoute
43
+ URLMatch = Union[str, Pattern[str], Callable[[str], bool]]
44
+ URLMatchRequest = Union[
45
+ str, Pattern[str], Callable[["Request"], Union[bool, Awaitable[bool]]]
46
+ ]
47
+ URLMatchResponse = Union[
48
+ str, Pattern[str], Callable[["Response"], Union[bool, Awaitable[bool]]]
49
+ ]
50
+ RouteHandlerCallback = Union[
51
+ Callable[["Route"], Any], Callable[["Route", "Request"], Any]
52
+ ]
53
+ WebSocketRouteHandlerCallback = Callable[["WebSocketRoute"], Any]
54
+ ColorScheme = Literal["dark", "light", "no-preference", "null"]
55
+ ForcedColors = Literal["active", "none", "null"]
56
+ Contrast = Literal["more", "no-preference", "null"]
57
+ ReducedMotion = Literal["no-preference", "null", "reduce"]
58
+ DocumentLoadState = Literal["commit", "domcontentloaded", "load", "networkidle"]
59
+ KeyboardModifier = Literal["Alt", "Control", "ControlOrMeta", "Meta", "Shift"]
60
+ MouseButton = Literal["left", "middle", "right"]
61
+ ServiceWorkersPolicy = Literal["allow", "block"]
62
+ HarMode = Literal["full", "minimal"]
63
+ HarContentPolicy = Literal["attach", "embed", "omit"]
64
+ RouteFromHarNotFoundPolicy = Literal["abort", "fallback"]
65
+
66
+
67
+ class ErrorPayload(TypedDict, total=False):
68
+ message: str
69
+ name: str
70
+ stack: str
71
+ value: Optional[Any]
72
+
73
+
74
+ class HarRecordingMetadata(TypedDict, total=False):
75
+ path: str
76
+ content: Optional[HarContentPolicy]
77
+
78
+
79
+ def prepare_record_har_options(params: Dict) -> Dict[str, Any]:
80
+ out_params: Dict[str, Any] = {"path": str(params["recordHarPath"])}
81
+ if "recordHarUrlFilter" in params:
82
+ opt = params["recordHarUrlFilter"]
83
+ if isinstance(opt, str):
84
+ out_params["urlGlob"] = opt
85
+ if isinstance(opt, Pattern):
86
+ out_params["urlRegexSource"] = opt.pattern
87
+ out_params["urlRegexFlags"] = escape_regex_flags(opt)
88
+ del params["recordHarUrlFilter"]
89
+ if "recordHarMode" in params:
90
+ out_params["mode"] = params["recordHarMode"]
91
+ del params["recordHarMode"]
92
+ new_content_api = None
93
+ old_content_api = None
94
+ if "recordHarContent" in params:
95
+ new_content_api = params["recordHarContent"]
96
+ del params["recordHarContent"]
97
+ if "recordHarOmitContent" in params:
98
+ old_content_api = params["recordHarOmitContent"]
99
+ del params["recordHarOmitContent"]
100
+ content = new_content_api or ("omit" if old_content_api else None)
101
+ if content:
102
+ out_params["content"] = content
103
+ return out_params
104
+
105
+
106
+ class ParsedMessageParams(TypedDict):
107
+ type: str
108
+ guid: str
109
+ initializer: Dict
110
+
111
+
112
+ class ParsedMessagePayload(TypedDict, total=False):
113
+ id: int
114
+ guid: str
115
+ method: str
116
+ params: ParsedMessageParams
117
+ result: Any
118
+ error: ErrorPayload
119
+ errorDetails: Any
120
+ log: List[str]
121
+
122
+
123
+ class Document(TypedDict):
124
+ request: Optional[Any]
125
+
126
+
127
+ class FrameNavigatedEvent(TypedDict):
128
+ url: str
129
+ name: str
130
+ newDocument: Optional[Document]
131
+ error: Optional[str]
132
+
133
+
134
+ Env = Dict[str, Union[str, float, bool]]
135
+
136
+
137
+ def url_matches(
138
+ base_url: Optional[str],
139
+ url_string: str,
140
+ match: Optional[URLMatch],
141
+ websocket_url: bool = None,
142
+ ) -> bool:
143
+ if not match:
144
+ return True
145
+ if isinstance(match, str):
146
+ match = re.compile(
147
+ resolve_glob_to_regex_pattern(base_url, match, websocket_url)
148
+ )
149
+ if isinstance(match, Pattern):
150
+ return bool(match.search(url_string))
151
+ return match(url_string)
152
+
153
+
154
+ def resolve_glob_to_regex_pattern(
155
+ base_url: Optional[str], glob: str, websocket_url: bool = None
156
+ ) -> str:
157
+ if websocket_url:
158
+ base_url = to_websocket_base_url(base_url)
159
+ glob = resolve_glob_base(base_url, glob)
160
+ return glob_to_regex_pattern(glob)
161
+
162
+
163
+ def to_websocket_base_url(base_url: Optional[str]) -> Optional[str]:
164
+ if base_url is not None and re.match("^https?://", base_url):
165
+ base_url = re.sub("^http", "ws", base_url)
166
+ return base_url
167
+
168
+
169
+ def resolve_glob_base(base_url: Optional[str], match: str) -> str:
170
+ if match[0] == "*":
171
+ return match
172
+ token_map: Dict[str, str] = {}
173
+
174
+ def map_token(original: str, replacement: str) -> str:
175
+ if len(original) == 0:
176
+ return ""
177
+ token_map[replacement] = original
178
+ return replacement
179
+
180
+ match = match.replace("\\\\?", "?")
181
+ if (
182
+ match.startswith("about:")
183
+ or match.startswith("data:")
184
+ or match.startswith("chrome:")
185
+ or match.startswith("edge:")
186
+ or match.startswith("file:")
187
+ ):
188
+ return match
189
+ processed_parts = []
190
+ for index, token in enumerate(match.split("/")):
191
+ if token in (".", "..", ""):
192
+ processed_parts.append(token)
193
+ continue
194
+ if index == 0 and token.endswith(":"):
195
+ if "*" in token or "{" in token:
196
+ processed_parts.append(map_token(token, "http:"))
197
+ else:
198
+ processed_parts.append(token)
199
+ continue
200
+ question_index = token.find("?")
201
+ if question_index == -1:
202
+ processed_parts.append(map_token(token, f"$_{index}_$"))
203
+ else:
204
+ new_prefix = map_token(token[:question_index], f"$_{index}_$")
205
+ new_suffix = map_token(token[question_index:], f"?$_{index}_$")
206
+ processed_parts.append(new_prefix + new_suffix)
207
+ relative_path = "/".join(processed_parts)
208
+ resolved, case_insensitive_part = resolve_base_url(base_url, relative_path)
209
+ for token, original in token_map.items():
210
+ normalize = case_insensitive_part and token in case_insensitive_part
211
+ resolved = resolved.replace(
212
+ token, original.lower() if normalize else original, 1
213
+ )
214
+ return resolved
215
+
216
+
217
+ def resolve_base_url(
218
+ base_url: Optional[str], given_url: str
219
+ ) -> Tuple[str, Optional[str]]:
220
+ try:
221
+ url = nodelike_urlparse(
222
+ urljoin(base_url if base_url is not None else "", given_url)
223
+ )
224
+ resolved = urlunparse(url)
225
+ hostname_port = url.hostname or ""
226
+ if url.port:
227
+ hostname_port += f":{url.port}"
228
+ case_insensitive_prefix = f"{url.scheme}://{hostname_port}"
229
+ return (resolved, case_insensitive_prefix)
230
+ except Exception:
231
+ return (given_url, None)
232
+
233
+
234
+ def nodelike_urlparse(url: str) -> ParseResult:
235
+ parsed = urlparse(url, allow_fragments=True)
236
+ is_special_url = parsed.scheme in ["http", "https", "ws", "wss", "ftp", "file"]
237
+ if is_special_url:
238
+ if parsed.path == "":
239
+ parsed = parsed._replace(path="/")
240
+ return parsed
241
+
242
+
243
+ class HarLookupResult(TypedDict, total=False):
244
+ action: Literal["error", "redirect", "fulfill", "noentry"]
245
+ message: Optional[str]
246
+ redirectURL: Optional[str]
247
+ status: Optional[int]
248
+ headers: Optional["HeadersArray"]
249
+ body: Optional[str]
250
+
251
+
252
+ DEFAULT_PLAYWRIGHT_TIMEOUT_IN_MILLISECONDS = 30000
253
+ DEFAULT_PLAYWRIGHT_LAUNCH_TIMEOUT_IN_MILLISECONDS = 180000
254
+ PLAYWRIGHT_MAX_DEADLINE = 2147483647
255
+
256
+
257
+ class TimeoutSettings:
258
+ @staticmethod
259
+ def launch_timeout(timeout: Optional[float] = None) -> float:
260
+ return (
261
+ timeout
262
+ if timeout is not None
263
+ else DEFAULT_PLAYWRIGHT_LAUNCH_TIMEOUT_IN_MILLISECONDS
264
+ )
265
+
266
+ def __init__(self, parent: Optional["TimeoutSettings"]) -> None:
267
+ self._parent = parent
268
+ self._default_timeout: Optional[float] = None
269
+ self._default_navigation_timeout: Optional[float] = None
270
+
271
+ def set_default_timeout(self, timeout: Optional[float]) -> None:
272
+ self._default_timeout = timeout
273
+
274
+ def timeout(self, timeout: float = None) -> float:
275
+ if timeout is not None:
276
+ return timeout
277
+ if self._default_timeout is not None:
278
+ return self._default_timeout
279
+ if self._parent:
280
+ return self._parent.timeout()
281
+ return DEFAULT_PLAYWRIGHT_TIMEOUT_IN_MILLISECONDS
282
+
283
+ def set_default_navigation_timeout(
284
+ self, navigation_timeout: Optional[float]
285
+ ) -> None:
286
+ self._default_navigation_timeout = navigation_timeout
287
+
288
+ def default_navigation_timeout(self) -> Optional[float]:
289
+ return self._default_navigation_timeout
290
+
291
+ def default_timeout(self) -> Optional[float]:
292
+ return self._default_timeout
293
+
294
+ def navigation_timeout(self, timeout: float = None) -> float:
295
+ if timeout is not None:
296
+ return timeout
297
+ if self._default_navigation_timeout is not None:
298
+ return self._default_navigation_timeout
299
+ if self._default_timeout is not None:
300
+ return self._default_timeout
301
+ if self._parent:
302
+ return self._parent.navigation_timeout()
303
+ return DEFAULT_PLAYWRIGHT_TIMEOUT_IN_MILLISECONDS
304
+
305
+
306
+ def serialize_error(ex: Exception, tb: Optional[TracebackType]) -> ErrorPayload:
307
+ return ErrorPayload(
308
+ message=str(ex), name="Error", stack="".join(traceback.format_tb(tb))
309
+ )
310
+
311
+
312
+ def parse_error(error: ErrorPayload, log: Optional[str] = None) -> Error:
313
+ base_error_class = Error
314
+ if error.get("name") == "TimeoutError":
315
+ base_error_class = TimeoutError
316
+ if error.get("name") == "TargetClosedError":
317
+ base_error_class = TargetClosedError
318
+ if not log:
319
+ log = ""
320
+ exc = base_error_class(patch_error_message(error["message"]) + log)
321
+ exc._name = error["name"]
322
+ exc._stack = error["stack"]
323
+ return exc
324
+
325
+
326
+ def patch_error_message(message: str) -> str:
327
+ match = re.match("(\\w+)(: expected .*)", message)
328
+ if match:
329
+ message = to_snake_case(match.group(1)) + match.group(2)
330
+ message = message.replace(
331
+ "Pass { acceptDownloads: true }", "Pass 'accept_downloads=True'"
332
+ )
333
+ return message
334
+
335
+
336
+ def locals_to_params(args: Dict) -> Dict:
337
+ copy = {}
338
+ for key in args:
339
+ if key == "self":
340
+ continue
341
+ if args[key] is not None:
342
+ copy[key] = (
343
+ args[key]
344
+ if not isinstance(args[key], Dict)
345
+ else locals_to_params(args[key])
346
+ )
347
+ return copy
348
+
349
+
350
+ def monotonic_time() -> int:
351
+ return math.floor(time.monotonic() * 1000)
352
+
353
+
354
+ def create_task_and_ignore_exception(
355
+ loop: asyncio.AbstractEventLoop, coro: Awaitable[Any]
356
+ ) -> None:
357
+
358
+ async def _ignore_exception() -> None:
359
+ try:
360
+ await coro
361
+ except Exception:
362
+ pass
363
+
364
+ loop.create_task(_ignore_exception())
365
+
366
+
367
+ class RouteHandlerInvocation:
368
+ complete: "asyncio.Future"
369
+ route: "Route"
370
+
371
+ def __init__(self, complete: "asyncio.Future", route: "Route") -> None:
372
+ self.complete = complete
373
+ self.route = route
374
+
375
+
376
+ class RouteHandler:
377
+ def __init__(
378
+ self,
379
+ base_url: Optional[str],
380
+ url: URLMatch,
381
+ handler: RouteHandlerCallback,
382
+ is_sync: bool,
383
+ times: Optional[int] = None,
384
+ ):
385
+ self._base_url = base_url
386
+ self.url = url
387
+ self.handler = handler
388
+ self._times = times if times else math.inf
389
+ self._handled_count = 0
390
+ self._is_sync = is_sync
391
+ self._ignore_exception = False
392
+ self._active_invocations: Set[RouteHandlerInvocation] = set()
393
+
394
+ def matches(self, request_url: str) -> bool:
395
+ return url_matches(self._base_url, request_url, self.url)
396
+
397
+ async def handle(self, route: "Route") -> bool:
398
+ handler_invocation = RouteHandlerInvocation(
399
+ asyncio.get_running_loop().create_future(), route
400
+ )
401
+ self._active_invocations.add(handler_invocation)
402
+ try:
403
+ return await self._handle_internal(route)
404
+ except Exception as e:
405
+ if self._ignore_exception:
406
+ return False
407
+ if is_target_closed_error(e):
408
+ optional_async_prefix = "await " if not self._is_sync else ""
409
+ raise rewrite_error(
410
+ e,
411
+ f'''"{str(e)}" while running route callback.\nConsider awaiting `{optional_async_prefix}page.unroute_all(behavior='ignoreErrors')`\nbefore the end of the test to ignore remaining routes in flight.''',
412
+ )
413
+ raise e
414
+ finally:
415
+ handler_invocation.complete.set_result(None)
416
+ self._active_invocations.remove(handler_invocation)
417
+
418
+ async def _handle_internal(self, route: "Route") -> bool:
419
+ handled_future = route._start_handling()
420
+ self._handled_count += 1
421
+ if self._is_sync:
422
+ handler_finished_future = route._loop.create_future()
423
+
424
+ def _handler() -> None:
425
+ try:
426
+ self.handler(route, route.request)
427
+ handler_finished_future.set_result(None)
428
+ except Exception as e:
429
+ handler_finished_future.set_exception(e)
430
+
431
+ g = RouteGreenlet(_handler)
432
+ g.switch()
433
+ await handler_finished_future
434
+ else:
435
+ coro_or_future = self.handler(route, route.request)
436
+ if coro_or_future:
437
+ await asyncio.ensure_future(coro_or_future)
438
+ return await handled_future
439
+
440
+ async def stop(self, behavior: Literal["ignoreErrors", "wait"]) -> None:
441
+ if behavior == "ignoreErrors":
442
+ self._ignore_exception = True
443
+ else:
444
+ tasks = []
445
+ for activation in self._active_invocations:
446
+ if not activation.route._did_throw:
447
+ tasks.append(activation.complete)
448
+ await asyncio.gather(*tasks)
449
+
450
+ @property
451
+ def will_expire(self) -> bool:
452
+ return self._handled_count + 1 >= self._times
453
+
454
+ @staticmethod
455
+ def prepare_interception_patterns(
456
+ handlers: List["RouteHandler"],
457
+ ) -> List[Dict[str, str]]:
458
+ patterns = []
459
+ all = False
460
+ for handler in handlers:
461
+ if isinstance(handler.url, str):
462
+ patterns.append({"glob": handler.url})
463
+ elif isinstance(handler.url, re.Pattern):
464
+ patterns.append(
465
+ {
466
+ "regexSource": handler.url.pattern,
467
+ "regexFlags": escape_regex_flags(handler.url),
468
+ }
469
+ )
470
+ else:
471
+ all = True
472
+ if all:
473
+ return [{"glob": "**/*"}]
474
+ return patterns
475
+
476
+
477
+ to_snake_case_regex = re.compile("((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))")
478
+
479
+
480
+ def to_snake_case(name: str) -> str:
481
+ return to_snake_case_regex.sub("_\\1", name).lower()
482
+
483
+
484
+ def make_dirs_for_file(path: Union[Path, str]) -> None:
485
+ if not os.path.isabs(path):
486
+ path = Path.cwd() / path
487
+ os.makedirs(os.path.dirname(path), exist_ok=True)
488
+
489
+
490
+ async def async_writefile(file: Union[str, Path], data: Union[str, bytes]) -> None:
491
+
492
+ def inner() -> None:
493
+ with open(file, "w" if isinstance(data, str) else "wb") as fh:
494
+ fh.write(data)
495
+
496
+ loop = asyncio.get_running_loop()
497
+ await loop.run_in_executor(None, inner)
498
+
499
+
500
+ async def async_readfile(file: Union[str, Path]) -> bytes:
501
+
502
+ def inner() -> bytes:
503
+ with open(file, "rb") as fh:
504
+ return fh.read()
505
+
506
+ loop = asyncio.get_running_loop()
507
+ return await loop.run_in_executor(None, inner)
508
+
509
+
510
+ T = TypeVar("T")
511
+
512
+
513
+ def to_impl(obj: T) -> T:
514
+ if hasattr(obj, "_impl_obj"):
515
+ return cast(Any, obj)._impl_obj
516
+ return obj
517
+
518
+
519
+ def object_to_array(obj: Optional[Dict]) -> Optional[List[NameValue]]:
520
+ if not obj:
521
+ return None
522
+ result = []
523
+ for key, value in obj.items():
524
+ result.append(NameValue(name=key, value=str(value)))
525
+ return result
526
+
527
+
528
+ def is_file_payload(value: Optional[Any]) -> bool:
529
+ return (
530
+ isinstance(value, dict)
531
+ and "name" in value
532
+ and ("mimeType" in value)
533
+ and ("buffer" in value)
534
+ )
535
+
536
+
537
+ TEXTUAL_MIME_TYPE = re.compile(
538
+ "^(text\\/.*?|application\\/(json|(x-)?javascript|xml.*?|ecmascript|graphql|x-www-form-urlencoded)|image\\/svg(\\+xml)?|application\\/.*?(\\+json|\\+xml))(;\\s*charset=.*)?$"
539
+ )
540
+
541
+
542
+ def is_textual_mime_type(mime_type: str) -> bool:
543
+ return bool(TEXTUAL_MIME_TYPE.match(mime_type))
544
+
545
+
546
+ def to_milliseconds(value: Union[float, datetime.timedelta, None]) -> Optional[float]:
547
+ if value is None:
548
+ return None
549
+ if isinstance(value, datetime.timedelta):
550
+ return value / datetime.timedelta(milliseconds=1)
551
+ return value
@@ -0,0 +1,141 @@
1
+ import inspect
2
+ from typing import Any, Callable, Dict, List, Optional, Sequence, Union
3
+ from patchright._impl._errors import Error
4
+ from patchright._impl._map import Map
5
+
6
+ API_ATTR = "_pw_api_instance_"
7
+ IMPL_ATTR = "_pw_impl_instance_"
8
+
9
+
10
+ class ImplWrapper:
11
+ def __init__(self, impl_obj: Any) -> None:
12
+ self._impl_obj = impl_obj
13
+
14
+ def __repr__(self) -> str:
15
+ return self._impl_obj.__repr__()
16
+
17
+
18
+ class ImplToApiMapping:
19
+ def __init__(self) -> None:
20
+ self._mapping: Dict[type, type] = {}
21
+
22
+ def register(self, impl_class: type, api_class: type) -> None:
23
+ self._mapping[impl_class] = api_class
24
+
25
+ def from_maybe_impl(
26
+ self, obj: Any, visited: Optional[Map[Any, Union[List, Dict]]] = None
27
+ ) -> Any:
28
+ if not visited:
29
+ visited = Map()
30
+ if not obj:
31
+ return obj
32
+ if isinstance(obj, dict):
33
+ if obj in visited:
34
+ return visited[obj]
35
+ o: Dict = {}
36
+ visited[obj] = o
37
+ for name, value in obj.items():
38
+ o[name] = self.from_maybe_impl(value, visited)
39
+ return o
40
+ if isinstance(obj, list):
41
+ if obj in visited:
42
+ return visited[obj]
43
+ a: List = []
44
+ visited[obj] = a
45
+ for item in obj:
46
+ a.append(self.from_maybe_impl(item, visited))
47
+ return a
48
+ api_class = self._mapping.get(type(obj))
49
+ if api_class:
50
+ api_instance = getattr(obj, API_ATTR, None)
51
+ if not api_instance:
52
+ api_instance = api_class(obj)
53
+ setattr(obj, API_ATTR, api_instance)
54
+ return api_instance
55
+ else:
56
+ return obj
57
+
58
+ def from_impl(self, obj: Any) -> Any:
59
+ assert obj
60
+ result = self.from_maybe_impl(obj)
61
+ assert result
62
+ return result
63
+
64
+ def from_impl_nullable(self, obj: Any = None) -> Optional[Any]:
65
+ return self.from_impl(obj) if obj else None
66
+
67
+ def from_impl_list(self, items: Sequence[Any]) -> List[Any]:
68
+ return list(map(lambda a: self.from_impl(a), items))
69
+
70
+ def from_impl_dict(self, map: Dict[str, Any]) -> Dict[str, Any]:
71
+ return {name: self.from_impl(value) for name, value in map.items()}
72
+
73
+ def to_impl(
74
+ self, obj: Any, visited: Optional[Map[Any, Union[List, Dict]]] = None
75
+ ) -> Any:
76
+ if visited is None:
77
+ visited = Map()
78
+ try:
79
+ if not obj:
80
+ return obj
81
+ if isinstance(obj, dict):
82
+ if obj in visited:
83
+ return visited[obj]
84
+ o: Dict = {}
85
+ visited[obj] = o
86
+ for name, value in obj.items():
87
+ o[name] = self.to_impl(value, visited)
88
+ return o
89
+ if isinstance(obj, list):
90
+ if obj in visited:
91
+ return visited[obj]
92
+ a: List = []
93
+ visited[obj] = a
94
+ for item in obj:
95
+ a.append(self.to_impl(item, visited))
96
+ return a
97
+ if isinstance(obj, ImplWrapper):
98
+ return obj._impl_obj
99
+ return obj
100
+ except RecursionError:
101
+ raise Error("Maximum argument depth exceeded")
102
+
103
+ def wrap_handler(self, handler: Callable[..., Any]) -> Callable[..., None]:
104
+
105
+ def wrapper_func(*args: Any) -> Any:
106
+ parameters = inspect.signature(handler).parameters
107
+ has_varargs = any(
108
+ (
109
+ parameter.kind == inspect.Parameter.VAR_POSITIONAL
110
+ for parameter in parameters.values()
111
+ )
112
+ )
113
+ arg_count = (
114
+ len(args)
115
+ if has_varargs
116
+ else sum(
117
+ (
118
+ parameter.kind
119
+ in (
120
+ inspect.Parameter.POSITIONAL_ONLY,
121
+ inspect.Parameter.POSITIONAL_OR_KEYWORD,
122
+ )
123
+ for parameter in parameters.values()
124
+ )
125
+ )
126
+ )
127
+ return handler(
128
+ *list(map(lambda a: self.from_maybe_impl(a), args))[:arg_count]
129
+ )
130
+
131
+ if inspect.ismethod(handler):
132
+ wrapper = getattr(handler.__self__, IMPL_ATTR + handler.__name__, None)
133
+ if not wrapper:
134
+ wrapper = wrapper_func
135
+ setattr(handler.__self__, IMPL_ATTR + handler.__name__, wrapper)
136
+ return wrapper
137
+ wrapper = getattr(handler, IMPL_ATTR, None)
138
+ if not wrapper:
139
+ wrapper = wrapper_func
140
+ setattr(handler, IMPL_ATTR, wrapper)
141
+ return wrapper