Skip to content

Commit

Permalink
Merge pull request #30 from ClericPy/dev
Browse files Browse the repository at this point in the history
2.3.1
  • Loading branch information
ClericPy authored Jun 22, 2020
2 parents 57fcf89 + 68b5ec8 commit 6c2fe6d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 37 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
1. `set_url` / `reload`
> navigate to a new url. `reload` equals to `set_url(None)`
> navigate to a new url(return bool for whether load finished), or send `Page.reload` message.
2. `wait_event`
> listening the events with given name, and separate from other same-name events with filter_function, finally run the callback_function with result.
Expand Down Expand Up @@ -364,23 +364,24 @@ Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD6
Type "help", "copyright", "credits" or "license" for more information.
>>> from ichrome.debugger import *
>>> tab = get_a_tab()
>>> tab.set_url('http://bing.com')
{'id': 4, 'result': {'frameId': 'DAC309349D270F07505C3DAB71084292', 'loaderId': '181418C22DB39654507D042627C22698'}}
>>> tab.click('#scpl0')
>>> tab.set_url('https://github.com/ClericPy')
True
>>> tab.click('.pinned-item-list-item-content [href="/ClericPy/ichrome"]')
Tag(a)
>>> tab.js('document.getElementById("sb_form_q").value = "jordan"')
{'id': 16, 'result': {'result': {'type': 'string', 'value': 'jordan'}}}
>>> tab.click('#sb_form_go')
Tag(input)
>>> tab.wait_loading(2)
True
>>> tab.wait_loading(2)
False
>>> tab.js('document.body.innerHTML="Updated"')
{'type': 'string', 'value': 'Updated'}
>>> tab.history_back()
True
>>> tab.set_html('hello')
{'id': 17, 'result': {}}
>>> tab.set_html('hello world')
{'id': 21, 'result': {}}
>>> tab.set_ua('no UA')
INFO 2020-05-11 20:14:07 [ichrome] async_utils.py(790): [set_ua] <Tab(connected): 08F4AFF9B389B1D5880AF0C0988B6DD4> userAgent => no UA
{'id': 12, 'result': {}}
{'id': 22, 'result': {}}
>>> tab.set_url('http://httpbin.org/user-agent')
{'id': 14, 'result': {'frameId': '08F4AFF9B389B1D5880AF0C0988B6DD4', 'loaderId': '15761B915F7AC36DC4687C1EED28195B'}}
True
>>> tab.html
'<html><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">{\n "user-agent": "no UA"\n}\n</pre></body></html>'
```
Expand Down
1 change: 0 additions & 1 deletion examples_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def test_js():
test_set_ua()
test_mouse_keyboard()
test_js()
network_sniffer()
# explicitly stop daemon is not necessary, daemon launched by debugger will auto shutdown after this script quit
# if the daemon is not launched by this script (using an existing one), the auto-shutdown will not run.
# daemon.stop()
2 changes: 1 addition & 1 deletion ichrome/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .logs import logger
from .sync_utils import Chrome, Tab

__version__ = "2.3.0"
__version__ = "2.3.1"
__tips__ = "[github]: https://github.com/ClericPy/ichrome\n[cdp]: https://chromedevtools.github.io/devtools-protocol/\n[cmd args]: https://peter.sh/experiments/chromium-command-line-switches/"
__all__ = [
'Chrome', 'ChromeDaemon', 'Tab', 'Tag', 'AsyncChrome', 'AsyncTab', 'logger',
Expand Down
7 changes: 7 additions & 0 deletions ichrome/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def main():
help="--remote-debugging-port, default to 9222",
default=argparse.SUPPRESS,
type=int)
parser.add_argument("--log-level",
"--log_level",
help="logger level, will be overwrited by --debug",
default=argparse.SUPPRESS)
parser.add_argument(
"--headless",
help="--headless and --hide-scrollbars, default to False",
Expand Down Expand Up @@ -204,6 +208,9 @@ def main():
proc_check_interval=args.proc_check_interval,
debug=args.debug,
)
log_level = getattr(args, 'log_level', None)
if log_level:
logger.setLevel(log_level)
if args.start_url == 'about:blank' or not args.start_url:
for config in kwargs['extra_config']:
if re.match('^https?://', config):
Expand Down
3 changes: 2 additions & 1 deletion ichrome/async_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ class Tab(GetValueMixin):
# aiohttp ws timeout default to 10.0, here is 5
_DEFAULT_CONNECT_TIMEOUT = 5.0
_RECV_DAEMON_BREAK_CALLBACK = None
_DEFAULT_WS_KWARGS: Dict = {}

def __init__(self,
tab_id: str = None,
Expand Down Expand Up @@ -266,7 +267,7 @@ def __init__(self,
self.chrome = chrome
self.timeout = self._DEFAULT_RECV_TIMEOUT if timeout is NotSet else timeout
self._created_time = self.now
self.ws_kwargs = ws_kwargs or {}
self.ws_kwargs = ws_kwargs or self._DEFAULT_WS_KWARGS
self.ws_kwargs.setdefault('timeout', self._DEFAULT_CONNECT_TIMEOUT)
self._closed = False
self._message_id = 0
Expand Down
42 changes: 21 additions & 21 deletions ichrome/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def _wrap_user_data_dir(self, user_data_dir):
self.ensure_dir(self.user_data_dir)
port_dir_size = get_readable_dir_size(port_user_dir)
total_dir_size = get_readable_dir_size(main_user_dir)
logger.info(
logger.debug(
f'user_data_dir({self.user_data_dir}) size: {port_dir_size} / {total_dir_size}'
)

Expand All @@ -209,19 +209,19 @@ def clear_user_dir(cls, user_data_dir, port=None):
main_user_dir = cls._ensure_user_dir(user_data_dir)
if port:
port_user_dir = main_user_dir / f"chrome_{port}"
logger.info(
logger.debug(
f'Clearing only port dir: {port_user_dir} => {get_readable_dir_size(port_user_dir)} / {get_readable_dir_size(main_user_dir)}'
)
cls.clear_dir_with_shutil(port_user_dir)
logger.info(
logger.debug(
f'Cleared only port dir: {port_user_dir} => {get_readable_dir_size(port_user_dir)} / {get_readable_dir_size(main_user_dir)}'
)
else:
logger.info(
logger.debug(
f'Clearing total user dir: {main_user_dir} => {get_readable_dir_size(main_user_dir)} / {get_readable_dir_size(main_user_dir)}'
)
cls.clear_dir_with_shutil(main_user_dir)
logger.info(
logger.debug(
f'Cleared total user dir: {main_user_dir} => {get_readable_dir_size(main_user_dir)} / {get_readable_dir_size(main_user_dir)}'
)

Expand All @@ -241,14 +241,14 @@ def clear_dir(cls, dir_path):
logger.warning(f'{dir_path} not exists, ignore.')
return
if not dir_path.is_dir():
logger.info(f'{dir_path} is not exist:.')
logger.debug(f'{dir_path} is not exist:.')
return True
for f in dir_path.iterdir():
if f.is_dir():
cls.clear_dir(f)
else:
f.unlink()
logger.info(f'File removed: {f}')
logger.debug(f'File removed: {f}')
dir_path.rmdir()

@property
Expand Down Expand Up @@ -317,7 +317,7 @@ def launch_chrome(self):

def check_chrome_ready(self):
if self.ok:
logger.info(
logger.debug(
f"launch_chrome success: {self}, args: {self.proc.args}")
return True
else:
Expand Down Expand Up @@ -354,7 +354,7 @@ def _ensure_port_free(self, max_tries=3):
self._timeout)
if ok:
return True
logger.info(f"shutting down chrome using port {self.port}")
logger.debug(f"shutting down chrome using port {self.port}")
self.kill(True)
else:
raise ValueError("port in used")
Expand Down Expand Up @@ -407,12 +407,12 @@ def _daemon(self, interval=None):
deaths = 0
while self._use_daemon:
if self._shutdown:
logger.info(
logger.debug(
f"{self} daemon break after shutdown({ttime(self._shutdown)})."
)
break
if deaths >= self.max_deaths:
logger.info(
logger.debug(
f"{self} daemon break for deaths is more than {self.max_deaths} times."
)
break
Expand All @@ -426,7 +426,7 @@ def _daemon(self, interval=None):
deaths += 1
except subprocess.TimeoutExpired:
deaths = 0
logger.info(f"{self} daemon exited. return_code: {return_code}")
logger.debug(f"{self} daemon exited. return_code: {return_code}")
self.update_shutdown_time()
return return_code

Expand Down Expand Up @@ -467,7 +467,7 @@ def kill(self, force=False):
self.port_in_using.discard(self.port)

def restart(self):
logger.info(f"restarting {self}")
logger.debug(f"restarting {self}")
self.kill()
return self.launch_chrome()

Expand All @@ -478,10 +478,10 @@ def update_shutdown_time(self):

def shutdown(self, reason=None):
if self._shutdown:
logger.info(f"{self} shutdown at {ttime(self._shutdown)} yet.")
logger.debug(f"{self} shutdown at {ttime(self._shutdown)} yet.")
return
reason = f' for {reason}' if reason else ''
logger.info(
logger.debug(
f"{self} shutting down{reason}, start-up: {ttime(self.start_time)}, duration: {timepass(time.time() - self.start_time, accuracy=3, format=1)}."
)
self.update_shutdown_time()
Expand Down Expand Up @@ -593,7 +593,7 @@ def _start_chrome_process(self):
self.proc = subprocess.Popen(**self.cmd_args)

async def restart(self):
logger.info(f"restarting {self}")
logger.debug(f"restarting {self}")
await self.loop.run_in_executor(None, super().kill)
return await self.launch_chrome()

Expand Down Expand Up @@ -638,7 +638,7 @@ async def get_free_port(cls,

async def check_chrome_ready(self):
if self.proc_ok and await self.check_connection():
logger.info(
logger.debug(
f"launch_chrome success: {self}, args: {self.proc.args}")
return True
else:
Expand Down Expand Up @@ -673,12 +673,12 @@ async def _daemon(self, interval=None):
deaths = 0
while self._use_daemon:
if self._shutdown:
logger.info(
logger.debug(
f"{self} daemon break after shutdown({ttime(self._shutdown)})."
)
break
if deaths >= self.max_deaths:
logger.info(
logger.debug(
f"{self} daemon break for deaths is more than {self.max_deaths} times."
)
break
Expand All @@ -693,7 +693,7 @@ async def _daemon(self, interval=None):
deaths += 1
except subprocess.TimeoutExpired:
deaths = 0
logger.info(f"{self} daemon exited.")
logger.debug(f"{self} daemon exited.")
self.update_shutdown_time()
return return_code

Expand All @@ -718,7 +718,7 @@ async def __aenter__(self):

async def create_chrome_workers(self):
for port in range(self.start_port, self.start_port + self.workers):
logger.info("ChromeDaemon cmd args: port=%s, %s" %
logger.debug("ChromeDaemon cmd args: port=%s, %s" %
(port, self.kwargs))
self.daemons.append(await
AsyncChromeDaemon(port=port,
Expand Down

0 comments on commit 6c2fe6d

Please sign in to comment.