@@ -61,7 +61,9 @@ def import_devtools(ver):
6161 devtools_path = pathlib .Path (__file__ ).parents [1 ].joinpath ("devtools" )
6262 versions = tuple (f .name for f in devtools_path .iterdir () if f .is_dir ())
6363 available_versions = tuple (
64- x for x in versions if x == "latest" or (x .startswith ("v" ) and x [1 :].isdigit ())
64+ x
65+ for x in versions
66+ if x == "latest" or (x .startswith ("v" ) and x [1 :].isdigit ())
6567 )
6668 numeric_versions = tuple (x [1 :] for x in available_versions if x .startswith ("v" ))
6769 if not numeric_versions :
@@ -73,7 +75,9 @@ def import_devtools(ver):
7375 return devtools
7476
7577
76- _connection_context : contextvars .ContextVar = contextvars .ContextVar ("connection_context" )
78+ _connection_context : contextvars .ContextVar = contextvars .ContextVar (
79+ "connection_context"
80+ )
7781_session_context : contextvars .ContextVar = contextvars .ContextVar ("session_context" )
7882
7983
@@ -128,7 +132,9 @@ def set_global_connection(connection):
128132 certain use cases such as running inside Jupyter notebook.
129133 """
130134 global _connection_context
131- _connection_context = contextvars .ContextVar ("_connection_context" , default = connection )
135+ _connection_context = contextvars .ContextVar (
136+ "_connection_context" , default = connection
137+ )
132138
133139
134140def set_global_session (session ):
@@ -225,7 +231,9 @@ async def execute(self, cmd: Generator[dict, T, Any]) -> T:
225231 logger .debug (f"Received CDP message: { response } " )
226232 if isinstance (response , Exception ):
227233 if logger .isEnabledFor (logging .DEBUG ):
228- logger .debug (f"Exception raised by { cmd_event } message: { type (response ).__name__ } " )
234+ logger .debug (
235+ f"Exception raised by { cmd_event } message: { type (response ).__name__ } "
236+ )
229237 raise response
230238 return response
231239
@@ -241,7 +249,9 @@ def listen(self, *event_types, buffer_size=10):
241249 return receiver
242250
243251 @asynccontextmanager
244- async def wait_for (self , event_type : type [T ], buffer_size = 10 ) -> AsyncGenerator [CmEventProxy , None ]:
252+ async def wait_for (
253+ self , event_type : type [T ], buffer_size = 10
254+ ) -> AsyncGenerator [CmEventProxy , None ]:
245255 """Wait for an event of the given type and return it.
246256
247257 This is an async context manager, so you should open it inside
@@ -282,7 +292,9 @@ def _handle_cmd_response(self, data: dict):
282292 try :
283293 cmd , event = self .inflight_cmd .pop (cmd_id )
284294 except KeyError :
285- logger .warning ("Got a message with a command ID that does not exist: %s" , data )
295+ logger .warning (
296+ "Got a message with a command ID that does not exist: %s" , data
297+ )
286298 return
287299 if "error" in data :
288300 # If the server reported an error, convert it to an exception and do
@@ -293,7 +305,9 @@ def _handle_cmd_response(self, data: dict):
293305 # into a CDP object.
294306 try :
295307 _ = cmd .send (data ["result" ])
296- raise InternalError ("The command's generator function did not exit when expected!" )
308+ raise InternalError (
309+ "The command's generator function did not exit when expected!"
310+ )
297311 except StopIteration as exit :
298312 return_ = exit .value
299313 self .inflight_result [cmd_id ] = return_
@@ -307,15 +321,19 @@ def _handle_event(self, data: dict):
307321 """
308322 global devtools
309323 if devtools is None :
310- raise RuntimeError ("CDP devtools module not loaded. Call import_devtools() first." )
324+ raise RuntimeError (
325+ "CDP devtools module not loaded. Call import_devtools() first."
326+ )
311327 event = devtools .util .parse_json_event (data )
312328 logger .debug ("Received event: %s" , event )
313329 to_remove = set ()
314330 for sender in self .channels [type (event )]:
315331 try :
316332 sender .send_nowait (event )
317333 except trio .WouldBlock :
318- logger .error ('Unable to send event "%r" due to full channel %s' , event , sender )
334+ logger .error (
335+ 'Unable to send event "%r" due to full channel %s' , event , sender
336+ )
319337 except trio .BrokenResourceError :
320338 to_remove .add (sender )
321339 if to_remove :
@@ -433,8 +451,12 @@ async def connect_session(self, target_id) -> "CdpSession":
433451 """Returns a new :class:`CdpSession` connected to the specified target."""
434452 global devtools
435453 if devtools is None :
436- raise RuntimeError ("CDP devtools module not loaded. Call import_devtools() first." )
437- session_id = await self .execute (devtools .target .attach_to_target (target_id , True ))
454+ raise RuntimeError (
455+ "CDP devtools module not loaded. Call import_devtools() first."
456+ )
457+ session_id = await self .execute (
458+ devtools .target .attach_to_target (target_id , True )
459+ )
438460 session = CdpSession (self .ws , session_id , target_id )
439461 self .sessions [session_id ] = session
440462 return session
@@ -446,7 +468,9 @@ async def _reader_task(self):
446468 """
447469 global devtools
448470 if devtools is None :
449- raise RuntimeError ("CDP devtools module not loaded. Call import_devtools() first." )
471+ raise RuntimeError (
472+ "CDP devtools module not loaded. Call import_devtools() first."
473+ )
450474 while True :
451475 try :
452476 message = await self .ws .get_message ()
@@ -459,7 +483,13 @@ async def _reader_task(self):
459483 try :
460484 data = json .loads (message )
461485 except json .JSONDecodeError :
462- raise BrowserError ({"code" : - 32700 , "message" : "Client received invalid JSON" , "data" : message })
486+ raise BrowserError (
487+ {
488+ "code" : - 32700 ,
489+ "message" : "Client received invalid JSON" ,
490+ "data" : message ,
491+ }
492+ )
463493 logger .debug ("Received message %r" , data )
464494 if "sessionId" in data :
465495 session_id = devtools .target .SessionID (data ["sessionId" ])
0 commit comments