Browse Source

Regex for unwanted extensions (#1907)

* allow regexp for unwanted extensions

* housekeeping

* use rss.convert_filter

* improve function name

* move convert_filter to misc
pull/1920/head
jcfp 4 years ago
committed by GitHub
parent
commit
7dbde008af
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      sabnzbd/filesystem.py
  2. 17
      sabnzbd/misc.py
  3. 19
      sabnzbd/rss.py
  4. 14
      tests/test_filesystem.py

23
sabnzbd/filesystem.py

@ -59,6 +59,25 @@ def get_ext(filename: str) -> str:
return ""
def is_listed_ext(ext: str, ext_list: list) -> bool:
"""Check if the extension is listed. In case of a regexp the entire extension must be matched;
partial matches aren't accepted (e.g. 'r[0-9]{2}' will be treated the same as '^r[0-9]{2}$' and
thus return false for extentions such as 'r007' despite the substring match on 'r00').
"""
for item in ext_list:
RE_EXT = sabnzbd.misc.convert_filter(item)
if RE_EXT:
try:
if len(RE_EXT.match(ext).group()) == len(ext):
return True
except Exception:
pass
elif item == ext:
return True
# No match found
return False
def has_unwanted_extension(filename: str) -> bool:
"""Determine if a filename has an unwanted extension, given the configured mode"""
extension = get_ext(filename).replace(".", "")
@ -66,11 +85,11 @@ def has_unwanted_extension(filename: str) -> bool:
return (
# Blacklisted
sabnzbd.cfg.unwanted_extensions_mode() == 0
and extension in sabnzbd.cfg.unwanted_extensions()
and is_listed_ext(extension, sabnzbd.cfg.unwanted_extensions())
) or (
# Not whitelisted
sabnzbd.cfg.unwanted_extensions_mode() == 1
and extension not in sabnzbd.cfg.unwanted_extensions()
and not is_listed_ext(extension, sabnzbd.cfg.unwanted_extensions())
)
else:
# Don't consider missing extensions unwanted to prevent indiscriminate blocking of

17
sabnzbd/misc.py

@ -220,6 +220,23 @@ def wildcard_to_re(text):
return "".join([_wildcard_to_regex.get(ch, ch) for ch in text])
def convert_filter(text):
"""Return compiled regex.
If string starts with re: it's a real regex
else quote all regex specials, replace '*' by '.*'
"""
text = text.strip().lower()
if text.startswith("re:"):
txt = text[3:].strip()
else:
txt = wildcard_to_re(text)
try:
return re.compile(txt, re.I)
except:
logging.debug("Could not compile regex: %s", text)
return None
def cat_convert(cat):
"""Convert indexer's category/group-name to user categories.
If no match found, but indexer-cat equals user-cat, then return user-cat

19
sabnzbd/rss.py

@ -31,7 +31,7 @@ from sabnzbd.constants import RSS_FILE_NAME, DEFAULT_PRIORITY, DUP_PRIORITY
from sabnzbd.decorators import synchronized
import sabnzbd.config as config
import sabnzbd.cfg as cfg
from sabnzbd.misc import cat_convert, wildcard_to_re, cat_to_opts, match_str, from_units, int_conv, get_base_url
from sabnzbd.misc import cat_convert, convert_filter, cat_to_opts, match_str, from_units, int_conv, get_base_url
import sabnzbd.emailer as emailer
import feedparser
@ -47,23 +47,6 @@ def notdefault(item):
return bool(item) and str(item).lower() not in ("default", "*", "", str(DEFAULT_PRIORITY))
def convert_filter(text):
"""Return compiled regex.
If string starts with re: it's a real regex
else quote all regex specials, replace '*' by '.*'
"""
text = text.strip().lower()
if text.startswith("re:"):
txt = text[3:].strip()
else:
txt = wildcard_to_re(text)
try:
return re.compile(txt, re.I)
except:
logging.debug("Could not compile regex: %s", text)
return None
def remove_obsolete(jobs, new_jobs):
"""Expire G/B links that are not in new_jobs (mark them 'X')
Expired links older than 3 days are removed from 'jobs'

14
tests/test_filesystem.py

@ -1094,7 +1094,7 @@ class TestRenamer:
class TestUnwantedExtensions:
# Only test lowercase extensions without a leading dot: the unwanted_extensions
# setting is sanitized accordingly in interface.saveSwitches() before saving.
test_extensions = "iso, cmd, bat, sh"
test_extensions = "iso, cmd, bat, sh, re:r[0-9]{2}, sab*"
# Test parameters as (filename, result) tuples, with result given for blacklist mode
test_params = [
("ubuntu.iso", True),
@ -1109,7 +1109,19 @@ class TestUnwantedExtensions:
("par2.cmd.notcmd", False),
("freedos.tab", False),
(".SH.hs", False),
("regexp.r0611", False),
("regexp.007", False),
("regexp.A01", False),
("regexp.r9", False),
("regexp.r2d2", False),
("regexp.r2d", False),
("regexp.r00", True),
("regexp.R42", True),
("test.sabnzbd", True),
("pass.sab", True),
("fail.sb", False),
("No_Extension", False),
("r42", False),
(480, False),
(None, False),
("", False),

Loading…
Cancel
Save