Use unquoted path for filename and path matching

This commit is contained in:
Kovid Goyal 2020-09-18 21:11:39 +05:30
parent de6528b7d8
commit cc7cefd3ed
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -64,20 +64,20 @@ def parse(lines: Iterable[str]) -> Generator[OpenAction, None, None]:
yield OpenAction(tuple(match_criteria), tuple(actions)) yield OpenAction(tuple(match_criteria), tuple(actions))
def url_matches_criterion(purl: 'ParseResult', url: str, mc: MatchCriteria) -> bool: def url_matches_criterion(purl: 'ParseResult', url: str, unquoted_path: str, mc: MatchCriteria) -> bool:
if mc.type == 'url': if mc.type == 'url':
import re import re
try: try:
pat = re.compile(mc.value) pat = re.compile(mc.value)
except re.error: except re.error:
return False return False
return pat.search(url) is not None return pat.search(unquote(url)) is not None
if mc.type == 'mime': if mc.type == 'mime':
import fnmatch import fnmatch
from mimetypes import guess_type from mimetypes import guess_type
try: try:
mt = guess_type(purl.path)[0] mt = guess_type(unquoted_path)[0]
except Exception: except Exception:
return False return False
if mt is None: if mt is None:
@ -93,7 +93,7 @@ def url_matches_criterion(purl: 'ParseResult', url: str, mc: MatchCriteria) -> b
if mc.type == 'ext': if mc.type == 'ext':
if not purl.path: if not purl.path:
return False return False
path = purl.path.lower() path = unquoted_path.lower()
for ext in mc.value.split(','): for ext in mc.value.split(','):
ext = ext.strip() ext = ext.strip()
if path.endswith('.' + ext): if path.endswith('.' + ext):
@ -113,7 +113,7 @@ def url_matches_criterion(purl: 'ParseResult', url: str, mc: MatchCriteria) -> b
if mc.type == 'path': if mc.type == 'path':
import fnmatch import fnmatch
try: try:
return fnmatch.fnmatchcase(purl.path.lower(), mc.value) return fnmatch.fnmatchcase(unquoted_path.lower(), mc.value)
except Exception: except Exception:
return False return False
@ -121,7 +121,7 @@ def url_matches_criterion(purl: 'ParseResult', url: str, mc: MatchCriteria) -> b
import fnmatch import fnmatch
import posixpath import posixpath
try: try:
fname = posixpath.basename(purl.path) fname = posixpath.basename(unquoted_path)
except Exception: except Exception:
return False return False
try: try:
@ -130,10 +130,10 @@ def url_matches_criterion(purl: 'ParseResult', url: str, mc: MatchCriteria) -> b
return False return False
def url_matches_criteria(purl: 'ParseResult', url: str, criteria: Iterable[MatchCriteria]) -> bool: def url_matches_criteria(purl: 'ParseResult', url: str, unquoted_path: str, criteria: Iterable[MatchCriteria]) -> bool:
for x in criteria: for x in criteria:
try: try:
if not url_matches_criterion(purl, url, x): if not url_matches_criterion(purl, url, unquoted_path, x):
return False return False
except Exception: except Exception:
return False return False
@ -151,7 +151,7 @@ def actions_for_url_from_list(url: str, actions: Iterable[OpenAction]) -> Genera
'URL': url, 'URL': url,
'FILE_PATH': path, 'FILE_PATH': path,
'FILE': posixpath.basename(path), 'FILE': posixpath.basename(path),
'FRAGMENT': purl.fragment 'FRAGMENT': unquote(purl.fragment)
} }
def expand(x: Any) -> Any: def expand(x: Any) -> Any:
@ -160,7 +160,7 @@ def actions_for_url_from_list(url: str, actions: Iterable[OpenAction]) -> Genera
return x return x
for action in actions: for action in actions:
if url_matches_criteria(purl, url, action.match_criteria): if url_matches_criteria(purl, url, path, action.match_criteria):
for ac in action.actions: for ac in action.actions:
yield ac._replace(args=tuple(map(expand, ac.args))) yield ac._replace(args=tuple(map(expand, ac.args)))
return return