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

Flask-side refactors #791

Closed
wants to merge 7 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
name: Dev Tests
name: App and Flask Coverage
on:
schedule:
- cron: "0 16 * * *" # Daily at noon EST
pull_request:
workflow_dispatch:

concurrency: # Cancel previous workflows on the same pull request
group: ${{ github.workflow }}-${{ github.ref }}
Expand All @@ -13,7 +14,7 @@ env:

jobs:
testing:
name: ${{ matrix.os }} # Will read on the dashboard as 'Dev Tests / {os}'
name: ${{ matrix.os }} # Will read on the dashboard as 'App and Flask Coverage / {os}'
runs-on: ${{ matrix.os }}
defaults:
run:
Expand Down
92 changes: 92 additions & 0 deletions .github/workflows/flask_dev_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
name: Flask Dev Tests
on:
schedule:
- cron: "0 17 * * *" # Daily at 1pm EST
pull_request:
workflow_dispatch:

# Cancel previous workflows on the same pull request
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
CACHE_NUMBER: 1 # increase to reset cache manually

jobs:
testing:
name: ${{ matrix.os }} # Will read on the dashboard as 'Flask Dev Tests / {os}'
runs-on: ${{ matrix.os }}
defaults:
run:
shell: bash -l {0}

strategy:
fail-fast: false
matrix:
include:
# current linux installation instructions use dev mode instead of distributable
# - python-version: "3.9"
# os: ubuntu-latest
# label: environments/environment-Linux.yml
# prefix: /usr/share/miniconda3/envs/nwb-guide

- python-version: "3.9"
os: macos-latest # Mac arm64 runner
label: environments/environment-MAC-apple-silicon.yml
prefix: /Users/runner/miniconda3/envs/nwb-guide

- python-version: "3.9"
os: macos-13 # Mac x64 runner
label: environments/environment-MAC-intel.yml
prefix: /Users/runner/miniconda3/envs/nwb-guide

- python-version: "3.9"
os: windows-latest
label: environments/environment-Windows.yml
prefix: C:\Miniconda3\envs\nwb-guide

steps:
- uses: actions/checkout@v4
- run: git fetch --prune --unshallow --tags

- name: Printout architecture
run: uname -m

# see https://github.com/conda-incubator/setup-miniconda#caching-environments
- name: Setup Mambaforge
uses: conda-incubator/setup-miniconda@v3
with:
miniforge-variant: Mambaforge
miniforge-version: latest
activate-environment: nwb-guide
use-mamba: true

- name: Set cache date
id: get-date
run: echo "today=$(/bin/date -u '+%Y%m%d')" >> $GITHUB_OUTPUT
shell: bash

- name: Cache Mamba env
uses: actions/cache@v4
with:
path: ${{ env.CONDA }}/envs
key:
conda-${{ runner.os }}-${{ runner.arch }}-${{steps.get-date.outputs.today }}-${{ hashFiles(matrix.label) }}-${{ env.CACHE_NUMBER }}
env:
CACHE_NUMBER: ${{ env.CACHE_NUMBER }}
id: cache

- if: steps.cache.outputs.cache-hit != 'true'
name: Update environment
run: mamba env update -f ${{ matrix.label }}

- name: Setup Node.js 20
uses: actions/setup-node@v4
with:
node-version: "20"

- run: npm ci --verbose

- name: Run Python tests on local dev mode
run: npm run test:server
20 changes: 9 additions & 11 deletions pyflask/apis/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import traceback

from errorHandlers import notBadRequestException
from flask_restx import Namespace, Resource, reqparse
from manageNeuroconv import generate_dataset, generate_test_data
from utils import catch_exception_and_abort, server_error_responses

data_api = Namespace("data", description="API route for dataset generation in the NWB GUIDE.")

Expand All @@ -22,15 +22,14 @@ def exception_handler(error):
@data_api.route("/generate")
@data_api.expect(generate_test_data_parser)
class GeneratetestData(Resource):
@data_api.doc(responses={200: "Success", 400: "Bad Request", 500: "Internal server error"})
@data_api.doc(
description="Generate example ecephys data using SpikeInterface.",
responses=server_error_responses(codes=[200, 500]),
)
@catch_exception_and_abort(api=data_api, code=500)
def post(self):
try:
arguments = generate_test_data_parser.parse_args()
generate_test_data(output_path=arguments["output_path"])
except Exception as exception:
if notBadRequestException(exception):
data_api.abort(500, str(exception))
raise exception
arguments = generate_test_data_parser.parse_args()
generate_test_data(output_path=arguments["output_path"])


generate_test_dataset_parser = reqparse.RequestParser()
Expand All @@ -48,5 +47,4 @@ def post(self):
return generate_dataset(input_path=arguments["input_path"], output_path=arguments["output_path"])

except Exception as exception:
if notBadRequestException(exception):
data_api.abort(500, str(exception))
data_api.abort(500, str(exception))
65 changes: 25 additions & 40 deletions pyflask/apis/neuroconv.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""API endpoint definitions for interacting with NeuroConv."""

import traceback
from typing import Dict

from errorHandlers import notBadRequestException
from flask import Response, request
from flask_restx import Namespace, Resource, reqparse
from manageNeuroconv import (
Expand All @@ -25,16 +25,18 @@
)
from manageNeuroconv.info import announcer

neuroconv_api = Namespace("neuroconv", description="Neuroconv neuroconv_api for the NWB GUIDE.")
neuroconv_api = Namespace(name="neuroconv", description="Neuroconv neuroconv_api for the NWB GUIDE.")

parser = reqparse.RequestParser()
parser.add_argument("interfaces", type=str, action="split", help="Interfaces cannot be converted")
parser.add_argument("interfaces", type=str, action="split", help="Interfaces cannot be converted.")


@neuroconv_api.errorhandler(Exception)
def exception_handler(error):
exceptiondata = traceback.format_exception(type(error), error, error.__traceback__)
return {"message": exceptiondata[-1], "traceback": "".join(exceptiondata)}
def exception_handler(error: Exception) -> Dict[str, str]:
full_traceback = traceback.format_exception(type(error), error, error.__traceback__)
message = full_traceback[-1]
remaining_traceback = "".join(full_traceback[:-1])
return {"message": message, "traceback": remaining_traceback}


@neuroconv_api.route("/")
Expand All @@ -50,8 +52,7 @@ def get(self):
**get_all_converter_info(),
}
except Exception as exception:
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))
neuroconv_api.abort(500, str(exception))
raise exception


Expand All @@ -62,8 +63,7 @@ def post(self):
try:
return get_source_schema(neuroconv_api.payload)
except Exception as exception:
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))
neuroconv_api.abort(500, str(exception))


@neuroconv_api.route("/locate")
Expand All @@ -73,8 +73,7 @@ def post(self):
try:
return locate_data(neuroconv_api.payload)
except Exception as exception:
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))
neuroconv_api.abort(500, str(exception))


@neuroconv_api.route("/locate/autocomplete")
Expand All @@ -84,8 +83,7 @@ def post(self):
try:
return autocomplete_format_string(neuroconv_api.payload)
except Exception as exception:
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))
neuroconv_api.abort(500, str(exception))


@neuroconv_api.route("/metadata")
Expand All @@ -97,8 +95,7 @@ def post(self):
neuroconv_api.payload.get("source_data"), neuroconv_api.payload.get("interfaces")
)
except Exception as exception:
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))
neuroconv_api.abort(500, str(exception))


@neuroconv_api.route("/convert")
Expand All @@ -109,8 +106,7 @@ def post(self):
return convert_to_nwb(neuroconv_api.payload)

except Exception as exception:
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))
neuroconv_api.abort(500, str(exception))


@neuroconv_api.route("/alignment")
Expand All @@ -121,8 +117,7 @@ def post(self):
return get_interface_alignment(neuroconv_api.payload)

except Exception as exception:
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))
neuroconv_api.abort(500, str(exception))


validate_parser = neuroconv_api.parser()
Expand All @@ -143,8 +138,7 @@ def post(self):
return validate_metadata(args.get("parent"), args.get("function_name"))

except Exception as exception:
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))
neuroconv_api.abort(500, str(exception))


@neuroconv_api.route("/upload/project")
Expand All @@ -163,8 +157,7 @@ def post(self):
return upload_project_to_dandi(**upload_options)

except Exception as exception:
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))
neuroconv_api.abort(500, str(exception))


@neuroconv_api.route("/upload/folder")
Expand All @@ -183,8 +176,7 @@ def post(self):
return upload_folder_to_dandi(**upload_options)

except Exception as exception:
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))
neuroconv_api.abort(500, str(exception))


@neuroconv_api.route("/upload")
Expand All @@ -206,8 +198,7 @@ def post(self):
return upload_multiple_filesystem_objects_to_dandi(**neuroconv_api.payload)

except Exception as exception:
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))
neuroconv_api.abort(500, str(exception))


@neuroconv_api.route("/inspect_file")
Expand All @@ -217,8 +208,7 @@ def post(self):
try:
return inspect_nwb_file(neuroconv_api.payload)
except Exception as exception:
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))
neuroconv_api.abort(500, str(exception))


@neuroconv_api.route("/inspect_folder")
Expand All @@ -230,8 +220,7 @@ def post(self):
return inspect_nwb_folder(url, neuroconv_api.payload)

except Exception as exception:
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))
neuroconv_api.abort(500, str(exception))


@neuroconv_api.route("/announce")
Expand All @@ -243,8 +232,7 @@ def post(self):
announcer.announce(data)
return True
except Exception as exception:
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))
neuroconv_api.abort(500, str(exception))


@neuroconv_api.route("/inspect")
Expand All @@ -271,8 +259,7 @@ def post(self):
return inspect_multiple_filesystem_objects(url, paths, **kwargs)

except Exception as exception:
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))
neuroconv_api.abort(500, str(exception))


@neuroconv_api.route("/html")
Expand All @@ -287,8 +274,7 @@ def post(self):
return html

except Exception as exception:
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))
neuroconv_api.abort(500, str(exception))


# Create an events endpoint
Expand All @@ -301,5 +287,4 @@ def get(self):
return Response(listen_to_neuroconv_events(), mimetype="text/event-stream")

except Exception as exception:
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))
neuroconv_api.abort(500, str(exception))
4 changes: 1 addition & 3 deletions pyflask/apis/startup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""API endpoint definitions for startup operations."""

from errorHandlers import notBadRequestException
from flask_restx import Namespace, Resource

startup_api = Namespace("startup", description="API for startup commands related to the NWB GUIDE.")
Expand Down Expand Up @@ -40,6 +39,5 @@ def get(self):

return True
except Exception as exception:
if notBadRequestException(exception=exception):
startup_api.abort(500, str(exception))
startup_api.abort(500, str(exception))
raise exception
Loading
Loading