okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
7de30f7
main
1"""Subscriptions are explicit scopes, separate from observation and interaction.""" 2 3from __future__import annotations 4from typing import Any 5from .transport import CDPSession ,Subscription 6 7 8class EventScope : 9def __init__ ( 10self , 11cdp :CDPSession , 12method :str , 13* , 14capacity :int = 256 , 15max_bytes :int = 2 * 1024 * 1024 , 16enable :bool = True , 17 )-> None : 18self .cdp = cdp 19self .method = method 20self .capacity = capacity 21self .max_bytes = max_bytes 22self .enable = enable 23self .subscription :Subscription | None = None 24 25async def __aenter__ (self )-> Subscription : 26if self .subscription is not None : 27raise RuntimeError ("An event scope cannot be entered twice" ) 28self .subscription = self .cdp .connection .subscribe ( 29self .method , 30self .cdp .session_id , 31capacity = self .capacity , 32max_bytes = self .max_bytes , 33 ) 34try : 35if self .enable : 36domain = self .method .split ("." )[0 ] 37if domain not in { 38"Network" , 39"Runtime" , 40"Page" , 41"DOM" , 42"Accessibility" , 43"Log" , 44"Performance" , 45"Security" , 46 }: 47raise ValueError ( 48f"Enable { domain } explicitly with CDP, then use enable=False" 49 ) 50await self .cdp .send (f" { domain } .enable" ) 51return self .subscription 52except BaseException : 53self .subscription .close () 54raise 55 56async def __aexit__ (self ,* exc :Any )-> None : 57if self .subscription is not None : 58self .subscription .close ()