Add YAPF style + ignore, and format tracked Python files
This commit is contained in:
@@ -18,9 +18,13 @@ except Exception as exc: # pragma: no cover
|
||||
PlaywrightTimeoutError = TimeoutError # type: ignore
|
||||
sync_playwright = None # type: ignore
|
||||
|
||||
|
||||
# Re-export for consumers (e.g. cmdlets catching navigation timeouts)
|
||||
__all__ = ["HAS_PLAYWRIGHT", "PlaywrightTimeoutError", "PlaywrightTool", "PlaywrightDefaults"]
|
||||
__all__ = [
|
||||
"HAS_PLAYWRIGHT",
|
||||
"PlaywrightTimeoutError",
|
||||
"PlaywrightTool",
|
||||
"PlaywrightDefaults"
|
||||
]
|
||||
|
||||
|
||||
def _get_nested(config: Dict[str, Any], *path: str) -> Any:
|
||||
@@ -66,7 +70,8 @@ class PlaywrightTool:
|
||||
"""
|
||||
|
||||
def __init__(self, config: Optional[Dict[str, Any]] = None) -> None:
|
||||
self._config: Dict[str, Any] = dict(config or {})
|
||||
self._config: Dict[str,
|
||||
Any] = dict(config or {})
|
||||
self.defaults = self._load_defaults()
|
||||
|
||||
def _load_defaults(self) -> PlaywrightDefaults:
|
||||
@@ -75,7 +80,8 @@ class PlaywrightTool:
|
||||
tool_block = _get_nested(cfg, "tool", "playwright")
|
||||
if not isinstance(tool_block, dict):
|
||||
tool_block = {}
|
||||
pw_block = cfg.get("playwright") if isinstance(cfg.get("playwright"), dict) else {}
|
||||
pw_block = cfg.get("playwright") if isinstance(cfg.get("playwright"),
|
||||
dict) else {}
|
||||
if not isinstance(pw_block, dict):
|
||||
pw_block = {}
|
||||
|
||||
@@ -90,7 +96,9 @@ class PlaywrightTool:
|
||||
return fallback if val is None else val
|
||||
|
||||
browser = str(_get("browser", defaults.browser)).strip().lower() or "chromium"
|
||||
if browser not in {"chromium", "firefox", "webkit"}:
|
||||
if browser not in {"chromium",
|
||||
"firefox",
|
||||
"webkit"}:
|
||||
browser = "chromium"
|
||||
|
||||
headless_raw = _get("headless", defaults.headless)
|
||||
@@ -145,12 +153,15 @@ class PlaywrightTool:
|
||||
|
||||
h = self.defaults.headless if headless is None else bool(headless)
|
||||
ua = self.defaults.user_agent if user_agent is None else str(user_agent)
|
||||
vw = self.defaults.viewport_width if viewport_width is None else int(viewport_width)
|
||||
vh = self.defaults.viewport_height if viewport_height is None else int(viewport_height)
|
||||
vw = self.defaults.viewport_width if viewport_width is None else int(
|
||||
viewport_width
|
||||
)
|
||||
vh = self.defaults.viewport_height if viewport_height is None else int(
|
||||
viewport_height
|
||||
)
|
||||
ihe = (
|
||||
self.defaults.ignore_https_errors
|
||||
if ignore_https_errors is None
|
||||
else bool(ignore_https_errors)
|
||||
if ignore_https_errors is None else bool(ignore_https_errors)
|
||||
)
|
||||
|
||||
# Support Playwright-native headers/user-agent.
|
||||
@@ -158,7 +169,9 @@ class PlaywrightTool:
|
||||
# we omit the user_agent override so Playwright uses its bundled Chromium UA.
|
||||
ua_value: Optional[str]
|
||||
ua_text = str(ua or "").strip()
|
||||
if not ua_text or ua_text.lower() in {"native", "playwright", "default"}:
|
||||
if not ua_text or ua_text.lower() in {"native",
|
||||
"playwright",
|
||||
"default"}:
|
||||
ua_value = None
|
||||
else:
|
||||
ua_value = ua_text
|
||||
@@ -178,10 +191,14 @@ class PlaywrightTool:
|
||||
headless=h,
|
||||
args=["--disable-blink-features=AutomationControlled"],
|
||||
)
|
||||
context_kwargs: Dict[str, Any] = {
|
||||
"viewport": {"width": vw, "height": vh},
|
||||
"ignore_https_errors": ihe,
|
||||
}
|
||||
context_kwargs: Dict[str,
|
||||
Any] = {
|
||||
"viewport": {
|
||||
"width": vw,
|
||||
"height": vh
|
||||
},
|
||||
"ignore_https_errors": ihe,
|
||||
}
|
||||
if ua_value is not None:
|
||||
context_kwargs["user_agent"] = ua_value
|
||||
|
||||
@@ -209,7 +226,9 @@ class PlaywrightTool:
|
||||
"""Navigate with configured timeout."""
|
||||
try:
|
||||
page.goto(
|
||||
url, timeout=int(self.defaults.navigation_timeout_ms), wait_until="domcontentloaded"
|
||||
url,
|
||||
timeout=int(self.defaults.navigation_timeout_ms),
|
||||
wait_until="domcontentloaded"
|
||||
)
|
||||
except Exception:
|
||||
raise
|
||||
|
||||
@@ -67,9 +67,14 @@ class YtDlpTool:
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self, config: Optional[Dict[str, Any]] = None, *, script_dir: Optional[Path] = None
|
||||
self,
|
||||
config: Optional[Dict[str,
|
||||
Any]] = None,
|
||||
*,
|
||||
script_dir: Optional[Path] = None
|
||||
) -> None:
|
||||
self._config: Dict[str, Any] = dict(config or {})
|
||||
self._config: Dict[str,
|
||||
Any] = dict(config or {})
|
||||
# `resolve_cookies_path` expects the app root so it can fall back to ./cookies.txt.
|
||||
# This file lives under ./tool/, so default to the parent directory.
|
||||
self._script_dir = script_dir or Path(__file__).resolve().parent.parent
|
||||
@@ -79,7 +84,7 @@ class YtDlpTool:
|
||||
def _init_cookiefile(self) -> Optional[Path]:
|
||||
"""Resolve cookies once at tool init (yt-dlp is the primary consumer)."""
|
||||
try:
|
||||
from config import resolve_cookies_path
|
||||
from SYS.config import resolve_cookies_path
|
||||
|
||||
resolved = resolve_cookies_path(self._config, script_dir=self._script_dir)
|
||||
if resolved is not None and resolved.is_file():
|
||||
@@ -100,24 +105,20 @@ class YtDlpTool:
|
||||
if not isinstance(tool_block, dict):
|
||||
tool_block = {}
|
||||
|
||||
ytdlp_block = cfg.get("ytdlp") if isinstance(cfg.get("ytdlp"), dict) else {}
|
||||
ytdlp_block = cfg.get("ytdlp") if isinstance(cfg.get("ytdlp"),
|
||||
dict) else {}
|
||||
if not isinstance(ytdlp_block, dict):
|
||||
ytdlp_block = {}
|
||||
|
||||
# Accept both nested and flat styles.
|
||||
video_format = (
|
||||
tool_block.get("video_format")
|
||||
or tool_block.get("format")
|
||||
or ytdlp_block.get("video_format")
|
||||
or ytdlp_block.get("video")
|
||||
or ytdlp_block.get("format_video")
|
||||
or cfg.get("ytdlp_video_format")
|
||||
tool_block.get("video_format") or tool_block.get("format")
|
||||
or ytdlp_block.get("video_format") or ytdlp_block.get("video")
|
||||
or ytdlp_block.get("format_video") or cfg.get("ytdlp_video_format")
|
||||
)
|
||||
audio_format = (
|
||||
tool_block.get("audio_format")
|
||||
or ytdlp_block.get("audio_format")
|
||||
or ytdlp_block.get("audio")
|
||||
or ytdlp_block.get("format_audio")
|
||||
tool_block.get("audio_format") or ytdlp_block.get("audio_format")
|
||||
or ytdlp_block.get("audio") or ytdlp_block.get("format_audio")
|
||||
or cfg.get("ytdlp_audio_format")
|
||||
)
|
||||
|
||||
@@ -126,17 +127,22 @@ class YtDlpTool:
|
||||
nested_audio = _get_nested(cfg, "ytdlp", "format", "audio")
|
||||
|
||||
fmt_sort_val = (
|
||||
tool_block.get("format_sort")
|
||||
or ytdlp_block.get("format_sort")
|
||||
or ytdlp_block.get("formatSort")
|
||||
or cfg.get("ytdlp_format_sort")
|
||||
or _get_nested(cfg, "ytdlp", "format", "sort")
|
||||
tool_block.get("format_sort") or ytdlp_block.get("format_sort")
|
||||
or ytdlp_block.get("formatSort") or cfg.get("ytdlp_format_sort")
|
||||
or _get_nested(cfg,
|
||||
"ytdlp",
|
||||
"format",
|
||||
"sort")
|
||||
)
|
||||
fmt_sort = _parse_csv_list(fmt_sort_val)
|
||||
|
||||
defaults = YtDlpDefaults(
|
||||
video_format=str(nested_video or video_format or _fallback_defaults.video_format),
|
||||
audio_format=str(nested_audio or audio_format or _fallback_defaults.audio_format),
|
||||
video_format=str(
|
||||
nested_video or video_format or _fallback_defaults.video_format
|
||||
),
|
||||
audio_format=str(
|
||||
nested_audio or audio_format or _fallback_defaults.audio_format
|
||||
),
|
||||
format_sort=fmt_sort,
|
||||
)
|
||||
|
||||
@@ -155,17 +161,18 @@ class YtDlpTool:
|
||||
"""Translate DownloadOptions into yt-dlp API options."""
|
||||
ensure_directory(opts.output_dir)
|
||||
outtmpl = str((opts.output_dir / "%(title)s.%(ext)s").resolve())
|
||||
base_options: Dict[str, Any] = {
|
||||
"outtmpl": outtmpl,
|
||||
"quiet": True,
|
||||
"no_warnings": True,
|
||||
"noprogress": True,
|
||||
"socket_timeout": 30,
|
||||
"retries": 10,
|
||||
"fragment_retries": 10,
|
||||
"http_chunk_size": 10_485_760,
|
||||
"restrictfilenames": True,
|
||||
}
|
||||
base_options: Dict[str,
|
||||
Any] = {
|
||||
"outtmpl": outtmpl,
|
||||
"quiet": True,
|
||||
"no_warnings": True,
|
||||
"noprogress": True,
|
||||
"socket_timeout": 30,
|
||||
"retries": 10,
|
||||
"fragment_retries": 10,
|
||||
"http_chunk_size": 10_485_760,
|
||||
"restrictfilenames": True,
|
||||
}
|
||||
|
||||
try:
|
||||
repo_root = Path(__file__).resolve().parents[1]
|
||||
@@ -195,7 +202,9 @@ class YtDlpTool:
|
||||
base_options["format"] = fmt
|
||||
|
||||
if opts.mode == "audio":
|
||||
base_options["postprocessors"] = [{"key": "FFmpegExtractAudio"}]
|
||||
base_options["postprocessors"] = [{
|
||||
"key": "FFmpegExtractAudio"
|
||||
}]
|
||||
else:
|
||||
format_sort = self.defaults.format_sort or [
|
||||
"res:4320",
|
||||
@@ -213,7 +222,9 @@ class YtDlpTool:
|
||||
if not isinstance(pps, list):
|
||||
pps = []
|
||||
already_has_metadata = any(
|
||||
isinstance(pp, dict) and str(pp.get("key") or "") == "FFmpegMetadata" for pp in pps
|
||||
isinstance(pp,
|
||||
dict) and str(pp.get("key") or "") == "FFmpegMetadata"
|
||||
for pp in pps
|
||||
)
|
||||
if not already_has_metadata:
|
||||
pps.append(
|
||||
|
||||
Reference in New Issue
Block a user