|
| 1 | +# Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The SFC licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import typing |
| 19 | +from dataclasses import dataclass |
| 20 | + |
| 21 | +from .session import session_subscribe |
| 22 | +from .session import session_unsubscribe |
| 23 | + |
| 24 | + |
| 25 | +class Script: |
| 26 | + def __init__(self, conn): |
| 27 | + self.conn = conn |
| 28 | + self.log_entry_subscribed = False |
| 29 | + |
| 30 | + def add_console_message_handler(self, handler): |
| 31 | + self._subscribe_to_log_entries() |
| 32 | + return self.conn.add_callback(LogEntryAdded, self._handle_log_entry("console", handler)) |
| 33 | + |
| 34 | + def add_javascript_error_handler(self, handler): |
| 35 | + self._subscribe_to_log_entries() |
| 36 | + return self.conn.add_callback(LogEntryAdded, self._handle_log_entry("javascript", handler)) |
| 37 | + |
| 38 | + def remove_console_message_handler(self, id): |
| 39 | + self.conn.remove_callback(LogEntryAdded, id) |
| 40 | + self._unsubscribe_from_log_entries() |
| 41 | + |
| 42 | + remove_javascript_error_handler = remove_console_message_handler |
| 43 | + |
| 44 | + def _subscribe_to_log_entries(self): |
| 45 | + if not self.log_entry_subscribed: |
| 46 | + self.conn.execute(session_subscribe(LogEntryAdded.event_class)) |
| 47 | + self.log_entry_subscribed = True |
| 48 | + |
| 49 | + def _unsubscribe_from_log_entries(self): |
| 50 | + if self.log_entry_subscribed and LogEntryAdded.event_class not in self.conn.callbacks: |
| 51 | + self.conn.execute(session_unsubscribe(LogEntryAdded.event_class)) |
| 52 | + self.log_entry_subscribed = False |
| 53 | + |
| 54 | + def _handle_log_entry(self, type, handler): |
| 55 | + def _handle_log_entry(log_entry): |
| 56 | + if log_entry.type_ == type: |
| 57 | + handler(log_entry) |
| 58 | + |
| 59 | + return _handle_log_entry |
| 60 | + |
| 61 | + |
| 62 | +class LogEntryAdded: |
| 63 | + event_class = "log.entryAdded" |
| 64 | + |
| 65 | + @classmethod |
| 66 | + def from_json(cls, json): |
| 67 | + print(json) |
| 68 | + if json["type"] == "console": |
| 69 | + return ConsoleLogEntry.from_json(json) |
| 70 | + elif json["type"] == "javascript": |
| 71 | + return JavaScriptLogEntry.from_json(json) |
| 72 | + |
| 73 | + |
| 74 | +@dataclass |
| 75 | +class ConsoleLogEntry: |
| 76 | + level: str |
| 77 | + text: str |
| 78 | + timestamp: str |
| 79 | + method: str |
| 80 | + args: typing.List[dict] |
| 81 | + type_: str |
| 82 | + |
| 83 | + @classmethod |
| 84 | + def from_json(cls, json): |
| 85 | + return cls( |
| 86 | + level=json["level"], |
| 87 | + text=json["text"], |
| 88 | + timestamp=json["timestamp"], |
| 89 | + method=json["method"], |
| 90 | + args=json["args"], |
| 91 | + type_=json["type"], |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +@dataclass |
| 96 | +class JavaScriptLogEntry: |
| 97 | + level: str |
| 98 | + text: str |
| 99 | + timestamp: str |
| 100 | + stacktrace: dict |
| 101 | + type_: str |
| 102 | + |
| 103 | + @classmethod |
| 104 | + def from_json(cls, json): |
| 105 | + return cls( |
| 106 | + level=json["level"], |
| 107 | + text=json["text"], |
| 108 | + timestamp=json["timestamp"], |
| 109 | + stacktrace=json["stackTrace"], |
| 110 | + type_=json["type"], |
| 111 | + ) |
0 commit comments