Skip to content

Commit c1a8a60

Browse files
committed
fix+enh: add empty_dirs option, fix documentation
1 parent 2d6247c commit c1a8a60

File tree

2 files changed

+79
-24
lines changed

2 files changed

+79
-24
lines changed

tools/schemacode/src/bidsschematools/render/text.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ def make_filename_template(
233233
n_dupes_to_combine=6,
234234
pdf_format=False,
235235
placeholders=False,
236+
empty_dirs=None,
236237
**kwargs,
237238
):
238239
"""Create codeblocks containing example filename patterns for a given datatype.
@@ -263,10 +264,13 @@ def make_filename_template(
263264
placeholders : bool, optional
264265
If True, placeholder meta-entities will replace keyword-value entities in the
265266
filename.
266-
If `dstype` is `"raw"`, the placeholder meta-entity is `<matches>`.
267-
If `dstype` is `"derivatives"`, the placeholder meta-entity is `<source_entities>`.
267+
If ``dstype`` is ``"raw"``, the placeholder meta-entity is ``<matches>``.
268+
If ``dstype`` is ``"derivatives"``, the placeholder meta-entity is ``<source_entities>``.
268269
Default is False.
269-
source_entities : bool
270+
empty_dirs: bool, optional
271+
If False, empty datatype directories are not included. If ``placeholders`` is True,
272+
this option is set False.
273+
Default is True.
270274
271275
Other Parameters
272276
----------------
@@ -318,6 +322,20 @@ def make_filename_template(
318322
for datatype in rule.datatypes:
319323
file_groups.setdefault(datatype, []).append(rule)
320324

325+
if placeholders:
326+
empty_dirs = False if empty_dirs is None else empty_dirs
327+
metaentity_name = "matches" if dstype == "raw" else "source_entities"
328+
ent_string = (
329+
lt
330+
+ utils._link_with_html(
331+
metaentity_name,
332+
html_path=GLOSSARY_PATH + ".html",
333+
heading=f"{metaentity_name}-metaentities",
334+
pdf_format=pdf_format,
335+
)
336+
+ gt
337+
)
338+
321339
for datatype in sorted(file_groups):
322340
group_lines = []
323341
datatype_string = utils._link_with_html(
@@ -328,19 +346,6 @@ def make_filename_template(
328346
)
329347
group_lines.append(f"\t\t{datatype_string}/")
330348

331-
if placeholders:
332-
metaentity_name = "matches" if dstype == "raw" else "source_entities"
333-
ent_string = (
334-
lt
335-
+ utils._link_with_html(
336-
metaentity_name,
337-
html_path=GLOSSARY_PATH + ".html",
338-
heading=f"{metaentity_name}-metaentities",
339-
pdf_format=pdf_format,
340-
)
341-
+ gt
342-
)
343-
344349
# Unique filename patterns
345350
for group in file_groups[datatype]:
346351
if not placeholders:
@@ -439,8 +444,9 @@ def make_filename_template(
439444
for extension in sorted(extensions)
440445
)
441446

442-
if placeholders and len(group_lines) == 1:
443-
continue
447+
# If the datatype does not have any files, skip
448+
if empty_dirs is False and len(group_lines) == 1:
449+
continue
444450

445451
lines.extend(group_lines)
446452

tools/schemacode/src/bidsschematools/tests/test_render_text.py

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,71 @@ def test_make_filename_template(schema_obj, schema_dir, placeholders):
9898

9999
datatype_count = len(datatypes)
100100
datatype_bases = [f" {i}/" for i in datatypes]
101-
datatype_level = False
102101
datatype_file_start = (
103102
" sub-<label>" if not placeholders else " <matches>_"
104103
)
105104
datatype_bases_found = 0
105+
template_started = False
106106
for line in filename_template.split("\n"):
107-
if datatype_level:
108-
# Is there at least one file pattern per datatype?
109-
assert line.startswith(datatype_file_start)
110-
datatype_level = False
107+
if "```" in line:
108+
if template_started:
109+
break
110+
111+
template_started = True
112+
continue
113+
114+
if not template_started:
115+
continue
116+
117+
if line.startswith("sub-<label>"):
118+
continue
119+
if line.startswith(" [ses-<label>/]"):
120+
continue
121+
111122
if line in datatype_bases:
112-
datatype_level = True
113123
datatype_bases_found += 1
124+
else:
125+
assert line.startswith(datatype_file_start)
126+
114127
# Are all datatypes listed?
115128
assert datatype_bases_found == datatype_count
116129

130+
# Restrict (a little) the datatype bases
131+
filename_template = text.make_filename_template(
132+
"raw",
133+
schema_obj,
134+
suffixes=["events"],
135+
placeholders=placeholders,
136+
empty_dirs=False,
137+
pdf_format=True,
138+
)
139+
140+
datatype_bases_found = 0
141+
template_started = False
142+
for line in filename_template.split("\n"):
143+
if "```" in line:
144+
if template_started:
145+
break
146+
147+
template_started = True
148+
continue
149+
150+
if not template_started:
151+
continue
152+
153+
if line.startswith("sub-<label>"):
154+
continue
155+
if line.startswith(" [ses-<label>/]"):
156+
continue
157+
158+
if line in datatype_bases:
159+
datatype_bases_found += 1
160+
else:
161+
assert line.startswith(datatype_file_start)
162+
163+
# In this case events is not defined for all datatypes
164+
assert datatype_bases_found < datatype_count
165+
117166

118167
def test_define_common_principles(schema_obj):
119168
"""Ensure that define_common_principles returns a string."""

0 commit comments

Comments
 (0)