anki/tools/reload_webviews.py
Aristotelis 2270ff425a
Add dev tools for live-reloading Anki's web views (#2151)
* Add dev tools for live-reloading the web stack while running Anki

* Handle CDP connection errors more graciously

* Include sass in web stack watchers

* Refactor monitored folder and event definition

* Switch to more specific build target

Thanks to @hikaru-y

* Add PyChromeDevTools to dev requirements

* Update rebuild-web for ninja

* Satisfy mypy

* Remove ts-watch

Superseded by web-watch (the version here was also still based around bazel)

* Simplify calls to other build tools

Given that `./ninja qt/aqt` has to be run from the project root anyways, it doesn't make sense to use calls relative to `rebuild-web` in an ill-guided effort to lower dependencies on hard-coded paths.

* Remove remaining script-relative tool path
2023-01-03 11:55:58 +10:00

56 lines
1.4 KiB
Python
Executable File

#!/usr/bin/env python
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
"""
Trigger a reload of Anki's web views using QtWebEngine' Chromium
Remote Debugging interface.
"""
import argparse
import sys
import PyChromeDevTools # type: ignore[import]
DEFAULT_HOST = "localhost"
DEFAULT_PORT = 8080
def print_error(message: str):
print(f"Error: {message}", file=sys.stderr)
parser = argparse.ArgumentParser("reload_webviews")
parser.add_argument(
"--host",
help=f"Host via which the Chrome session can be reached, e.g. {DEFAULT_HOST}",
type=str,
default=DEFAULT_HOST,
required=False,
)
parser.add_argument(
"--port",
help=f"Port via which the Chrome session can be reached, e.g. {DEFAULT_PORT}",
type=str,
default=DEFAULT_PORT,
required=False,
)
args = parser.parse_args()
try:
chrome = PyChromeDevTools.ChromeInterface(host=args.host, port=args.port)
except Exception as e:
print_error(
f"Could not establish connection to Chromium remote debugger. Exception:\n{e}"
)
exit(1)
if chrome.tabs is None:
print_error("Was unable to get active web views.")
exit(1)
for tab_index, tab_data in enumerate(chrome.tabs):
print(f"Reloading page: {tab_data['title']}")
chrome.connect(tab=tab_index, update_tabs=False)
chrome.Page.reload()