Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always set SnakeBidsApp.args in run_snakemake #310

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions snakebids/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,13 @@ def run_snakemake(self) -> None:

# If no SnakebidsArgs were provided on class instantiation, we compute args
# using the provided parser
if self.args:
args = self.args
else:
if not self.args:
# Dynamic args include --filter-... and --wildcards-... . They depend on the
# config
add_dynamic_args(
self.parser, self.config["parse_args"], self.config["pybids_inputs"]
)
args = parse_snakebids_args(self.parser)
self.args = parse_snakebids_args(self.parser)

# Update our config file:
# - Add path to snakefile to the config so workflows can grab files relative to
Expand All @@ -154,21 +152,21 @@ def run_snakemake(self) -> None:
self.config["snakefile"] = self.snakefile_path

# Update config with pybids settings
self.config["pybidsdb_dir"] = args.pybidsdb_dir
self.config["pybidsdb_reset"] = args.pybidsdb_reset
self.config["pybidsdb_dir"] = self.args.pybidsdb_dir
self.config["pybidsdb_reset"] = self.args.pybidsdb_reset

update_config(self.config, args)
update_config(self.config, self.args)

# First, handle outputs in snakebids_root or results folder
try:
# py3.9 has the Path.is_relative() function. But as long as we support py38
# and lower, this is the easiest way
args.outputdir.resolve().relative_to(self.snakemake_dir / "results")
self.args.outputdir.resolve().relative_to(self.snakemake_dir / "results")
relative_to_results = True
except ValueError:
relative_to_results = False

if self.snakemake_dir == args.outputdir.resolve() or relative_to_results:
if self.snakemake_dir == self.args.outputdir.resolve() or relative_to_results:
write_output_mode(self.snakemake_dir / ".snakebids", "workflow")

new_config_file = self.snakemake_dir / self.configfile_path
Expand All @@ -192,12 +190,12 @@ def run_snakemake(self) -> None:
# Attempt to prepare the output folder. Anything going wrong will raise a
# RunError, as described in the docstring
try:
prepare_bidsapp_output(args.outputdir, args.force)
prepare_bidsapp_output(self.args.outputdir, self.args.force)
except RunError as err:
print(err.msg)
sys.exit(1)
cwd = args.outputdir
new_config_file = args.outputdir / self.configfile_path
cwd = self.args.outputdir
new_config_file = self.args.outputdir / self.configfile_path
self.config["root"] = ""

app = self
Expand Down
28 changes: 28 additions & 0 deletions snakebids/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import copy
import json
import sys
from pathlib import Path
from typing import Any, cast

Expand Down Expand Up @@ -204,6 +205,33 @@ def test_runs_in_correct_mode(
]
)

def test_plugin_args(self, mocker: MockerFixture, app: SnakeBidsApp):
"""Test that plugins have access to args parsed from the CLI."""
# Get mocks for all the io functions
self.io_mocks(mocker)
mocker.patch.object(
sn_app,
"update_config",
side_effect=(
lambda config, sn_args: config.update(sn_args.args_dict) # type: ignore
),
)
mocker.patch.object(
sys, "argv", ["script_name", "path/to/input", "app/results", "participant"]
)

def plugin(my_app: SnakeBidsApp):
my_app.foo = my_app.args.outputdir # type: ignore

app.plugins.extend([plugin])
try:
app.run_snakemake()
except SystemExit as e:
print("System exited prematurely")
print(e)

assert app.foo == Path("app/results").resolve() # type: ignore

def test_plugins(self, mocker: MockerFixture, app: SnakeBidsApp):
# Get mocks for all the io functions
self.io_mocks(mocker)
Expand Down