1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import base64
import collections.abc
import datetime
import math
import struct
import traceback
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
from urllib.parse import ParseResult, urlparse, urlunparse
from playwright._impl._connection import Channel, ChannelOwner, from_channel
from playwright._impl._errors import Error, is_target_closed_error
from playwright._impl._map import Map
if TYPE_CHECKING: # pragma: no cover
from playwright._impl._element_handle import ElementHandle
Serializable = Any
class VisitorInfo:
visited: Map[Any, int]
last_id: int
def __init__(self) -> None:
self.visited = Map()
self.last_id = 0
def visit(self, obj: Any) -> int:
assert obj not in self.visited
self.last_id += 1
self.visited[obj] = self.last_id
return self.last_id
class JSHandle(ChannelOwner):
def __init__(
self, parent: ChannelOwner, type: str, guid: str, initializer: Dict
) -> None:
super().__init__(parent, type, guid, initializer)
self._preview = self._initializer["preview"]
self._channel.on(
"previewUpdated", lambda params: self._on_preview_updated(params["preview"])
)
def __repr__(self) -> str:
return f"<JSHandle preview={self._preview}>"
def __str__(self) -> str:
return self._preview
def _on_preview_updated(self, preview: str) -> None:
self._preview = preview
async def evaluate(self, expression: str, arg: Serializable = None) -> Any:
return parse_result(
await self._channel.send(
"evaluateExpression",
None,
dict(
expression=expression,
arg=serialize_argument(arg),
),
)
)
async def evaluate_handle(
self, expression: str, arg: Serializable = None
) -> "JSHandle":
return from_channel(
await self._channel.send(
"evaluateExpressionHandle",
None,
dict(
expression=expression,
arg=serialize_argument(arg),
),
)
)
async def get_property(self, propertyName: str) -> "JSHandle":
return from_channel(
await self._channel.send("getProperty", None, dict(name=propertyName))
)
async def get_properties(self) -> Dict[str, "JSHandle"]:
return {
prop["name"]: from_channel(prop["value"])
for prop in await self._channel.send(
"getPropertyList",
None,
)
}
def as_element(self) -> Optional["ElementHandle"]:
return None
async def dispose(self) -> None:
try:
await self._channel.send(
"dispose",
None,
)
except Exception as e:
if not is_target_closed_error(e):
raise e
async def json_value(self) -> Any:
return parse_result(
await self._channel.send(
"jsonValue",
None,
)
)
def serialize_value(
value: Any, handles: List[Channel], visitor_info: Optional[VisitorInfo] = None
) -> Any:
if visitor_info is None:
visitor_info = VisitorInfo()
if isinstance(value, JSHandle):
h = len(handles)
handles.append(value._channel)
return dict(h=h)
if value is None:
return dict(v="null")
if isinstance(value, float):
if value == float("inf"):
return dict(v="Infinity")
if value == float("-inf"):
return dict(v="-Infinity")
if value == float("-0"):
return dict(v="-0")
if math.isnan(value):
return dict(v="NaN")
if isinstance(value, datetime.datetime):
# Node.js Date objects are always in UTC.
return {
"d": datetime.datetime.strftime(
value.astimezone(datetime.timezone.utc), "%Y-%m-%dT%H:%M:%S.%fZ"
)
}
if isinstance(value, Exception):
return {
"e": {
"m": str(value),
"n": (
(value.name or "")
if isinstance(value, Error)
else value.__class__.__name__
),
"s": (
(value.stack or "")
if isinstance(value, Error)
else "".join(
traceback.format_exception(type(value), value=value, tb=None)
)
),
}
}
if isinstance(value, bool):
return {"b": value}
if isinstance(value, (int, float)):
return {"n": value}
if isinstance(value, str):
return {"s": value}
if isinstance(value, ParseResult):
return {"u": urlunparse(value)}
if value in visitor_info.visited:
return dict(ref=visitor_info.visited[value])
if isinstance(value, collections.abc.Sequence) and not isinstance(value, str):
id = visitor_info.visit(value)
a = []
for e in value:
a.append(serialize_value(e, handles, visitor_info))
return dict(a=a, id=id)
if isinstance(value, dict):
id = visitor_info.visit(value)
o = []
for name in value:
o.append(
{"k": name, "v": serialize_value(value[name], handles, visitor_info)}
)
return dict(o=o, id=id)
return dict(v="undefined")
def serialize_argument(arg: Serializable = None) -> Any:
handles: List[Channel] = []
value = serialize_value(arg, handles)
return dict(value=value, handles=handles)
def parse_value(value: Any, refs: Optional[Dict[int, Any]] = None) -> Any:
if refs is None:
refs = {}
if value is None:
return None
if isinstance(value, dict):
if "ref" in value:
return refs[value["ref"]]
if "v" in value:
v = value["v"]
if v == "Infinity":
return float("inf")
if v == "-Infinity":
return float("-inf")
if v == "-0":
return float("-0")
if v == "NaN":
return float("nan")
if v == "undefined":
return None
if v == "null":
return None
return v
if "u" in value:
return urlparse(value["u"])
if "bi" in value:
return int(value["bi"])
if "e" in value:
error = Error(value["e"]["m"])
error._name = value["e"]["n"]
error._stack = value["e"]["s"]
return error
if "a" in value:
a: List = []
refs[value["id"]] = a
for e in value["a"]:
a.append(parse_value(e, refs))
return a
if "d" in value:
# Node.js Date objects are always in UTC.
return datetime.datetime.strptime(
value["d"], "%Y-%m-%dT%H:%M:%S.%fZ"
).replace(tzinfo=datetime.timezone.utc)
if "o" in value:
o: Dict = {}
refs[value["id"]] = o
for e in value["o"]:
o[e["k"]] = parse_value(e["v"], refs)
return o
if "n" in value:
return value["n"]
if "s" in value:
return value["s"]
if "b" in value:
return value["b"]
if "ta" in value:
encoded_bytes = value["ta"]["b"]
decoded_bytes = base64.b64decode(encoded_bytes)
array_type = value["ta"]["k"]
if array_type == "i8":
word_size = 1
fmt = "b"
elif array_type == "ui8" or array_type == "ui8c":
word_size = 1
fmt = "B"
elif array_type == "i16":
word_size = 2
fmt = "h"
elif array_type == "ui16":
word_size = 2
fmt = "H"
elif array_type == "i32":
word_size = 4
fmt = "i"
elif array_type == "ui32":
word_size = 4
fmt = "I"
elif array_type == "f32":
word_size = 4
fmt = "f"
elif array_type == "f64":
word_size = 8
fmt = "d"
elif array_type == "bi64":
word_size = 8
fmt = "q"
elif array_type == "bui64":
word_size = 8
fmt = "Q"
else:
raise ValueError(f"Unsupported array type: {array_type}")
byte_len = len(decoded_bytes)
if byte_len % word_size != 0:
raise ValueError(
f"Decoded bytes length {byte_len} is not a multiple of word size {word_size}"
)
if byte_len == 0:
return []
array_len = byte_len // word_size
# "<" denotes little-endian
format_string = f"<{array_len}{fmt}"
return list(struct.unpack(format_string, decoded_bytes))
return value
def parse_result(result: Any) -> Any:
return parse_value(result)
def add_source_url_to_script(source: str, path: Union[str, Path]) -> str:
return source + "\n//# sourceURL=" + str(path).replace("\n", "")
|