Skip to content

Commit

Permalink
Merge pull request #130 from plotly/neyberson/cleanup
Browse files Browse the repository at this point in the history
Neyberson/cleanup
  • Loading branch information
ayjayt authored Oct 7, 2024
2 parents 217cae7 + 533c62a commit 8bb9fad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
20 changes: 12 additions & 8 deletions devtools/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def __init__(

# Initializing
super().__init__("0", self) # NOTE: 0 can't really be used externally
self.add_session(Session(self, ""))
self._add_session(Session(self, ""))

if not self.loop:
self._open()
Expand Down Expand Up @@ -407,7 +407,9 @@ async def create_tab(self, url="", width=None, height=None):

response = await self.browser.send_command("Target.createTarget", params=params)
if "error" in response:
raise RuntimeError("Could not create tab") from Exception(response["error"])
raise RuntimeError("Could not create tab") from DevtoolsProtocolError(
response
)
target_id = response["result"]["targetId"]
new_tab = Tab(target_id, self)
self._add_tab(new_tab)
Expand All @@ -429,7 +431,9 @@ async def close_tab(self, target_id):
)
self._remove_tab(target_id)
if "error" in response:
raise RuntimeError("Could not close tab") from Exception(response["error"])
raise RuntimeError("Could not close tab") from DevtoolsProtocolError(
response
)
return response

async def create_session(self):
Expand All @@ -442,12 +446,12 @@ async def create_session(self):
)
response = await self.browser.send_command("Target.attachToBrowserTarget")
if "error" in response:
raise RuntimeError("Could not create session") from Exception(
response["error"]
raise RuntimeError("Could not create session") from DevtoolsProtocolError(
response
)
session_id = response["result"]["sessionId"]
new_session = Session(self, session_id)
self.add_session(new_session)
self._add_session(new_session)
return new_session

async def populate_targets(self):
Expand Down Expand Up @@ -522,7 +526,7 @@ async def read_loop():
error = self.protocol.get_error(response)
key = self.protocol.calculate_key(response)
if not self.protocol.has_id(response) and error:
raise RuntimeError(error)
raise DevtoolsProtocolError(response)
elif self.protocol.is_event(response):
### INFORMATION WE NEED FOR EVERY EVENT
event_session_id = response.get("sessionId", "") # GET THE SESSION THAT THE EVENT CAME IN ON
Expand Down Expand Up @@ -569,7 +573,7 @@ async def read_loop():
if session_closed == "": continue # not actually possible to close browser session this way...
target_closed = self._get_target_for_session(session_closed)
if target_closed:
target_closed.remove_session(session_closed)
target_closed._remove_session(session_closed)
_ = self.protocol.sessions.pop(session_closed, None)
if self.debug:
print(
Expand Down
12 changes: 6 additions & 6 deletions devtools/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ def __init__(self, target_id, browser):
self.sessions = OrderedDict()
self.target_id = target_id

def add_session(self, session):
def _add_session(self, session):
if not isinstance(session, Session):
raise TypeError("session must be an object of class Session")
self.sessions[session.session_id] = session
self.browser.protocol.sessions[session.session_id] = session

def remove_session(self, session_id):
def _remove_session(self, session_id):
if isinstance(session_id, Session):
session_id = session_id.session_id
_ = self.sessions.pop(session_id, None)
Expand All @@ -43,7 +43,7 @@ async def create_session(self):
)
session_id = response["result"]["sessionId"]
new_session = Session(self.browser, session_id)
self.add_session(new_session)
self._add_session(new_session)
return new_session

async def close_session(self, session_id):
Expand All @@ -57,10 +57,10 @@ async def close_session(self, session_id):
command="Target.detachFromTarget",
params={"sessionId": session_id},
)
self.remove_session(session_id)
self._remove_session(session_id)
if "error" in response:
raise RuntimeError("Could not close session") from Exception(
response["error"]
raise RuntimeError("Could not close session") from DevtoolsProtocolError(
response
)
print(f"The session {session_id} has been closed", file=sys.stderr)
return response
Expand Down

0 comments on commit 8bb9fad

Please sign in to comment.