Skip to content

Commit

Permalink
update default_recv_callback format with (tab, data_dict)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClericPy committed Oct 12, 2020
1 parent 0869a23 commit e4e598e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
8 changes: 8 additions & 0 deletions examples_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,16 @@ def on_shutdown(chromed):
# test init tab from chromed
async with chromed.connect_tab("https://github.com",
auto_close=True) as tab:
TEST_DRC_OK = False

def test_drc(tab, data_dict):
nonlocal TEST_DRC_OK
TEST_DRC_OK = True

tab.default_recv_callback.append(test_drc)
await tab.wait_loading(5)
title = await tab.current_title
assert TEST_DRC_OK, 'test default_recv_callback failed'
assert 'GitHub' in title
logger.info('test init tab from chromed OK.')
# test on_startup
Expand Down
26 changes: 22 additions & 4 deletions ichrome/async_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from torequests.utils import UA, quote_plus, urljoin

from .base import (INF, NotSet, Tag, TagNotFound, async_run,
clear_chrome_process, get_memory_by_port)
clear_chrome_process, ensure_awaitable, get_memory_by_port)
from .exceptions import ChromeRuntimeError, ChromeTypeError, ChromeValueError
from .logs import logger
"""
Expand Down Expand Up @@ -291,6 +291,7 @@ def __init__(self,
self._listener = Listener()
self._enabled_domains: Set[str] = set()
self._default_recv_callback: List[Callable] = []
# using default_recv_callback.setter, default_recv_callback can be list or function
self.default_recv_callback = default_recv_callback

def __hash__(self):
Expand Down Expand Up @@ -320,7 +321,7 @@ def ensure_timeout(self, timeout):

@property
def default_recv_callback(self):
return self._default_recv_callback or []
return self._default_recv_callback

@default_recv_callback.setter
def default_recv_callback(self, value):
Expand All @@ -334,11 +335,28 @@ def default_recv_callback(self, value):
raise ChromeValueError(
'default_recv_callback should be list or callable, and you can use tab.default_recv_callback.append(cb) to add new callback'
)
self.ensure_callback_type(self.default_recv_callback)

@default_recv_callback.deleter
def default_recv_callback(self):
self._default_recv_callback = []

@staticmethod
def ensure_callback_type(_default_recv_callback):
"""
Ensure callback function has correct args
"""
must_args = ('tab', 'data_dict')
for func in _default_recv_callback:
if not callable(func):
raise ChromeTypeError(
f'callback function ({getattr(func, "__name__", func)}) should be callable')
if not inspect.isbuiltin(func) and len(
inspect.signature(func).parameters) != 2:
raise ChromeTypeError(
f'callback function ({getattr(func, "__name__", func)}) should handle two args for {must_args}'
)

async def close_browser(self, timeout=0):
return await self.send('Browser.close', timeout=timeout)

Expand Down Expand Up @@ -442,9 +460,9 @@ async def _recv_daemon(self):
logger.debug(
f'[json] data_str can not be json.loads: {data_str}')
continue
for cb in self.default_recv_callback:
for callback in self.default_recv_callback:
asyncio.ensure_future(
_ensure_awaitable_callback_result(cb, data_dict))
ensure_awaitable(callback(self, data_dict)))
f = self._listener.find_future(data_dict)
if f:
if f._state == _PENDING:
Expand Down

0 comments on commit e4e598e

Please sign in to comment.