29 lines
739 B
Python
29 lines
739 B
Python
import glob
|
|
import re
|
|
from fnmatch import fnmatchcase, translate
|
|
|
|
GLOB_MAX_LEN = 200
|
|
CONTROL_RE = re.compile(r"[\x00-\x1f\x7f]")
|
|
|
|
|
|
def glob_matches(pattern: str, value: str) -> bool:
|
|
if not pattern:
|
|
return True
|
|
return fnmatchcase(value.lower(), pattern.lower())
|
|
|
|
|
|
def validate_glob(pattern: str, label: str) -> list[str]:
|
|
if len(pattern) > GLOB_MAX_LEN:
|
|
return [f"{label} ist zu lang (max. {GLOB_MAX_LEN} Zeichen)."]
|
|
if CONTROL_RE.search(pattern):
|
|
return [f"{label} enthält Steuerzeichen."]
|
|
try:
|
|
re.compile(translate(pattern))
|
|
except re.error:
|
|
return [f"{label} ist kein gültiges Muster."]
|
|
return []
|
|
|
|
|
|
def literal_glob(value: str) -> str:
|
|
return glob.escape(value)
|