Skip to content

Commit

Permalink
Eliminate initial "_" when suffix is only entity
Browse files Browse the repository at this point in the history
  • Loading branch information
pvandyken committed Feb 22, 2024
1 parent d3f1d74 commit 0b2b6a4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
20 changes: 15 additions & 5 deletions snakebids/paths/_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def bids_factory(spec: BidsPathSpec, *, _implicit: bool = False) -> BidsFunction

parse_entities = _get_entity_parser(aliases)

def bids(
def bids( # noqa: PLR0912
root: str | Path | None = None,
*,
datatype: str | None = None,
Expand Down Expand Up @@ -228,13 +228,23 @@ def bids(

if datatype:
path_parts.append(datatype)
tail: list[str] = []
if suffix is not None:
tail.append(suffix)
if extension is not None:
tail.append(extension)
path_parts.append(
"_".join(it.chain(spec_parts[:split], custom_parts, spec_parts[split:]))
"_".join(
it.chain(
spec_parts[:split],
custom_parts,
spec_parts[split:],
["".join(tail)],
)
)
)

tail = f"_{suffix}{extension or ''}" if suffix else extension or ""

result = os.path.join(*path_parts) + tail
result = os.path.join(*path_parts)
if custom_parts and _implicit and not in_interactive_session():
wrn_msg = (
f"Path generated with unrecognized entities, and a snakebids spec has "
Expand Down
8 changes: 8 additions & 0 deletions snakebids/tests/test_paths/test_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ def test_underscore_does_not_precede_extension(
def test_no_underscore_at_end_if_no_suffix(self, entities: dict[str, str]):
assert bids(**entities)[-1] != "_"

@given(entities=_bids_args(nonstandard=True, entities=None))
def test_no_underscore_at_beginning_if_only_suffix(
self, entities: dict[str, str]
):
assert bids(**entities)[0] != "_"

@given(entities=_bids_args(), root=_roots())
def test_beginning_of_path_always_root(
self, entities: dict[str, str], root: str
Expand Down Expand Up @@ -322,4 +328,6 @@ def test_benchmark_bids(benchmark: Benchmark):
england="britain",
space="cosmos",
rome="fell",
suffix="suffix",
extension=".ext",
)

0 comments on commit 0b2b6a4

Please sign in to comment.