From 434bf3b1f33b02b4ab8aa876fce6c57d03c66f70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Mon, 23 Jun 2025 20:27:40 +0200 Subject: [PATCH] use 'util.re()' in more places continuation of b5c88b3d3ed505aefecf51b5908d6c3503c457dc --- gallery_dl/__init__.py | 3 +-- gallery_dl/actions.py | 7 +++---- gallery_dl/path.py | 4 +--- gallery_dl/update.py | 3 +-- gallery_dl/ytdl.py | 10 ++++------ 5 files changed, 10 insertions(+), 17 deletions(-) diff --git a/gallery_dl/__init__.py b/gallery_dl/__init__.py index de8c5936..b19507f8 100644 --- a/gallery_dl/__init__.py +++ b/gallery_dl/__init__.py @@ -492,8 +492,7 @@ class InputManager(): # url if " #" in line or "\t#" in line: if strip_comment is None: - import re - strip_comment = re.compile(r"\s+#.*").sub + strip_comment = util.re(r"\s+#.*").sub line = strip_comment("", line) if gconf or lconf: url = ExtendedUrl(line, gconf, lconf) diff --git a/gallery_dl/actions.py b/gallery_dl/actions.py index 668032d5..d5a3486f 100644 --- a/gallery_dl/actions.py +++ b/gallery_dl/actions.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2023 Mike Fährmann +# Copyright 2023-2025 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as @@ -8,7 +8,6 @@ """ """ -import re import time import logging import operator @@ -32,7 +31,7 @@ def parse(actionspec): for event, spec in actionspec: level, _, pattern = event.partition(":") - search = re.compile(pattern).search if pattern else util.true + search = util.re(pattern).search if pattern else util.true if isinstance(spec, str): type, _, args = spec.partition(" ") @@ -138,7 +137,7 @@ def action_print(opts): def action_status(opts): - op, value = re.match(r"\s*([&|^=])=?\s*(\d+)", opts).groups() + op, value = util.re(r"\s*([&|^=])=?\s*(\d+)").match(opts).groups() op = { "&": operator.and_, diff --git a/gallery_dl/path.py b/gallery_dl/path.py index 0fdaeabb..ef1dba98 100644 --- a/gallery_dl/path.py +++ b/gallery_dl/path.py @@ -9,7 +9,6 @@ """Filesystem path handling""" import os -import re import shutil import functools from . import util, formatter, exception @@ -148,8 +147,7 @@ class PathFormat(): def func(x, c=chars, r=repl): return x.replace(c, r) else: - return functools.partial( - re.compile("[" + chars + "]").sub, repl) + return functools.partial(util.re(f"[{chars}]").sub, repl) return func def _process_repl_dict(self, chars): diff --git a/gallery_dl/update.py b/gallery_dl/update.py index 936a5088..4cc607fe 100644 --- a/gallery_dl/update.py +++ b/gallery_dl/update.py @@ -7,7 +7,6 @@ # published by the Free Software Foundation. import os -import re import sys from .extractor.common import Extractor, Message @@ -184,7 +183,7 @@ class UpdateExtractor(Extractor): tag = channel exact = True - if re.match(r"\d\.\d+\.\d+", tag): + if util.re_compile(r"\d\.\d+\.\d+").match(tag): tag = "v" + tag try: diff --git a/gallery_dl/ytdl.py b/gallery_dl/ytdl.py index 9525d989..8cd6501a 100644 --- a/gallery_dl/ytdl.py +++ b/gallery_dl/ytdl.py @@ -8,7 +8,6 @@ """Helpers for interacting with youtube-dl""" -import re import shlex import itertools from . import text, util, exception @@ -258,13 +257,12 @@ def parse_command_line(module, argv): cookiesfrombrowser = getattr(opts, "cookiesfrombrowser", None) if cookiesfrombrowser: - match = re.fullmatch(r"""(?x) + pattern = util.re(r"""(?x) (?P[^+:]+) (?:\s*\+\s*(?P[^:]+))? (?:\s*:\s*(?!:)(?P.+?))? - (?:\s*::\s*(?P.+))? - """, cookiesfrombrowser) - if match: + (?:\s*::\s*(?P.+))?""") + if match := pattern.fullmatch(cookiesfrombrowser): browser, keyring, profile, container = match.groups() if keyring is not None: keyring = keyring.upper() @@ -524,7 +522,7 @@ def legacy_postprocessors(opts, module, ytdlp, compat_opts): if len(dur) == 2 and all(t is not None for t in dur): remove_ranges.append(tuple(dur)) continue - remove_chapters_patterns.append(re.compile(regex)) + remove_chapters_patterns.append(util.re(regex)) if opts.remove_chapters or sponsorblock_query: postprocessors.append({ "key": "ModifyChapters",