Skip to content

Commit

Permalink
debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyCBakerPhD committed May 24, 2024
1 parent 3620d65 commit c70954e
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 53 deletions.
Empty file added src/__init__.py
Empty file.
Empty file added src/pyflask/__init__.py
Empty file.
6 changes: 3 additions & 3 deletions src/pyflask/apis/_dandi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from typing import List, Tuple, Union

import flask.restx
import flask_restx

from ..utils import catch_exception_and_abort, server_error_responses
from .utils import catch_exception_and_abort, server_error_responses

dandi_api = flask_restx.Namespace(
name="dandi", description="Request various static listings from the DANDI Python API."
Expand All @@ -14,7 +14,7 @@
@dandi_api.route(rule="/get-recommended-species")
class SupportedSpecies(flask_restx.Resource):

@neurosift_api.doc(
@dandi_api.doc(
description="Request the list of currently supported species (by Latin Binomial name) for DANDI. Note that any "
"explicit NCBI taxonomy link is also supported.",
responses=server_error_responses(codes=[200, 500]),
Expand Down
4 changes: 2 additions & 2 deletions src/pyflask/apis/_neurosift.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from typing import Union

import flask
import flask.restx
import flask_restx

from ..utils import (
from .utils import (
abort_if_not_nwb_file,
catch_exception_and_abort,
server_error_responses,
Expand Down
4 changes: 2 additions & 2 deletions src/pyflask/apis/_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from typing import Dict, Union

import flask.restx
import flask_restx

from ..utils import catch_exception_and_abort, server_error_responses
from .utils import catch_exception_and_abort, server_error_responses

system_api = flask_restx.Namespace(name="system", description="Request various system specific information.")

Expand Down
3 changes: 2 additions & 1 deletion src/pyflask/apis/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

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

from .utils import catch_exception_and_abort, server_error_responses

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

Expand Down
File renamed without changes.
File renamed without changes.
37 changes: 11 additions & 26 deletions src/pyflask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
from datetime import datetime
from logging import DEBUG, Formatter
from logging.handlers import RotatingFileHandler
from os.path import isabs
from pathlib import Path
from signal import SIGINT
from typing import Union
from urllib.parse import unquote

# https://stackoverflow.com/questions/32672596/pyinstaller-loads-script-multiple-times#comment103216434_32677108
multiprocessing.freeze_support()
Expand All @@ -25,16 +22,11 @@
startup_api,
system_api,
)
from flask import Flask, Response, send_file, send_from_directory
from apis.utils import catch_exception_and_abort, server_error_responses
from flask import Flask
from flask_cors import CORS
from flask_restx import Api, Resource
from manageNeuroconv.info import (
CONVERSION_SAVE_FOLDER_PATH,
GUIDE_ROOT_FOLDER,
STUB_SAVE_FOLDER_PATH,
resource_path,
)
from utils import catch_exception_and_abort, server_error_responses
from manageNeuroconv.info import GUIDE_ROOT_FOLDER, get_project_root_path

all_apis = [data_api, neuroconv_api, startup_api, neurosift_api, dandi_api, system_api]

Expand All @@ -51,8 +43,8 @@
LOG_FILE_PATH = Path(LOG_FOLDER) / f"{timestamp}.log"

# Fetch version from package.json
package_json_file_path = resource_path("package.json")
with open(file=package_json_file_path) as fp:
package_json_file_path = get_project_root_path() / "package.json"
with open(file=package_json_file_path, mode="r") as fp:
package_json = json.load(fp=fp)

# Initialize top-level API and set namespaces
Expand Down Expand Up @@ -89,21 +81,14 @@ def post(self):
selected_logger(message)


@flask_app.route("/cpus")
def get_cpu_count():
from psutil import cpu_count

physical = cpu_count(logical=False)
logical = cpu_count()

return dict(physical=physical, logical=logical)


@flask_api.route("/server_shutdown", endpoint="shutdown")
@neurosift_api.doc(
description="Handle adding and fetching NWB files from the global file registry.",
)
@flask_api.doc(description="Close the Flask server.")
class Shutdown(Resource):

@flask_api.doc(
description="To trigger a shutdown, set a GET request to this endpoint. It will not return a response.",
responses=server_error_responses(codes=[200, 500]),
)
def get(self) -> None:
werkzeug_shutdown_function = flask.request.environ.get("werkzeug.server.shutdown")
flask_api.logger.info("Shutting down server...")
Expand Down
3 changes: 2 additions & 1 deletion src/pyflask/manageNeuroconv/info/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
CONVERSION_SAVE_FOLDER_PATH,
GUIDE_ROOT_FOLDER,
STUB_SAVE_FOLDER_PATH,
resource_path,
get_project_root_path,
get_source_base_path,
)
50 changes: 32 additions & 18 deletions src/pyflask/manageNeuroconv/info/urls.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,50 @@
import json
import os
import pathlib
import sys
from pathlib import Path


def resource_path(relative_path):
"""Get absolute path to resource, works for dev and for PyInstaller"""
try:
def get_source_base_path() -> pathlib.Path:
"""Get absolute path of a relative resource to the app; works for both dev mode and for PyInstaller."""
# Production: PyInstaller creates a temp folder and stores path in _MEIPASS
if hasattr(sys, "_MEIPASS"):
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = Path(__file__).parent.parent.parent.parent
base_path = pathlib.Path(sys._MEIPASS)

return Path(base_path) / relative_path
# Dev mode: base is the root of the `src` directory for the project
else:
base_path = pathlib.Path(__file__).parent.parent.parent.parent

return base_path


def get_project_root_path() -> pathlib.Path:
"""Get absolute path of a relative resource to the app; works for both dev mode and for PyInstaller."""
# Production: PyInstaller creates a temp folder and stores path in _MEIPASS
if hasattr(sys, "_MEIPASS"):
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = pathlib.Path(sys._MEIPASS).parent

# Dev mode: base is the root of the `src` directory for the project
else:
base_path = pathlib.Path(__file__).parent.parent.parent.parent.parent

return base_path


is_test_environment = os.environ.get("VITEST")

path_config = resource_path(
"paths.config.json"
) # NOTE: Must have pyflask for running the GUIDE as a whole, but errors for just the server
f = path_config.open()
data = json.load(f)
GUIDE_ROOT_FOLDER = Path(Path.home(), data["root"])

path_config_file_path = get_source_base_path() / "paths.config.json"
with open(file=path_config_file_path, mode="r") as fp:
path_config = json.load(fp=fp)
GUIDE_ROOT_FOLDER = pathlib.Path.home() / path_config["root"]

if is_test_environment:
GUIDE_ROOT_FOLDER = GUIDE_ROOT_FOLDER / ".test"

STUB_SAVE_FOLDER_PATH = Path(GUIDE_ROOT_FOLDER, *data["subfolders"]["preview"])
CONVERSION_SAVE_FOLDER_PATH = Path(GUIDE_ROOT_FOLDER, *data["subfolders"]["conversions"])

f.close()
STUB_SAVE_FOLDER_PATH = pathlib.Path(GUIDE_ROOT_FOLDER, *path_config["subfolders"]["preview"])
CONVERSION_SAVE_FOLDER_PATH = pathlib.Path(GUIDE_ROOT_FOLDER, *path_config["subfolders"]["conversions"])

# Create all nested home folders
STUB_SAVE_FOLDER_PATH.mkdir(exist_ok=True, parents=True)
Expand Down

0 comments on commit c70954e

Please sign in to comment.