diff options
author | Simon Leiner <simon@leiner.me> | 2023-03-14 18:23:40 +0100 |
---|---|---|
committer | Jordan Cook <jordan.cook.git@proton.me> | 2023-03-24 18:50:18 -0500 |
commit | 94331ef108ad160faddb48cfc6a259c8a2497c99 (patch) | |
tree | ff52bb7d9d7536e2ba139806b3532ed3401d56f0 /requests_cache/policy/expiration.py | |
parent | 812301ab85a3b54387dcebd7d65407ace2589b9f (diff) | |
download | requests-cache-94331ef108ad160faddb48cfc6a259c8a2497c99.tar.gz |
Allow regexes for URL expiration patterns
This allows for more fine-grained control over URL patterns than
globbing in the rare cases where that is needed.
Diffstat (limited to 'requests_cache/policy/expiration.py')
-rw-r--r-- | requests_cache/policy/expiration.py | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/requests_cache/policy/expiration.py b/requests_cache/policy/expiration.py index 1041e36..1350951 100644 --- a/requests_cache/policy/expiration.py +++ b/requests_cache/policy/expiration.py @@ -5,9 +5,10 @@ from fnmatch import fnmatch from logging import getLogger from math import ceil from typing import Optional +from typing import Pattern as RegexPattern from .._utils import try_int -from . import ExpirationPatterns, ExpirationTime +from . import ExpirationPattern, ExpirationPatterns, ExpirationTime # Special expiration values that may be set by either headers or keyword args DO_NOT_CACHE = 0x0D0E0200020704 # Per RFC 4824 @@ -89,7 +90,7 @@ def _to_utc(dt: datetime): return dt -def _url_match(url: str, pattern: str) -> bool: +def _url_match(url: str, pattern: ExpirationPattern) -> bool: """Determine if a URL matches a pattern Args: @@ -103,7 +104,15 @@ def _url_match(url: str, pattern: str) -> bool: True >>> url_match('https://httpbin.org/stream/2', 'httpbin.org/*/1') False + >>> url_match('https://httpbin.org/stream/2', re.compile('httpbin.org/*/\\d+')) + True + >>> url_match('https://httpbin.org/stream/x', re.compile('httpbin.org/*/\\d+')) + False """ - url = url.split('://')[-1] - pattern = pattern.split('://')[-1].rstrip('*') + '**' - return fnmatch(url, pattern) + if isinstance(pattern, RegexPattern): + match = pattern.search(url) + return match is not None + else: + url = url.split('://')[-1] + pattern = pattern.split('://')[-1].rstrip('*') + '**' + return fnmatch(url, pattern) |