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

Implement first pass at simple plugins #261

Merged
merged 8 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 23 additions & 1 deletion snakebids/app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Tools to generate a Snakemake-based BIDS app."""
from __future__ import annotations

import argparse
import logging
import sys
from collections.abc import Sequence
from pathlib import Path
from typing import Any, Dict, List, Optional
from typing import Any, Callable, Dict, List, Optional

import attr
import boutiques.creator as bc
Expand Down Expand Up @@ -111,6 +113,22 @@ class SnakeBidsApp:
takes_self=True,
)
args: Optional[SnakebidsArgs] = None
plugins: list[Callable[[SnakeBidsApp], None]] = attr.Factory(list)

def add_plugins(self, plugins: Sequence[Callable[[SnakeBidsApp], None]]):
tkkuehn marked this conversation as resolved.
Show resolved Hide resolved
"""Supply list of methods to be called after cli parsing

Each callable in ``plugins`` should take, as a single argument, a
reference to the ``SnakeBidsApp``, and should not return anything.
Plugins may perform any arbitrary side effects, including validation,
pvandyken marked this conversation as resolved.
Show resolved Hide resolved
optimization, other other enhancements to the snakebids app.

CLI parameters may be read from ``SnakeBidsApp.config``. Plugins
are responsible for documenting what properties they expect to find
in the config.
"""
# pylint: disable=no-member
self.plugins.extend(plugins)

def run_snakemake(self):
"""Run snakemake with that config.
Expand Down Expand Up @@ -187,6 +205,10 @@ def run_snakemake(self):
new_config_file = args.outputdir / self.configfile_path
self.config["root"] = ""

# pylint: disable=not-an-iterable
for plugin in self.plugins:
tkkuehn marked this conversation as resolved.
Show resolved Hide resolved
plugin(self)

# Write the config file
write_config_file(
config_file=new_config_file,
Expand Down
7 changes: 7 additions & 0 deletions snakebids/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,19 @@ def test_runs_in_correct_mode(
reset_db=True,
)

def plugin(my_app):
my_app.foo = "bar"

app.add_plugins([plugin])

try:
app.run_snakemake()
except SystemExit as e:
print("System exited prematurely")
print(e)

assert app.foo == "bar"
pvandyken marked this conversation as resolved.
Show resolved Hide resolved

# First condition: outputdir is an arbitrary path
if root not in ["app", "app/results"] or (root == "app" and tail):
cwd = outputdir.resolve()
Expand Down