diff --git a/tools/schemacode/bidsschematools/rules.py b/tools/schemacode/bidsschematools/rules.py index 22e1fce649..a27b93c785 100644 --- a/tools/schemacode/bidsschematools/rules.py +++ b/tools/schemacode/bidsschematools/rules.py @@ -4,6 +4,7 @@ ``schema.rules.files``. """ +import fnmatch import re import typing as ty from collections.abc import Mapping @@ -170,11 +171,16 @@ def _sanitize_extension(ext: str) -> str: def _stem_rule(rule: bst.types.Namespace): - stem_regex = re.escape(rule.stem) + # translate includes a trailing \Z (end of string) but we expect extensions + stem_regex = fnmatch.translate(rule.stem)[:-2] + + dtypes = set(rule.get("datatypes", ())) + dir_regex = f"(?P{'|'.join(dtypes)})/" if dtypes else "" + ext_match = "|".join(_sanitize_extension(ext) for ext in rule.extensions) - ext_regex = f"(?P{ext_match})" + ext_regex = rf"(?P{ext_match})\Z" - return {"regex": stem_regex + ext_regex, "mandatory": rule.level == "required"} + return {"regex": dir_regex + stem_regex + ext_regex, "mandatory": rule.level == "required"} def _path_rule(rule: bst.types.Namespace):