Skip to content

Commit

Permalink
FIX: Implement inheritance for all TSV/JSON files
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Feb 21, 2024
1 parent 51f1d1f commit 7d8844e
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions tools/schemacode/bidsschematools/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def _entity_rule(rule: Mapping, schema: bst.types.Namespace):
}


def _split_inheritance_rules(rule: Mapping) -> ty.List[Mapping]:
def _split_inheritance_rules(rule: dict) -> ty.List[dict]:
"""Break composite rules into main and sidecar rules
Implements the inheritance principle for file naming.
Expand All @@ -139,32 +139,28 @@ def _split_inheritance_rules(rule: Mapping) -> ty.List[Mapping]:
rule_exts = set(rule["extensions"])

main_exts = rule_exts - heritable_exts
# If a rule only has TSV or JSON files, entities can be
# made required
if not main_exts:
if ".tsv" in rule_exts:
main_exts = {".tsv"}
elif ".json" in rule_exts:
main_exts = {".json"}

sidecar_exts = rule_exts - main_exts
if not sidecar_exts:
return [rule]

sidecar_dtypes = [""] + rule.get("datatypes", [])
sidecar_entities = {ent: "optional" for ent in rule["entities"]}

main_rule = {**rule, **{"extensions": list(main_exts)}}
sidecar_rule = {
**rule,
**{
"extensions": list(sidecar_exts),
"datatypes": sidecar_dtypes,
"entities": sidecar_entities,
},
}
rules = []

# Some rules only address metadata, such as events.tsv or coordsystem.json
if main_exts:
rules.append({**rule, **{"extensions": list(main_exts)}})

rules.append(
{
**rule,
**{
"extensions": list(sidecar_exts),
"datatypes": [""] + rule.get("datatypes", []),
"entities": {ent: "optional" for ent in rule["entities"]},
},
}
)

return [main_rule, sidecar_rule]
return rules


def _sanitize_extension(ext: str) -> str:
Expand Down

0 comments on commit 7d8844e

Please sign in to comment.