Skip to content

Commit

Permalink
Move CLI commands to their own module
Browse files Browse the repository at this point in the history
  • Loading branch information
tkkuehn committed Sep 16, 2022
1 parent 18f3461 commit 1c1ec54
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 46 deletions.
2 changes: 1 addition & 1 deletion autobidsportal.ini.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[uwsgi]
module = bids_form:app
module = autobidsportal:create_app()

master = true
processes = 5
Expand Down
3 changes: 3 additions & 0 deletions autobidsportal/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Expose app creation function"""

from autobidsportal.app import create_app
33 changes: 31 additions & 2 deletions autobidsportal/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,22 @@
import rq

from autobidsportal.routes import portal_blueprint
from autobidsportal.models import db, login
from autobidsportal.models import (
db,
login,
User,
Study,
Principal,
Notification,
Task,
Cfmm2tarOutput,
Tar2bidsOutput,
ExplicitPatient,
)
from autobidsportal.errors import bad_request, not_found_error, internal_error
from autobidsportal.email import mail
# This will register the CLI commands
import autobidsportal.cli # pylint: disable=unused-import


def create_app(config_object=None, override_dict=None):
Expand Down Expand Up @@ -42,7 +55,7 @@ def create_app(config_object=None, override_dict=None):
if override_dict is not None:
app.config.update(override_dict)
app.logger.setLevel(app.config["LOG_LEVEL"])
app.register_blueprint(portal_blueprint, url_prefix="/")
app.register_blueprint(portal_blueprint, url_prefix="/", cli_group=None)
app.register_error_handler(400, bad_request)
app.register_error_handler(404, not_found_error)
app.register_error_handler(500, internal_error)
Expand All @@ -56,4 +69,20 @@ def create_app(config_object=None, override_dict=None):
login.login_view = "login"
mail.init_app(app)

@app.shell_context_processor
def make_shell_context():
"""Add useful variables into the shell context."""
return {
"db": db,
"User": User,
"Study": Study,
"Principal": Principal,
"Notification": Notification,
"Task": Task,
"Cfmm2tarOutput": Cfmm2tarOutput,
"Tar2bidsOutput": Tar2bidsOutput,
"ExplicitPatient": ExplicitPatient,
}


return app
58 changes: 16 additions & 42 deletions bids_form.py → autobidsportal/cli.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,13 @@
"""Flask entry point with extra CLI commands."""

from autobidsportal.app import create_app
from flask import current_app

from autobidsportal.dcm4cheutils import gen_utils, Dcm4cheError
from autobidsportal.models import (
db,
User,
Study,
Principal,
Notification,
Task,
Cfmm2tarOutput,
Tar2bidsOutput,
ExplicitPatient,
)
from autobidsportal.tasks import update_heuristics


app = create_app()


@app.shell_context_processor
def make_shell_context():
"""Add useful variables into the shell context."""
return {
"db": db,
"User": User,
"Study": Study,
"Principal": Principal,
"Notification": Notification,
"Task": Task,
"Cfmm2tarOutput": Cfmm2tarOutput,
"Tar2bidsOutput": Tar2bidsOutput,
"ExplicitPatient": ExplicitPatient,
}


@app.cli.command()
from autobidsportal.models import db, Principal, Study, Task
from autobidsportal.routes import portal_blueprint


@portal_blueprint.cli.command()
def check_pis():
"""Add a list of pi names from dicom server to the Principal table."""
try:
Expand All @@ -50,17 +22,19 @@ def check_pis():
return "Success"


@app.cli.command()
@portal_blueprint.cli.command()
def run_update_heuristics():
"""Clone the heuristic repo if it doesn't exist, then pull from it.
The point of this wrapper function is to expose the task to the CLI.
"""

update_heuristics()
Task.launch_task(
"update_heuristics", "Update heuristics from CLI"
)


@app.cli.command()
@portal_blueprint.cli.command()
def run_all_cfmm2tar():
"""Run cfmm2tar on all active studies.
Expand All @@ -80,12 +54,12 @@ def run_all_cfmm2tar():
) or (not study.active):
print(f"Skipping study {study.id}. Active: {study.active}")
continue
app.task_queue.enqueue(
current_app.task_queue.enqueue(
"autobidsportal.tasks.check_tar_files", study.id
)


@app.cli.command()
@portal_blueprint.cli.command()
def run_all_tar2bids():
"""Run tar2bids on all active studies."""
for study in Study.query.all():
Expand All @@ -101,12 +75,12 @@ def run_all_tar2bids():
) or not study.active:
print(f"Skipping study {study.id}. Active: {study.active}")
continue
app.task_queue.enqueue(
current_app.task_queue.enqueue(
"autobidsportal.tasks.find_unprocessed_tar_files", study.id
)


@app.cli.command()
@portal_blueprint.cli.command()
def run_all_archive():
"""Archive all active studies' raw datasets.
Expand Down
2 changes: 1 addition & 1 deletion autobidsportal/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
from autobidsportal.email import send_email

portal_blueprint = Blueprint(
"portal_blueprint", __name__, template_folder="templates"
"portal_blueprint", __name__, template_folder="templates",
)


Expand Down

0 comments on commit 1c1ec54

Please sign in to comment.