| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
) |