From fda0163ff237ec709672761717a1ac140386adbc Mon Sep 17 00:00:00 2001 From: Tristan Kuehn Date: Fri, 16 Sep 2022 14:48:31 -0400 Subject: [PATCH] Move CLI commands to their own module --- autobidsportal.ini.example | 2 +- autobidsportal/__init__.py | 1 + autobidsportal/app.py | 33 ++++++++++++++- bids_form.py => autobidsportal/cli.py | 60 ++++++++------------------- autobidsportal/routes.py | 2 +- 5 files changed, 51 insertions(+), 47 deletions(-) rename bids_form.py => autobidsportal/cli.py (72%) diff --git a/autobidsportal.ini.example b/autobidsportal.ini.example index 8ea7ea15..d20497f2 100644 --- a/autobidsportal.ini.example +++ b/autobidsportal.ini.example @@ -1,5 +1,5 @@ [uwsgi] -module = bids_form:app +module = autobidsportal:create_app() master = true processes = 5 diff --git a/autobidsportal/__init__.py b/autobidsportal/__init__.py index e69de29b..ee6a952c 100644 --- a/autobidsportal/__init__.py +++ b/autobidsportal/__init__.py @@ -0,0 +1 @@ +from autobidsportal.app import create_app diff --git a/autobidsportal/app.py b/autobidsportal/app.py index f9e03198..dc0d0d3e 100644 --- a/autobidsportal/app.py +++ b/autobidsportal/app.py @@ -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): @@ -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) @@ -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 diff --git a/bids_form.py b/autobidsportal/cli.py similarity index 72% rename from bids_form.py rename to autobidsportal/cli.py index f7c53a42..77a108c8 100644 --- a/bids_form.py +++ b/autobidsportal/cli.py @@ -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: @@ -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. + The point of this wrcurrent_apper 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. @@ -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(): @@ -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. diff --git a/autobidsportal/routes.py b/autobidsportal/routes.py index a6a574c0..71c7b439 100644 --- a/autobidsportal/routes.py +++ b/autobidsportal/routes.py @@ -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", )