Compare commits

...
2 changed files with 107 additions and 3 deletions

No files matched your search

+45 -3
View File
@@ -3,7 +3,6 @@ from __future__ import annotations
import logging
import re
from fnmatch import fnmatch
from fnmatch import translate as fnmatch_translate
from typing import TYPE_CHECKING
from rest_framework import serializers
@@ -548,6 +547,48 @@ def existing_document_matches_workflow(
return (True, None)
def _glob_to_portable_regex(pattern: str) -> str:
"""
Translate an fnmatch-style glob into an anchored regex which is accepted by
Python, PostgreSQL (ARE), and MariaDB (PCRE).
fnmatch.translate() can't be used, as its output (`(?s:...)\\Z`) uses syntax
PostgreSQL rejects. Supports `*`, `?`, `[seq]` and `[!seq]`.
"""
out: list[str] = []
i = 0
n = len(pattern)
while i < n:
c = pattern[i]
i += 1
if c == "*":
out.append(".*")
elif c == "?":
out.append(".")
elif c == "[":
j = i
if j < n and pattern[j] == "!":
j += 1
if j < n and pattern[j] == "]":
j += 1
while j < n and pattern[j] != "]":
j += 1
if j >= n:
# No closing bracket, treat as a literal
out.append(r"\[")
else:
body = pattern[i:j]
i = j + 1
negate = body.startswith("!")
if negate:
body = body[1:]
body = "".join(f"\\{ch}" if ch in "\\[^" else ch for ch in body)
out.append(f"[{'^' if negate else ''}{body}]")
else:
out.append(re.escape(c))
return f"^{''.join(out)}$"
def prefilter_documents_by_workflowtrigger(
documents: QuerySet[Document],
trigger: WorkflowTrigger,
@@ -628,8 +669,9 @@ def prefilter_documents_by_workflowtrigger(
documents = documents.annotate(**annotations).filter(custom_field_q)
if trigger.filter_filename:
regex = fnmatch_translate(trigger.filter_filename).lstrip("^").rstrip("$")
documents = documents.filter(original_filename__iregex=regex)
documents = documents.filter(
original_filename__iregex=_glob_to_portable_regex(trigger.filter_filename),
)
return documents
@@ -0,0 +1,62 @@
import fnmatch
import re
import pytest
from documents.matching import _glob_to_portable_regex
@pytest.mark.parametrize(
("pattern", "expected"),
[
pytest.param("", r"^$", id="empty"),
pytest.param("abc", r"^abc$", id="literal"),
pytest.param("*", r"^.*$", id="star"),
pytest.param("?", r"^.$", id="question"),
pytest.param("*.pdf", r"^.*\.pdf$", id="star-escapes-dot"),
pytest.param("[abc]", r"^[abc]$", id="class"),
pytest.param("[a-z]", r"^[a-z]$", id="class-range"),
pytest.param("[!abc]", r"^[^abc]$", id="negated-class"),
pytest.param("[]a]", r"^[]a]$", id="leading-close-bracket-is-member"),
pytest.param("[!]a]", r"^[^]a]$", id="negated-leading-close-bracket"),
pytest.param("[a^b]", r"^[a\^b]$", id="caret-escaped-in-class"),
pytest.param("[a[b]", r"^[a\[b]$", id="open-bracket-escaped-in-class"),
pytest.param("[a\\b]", r"^[a\\b]$", id="backslash-escaped-in-class"),
pytest.param("[abc", r"^\[abc$", id="unclosed-class-literal"),
pytest.param("[", r"^\[$", id="lone-open-bracket"),
pytest.param("[!", r"^\[!$", id="unclosed-negation-literal"),
pytest.param("[]", r"^\[\]$", id="unclosed-with-only-close-bracket"),
pytest.param("a[0-9]?*", r"^a[0-9]..*$", id="mixed"),
],
)
def test_translation(pattern: str, expected: str) -> None:
"""
GIVEN:
- A glob pattern
WHEN:
- It is translated to a portable regex
THEN:
- The exact anchored regex is produced
"""
assert _glob_to_portable_regex(pattern) == expected
@pytest.mark.parametrize(
"pattern",
["*", "?", "*.pdf", "a?c", "[abc]x", "[!abc]x", "[]a]", "[!]a]", "[a-c]*", "[a"],
)
@pytest.mark.parametrize(
"name",
["", "a", "abc", "x", "]", "]x", "file.pdf", "ax", "bx", "dx", "[a"],
)
def test_agrees_with_fnmatch(pattern: str, name: str) -> None:
"""
GIVEN:
- A glob pattern and a name
WHEN:
- Matched via the portable regex and via stdlib fnmatch
THEN:
- Both agree
"""
regex = _glob_to_portable_regex(pattern)
assert bool(re.search(regex, name)) == fnmatch.fnmatchcase(name, pattern)