Skip to content

Commit

Permalink
ENH: Update stem rule to accept datatypes, glob stems
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Apr 26, 2024
1 parent 863b5ad commit 3c2b821
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions tools/schemacode/bidsschematools/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
``schema.rules.files``.
"""

import fnmatch
import re
import typing as ty
from collections.abc import Mapping
Expand Down Expand Up @@ -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<datatype>{'|'.join(dtypes)})/" if dtypes else ""

ext_match = "|".join(_sanitize_extension(ext) for ext in rule.extensions)
ext_regex = f"(?P<extension>{ext_match})"
ext_regex = rf"(?P<extension>{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):
Expand Down

0 comments on commit 3c2b821

Please sign in to comment.