Skip to content

Commit

Permalink
reformatting to satisfy flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy Wiggins authored and Roy Wiggins committed Dec 7, 2024
1 parent 2af6333 commit a10cc79
Show file tree
Hide file tree
Showing 77 changed files with 1,194 additions and 1,031 deletions.
4 changes: 2 additions & 2 deletions app/alembic/env.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from logging.config import fileConfig
# from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool
Expand All @@ -16,7 +16,7 @@

# Interpret the config file for Python logging.
# This line sets up loggers basically.
# fileConfig(config.config_file_name) # type: ignore
# fileConfig(config.config_file_name) # type: ignore

# add your model's MetaData object here
# for 'autogenerate' support
Expand Down
4 changes: 1 addition & 3 deletions app/alembic/versions/1c51b42f13d5_self_tests_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "1c51b42f13d5"
Expand All @@ -20,7 +18,7 @@ def upgrade():
connection = op.get_bind()
dialect = connection.dialect
if dialect.name == "sqlite":
op.execute("ALTER TABLE tests ADD COLUMN rule_type character varying NULL")
op.execute("ALTER TABLE tests ADD COLUMN rule_type character varying NULL")
else:
op.execute("ALTER TABLE tests ADD COLUMN IF NOT EXISTS rule_type character varying NULL")

Expand Down
2 changes: 0 additions & 2 deletions app/alembic/versions/31a5db4f993e_subtasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "31a5db4f993e"
Expand Down
4 changes: 2 additions & 2 deletions app/alembic/versions/6041e3878f32_self_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def upgrade():
dialect = connection.dialect
print(dialect.name)
if dialect.name == "sqlite":
jsonb = sa.Text() # type: ignore
jsonb = sa.Text() # type: ignore
else:
jsonb = JSONB(astext_type=sa.Text()) # type: ignore
jsonb = JSONB(astext_type=sa.Text()) # type: ignore

op.create_table(
"tests",
Expand Down
6 changes: 3 additions & 3 deletions app/alembic/versions/9c38f4f15a29_processor_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ def upgrade():
connection = op.get_bind()
dialect = connection.dialect
if dialect.name == "sqlite":
jsonb = sa.Text # type: ignore
jsonb = sa.Text # type: ignore
else:
jsonb = postgresql.JSONB # type: ignore
jsonb = postgresql.JSONB # type: ignore

op.create_table(
"processor_outputs",
sa.Column("id", sa.Integer, primary_key=True, autoincrement=True),
sa.Column("time", sa.DateTime(timezone=True), server_default=func.now()),
sa.Column("task_id", sa.String, sa.ForeignKey("tasks.id"),nullable=True),
sa.Column("task_id", sa.String, sa.ForeignKey("tasks.id"), nullable=True),
sa.Column("task_acc", sa.String),
sa.Column("task_mrn", sa.String),
sa.Column("module", sa.String),
Expand Down
1 change: 0 additions & 1 deletion app/alembic/versions/af102cd510bd_remove_task_fk.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
Expand Down
4 changes: 2 additions & 2 deletions app/alembic/versions/ee4575e2cf40_tasks_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def upgrade():
connection = op.get_bind()
dialect = connection.dialect
if dialect.name == "sqlite":
jsonb = sa.Text() # type: ignore
jsonb = sa.Text() # type: ignore
else:
jsonb = postgresql.JSONB(astext_type=sa.Text()) # type: ignore
jsonb = postgresql.JSONB(astext_type=sa.Text()) # type: ignore
create_table(
"tasks",
sa.Column("id", sa.String(), nullable=False),
Expand Down
38 changes: 18 additions & 20 deletions app/bookkeeping/bookkeeper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
import contextlib
import os
from pathlib import Path
import subprocess
import sys
from typing import Union
import asyncpg
from sqlalchemy.dialects.postgresql import insert
import uvicorn
import datetime
import daiquiri
import hupper

# Starlette-related includes
Expand All @@ -36,10 +33,13 @@
import bookkeeping.query as query
import bookkeeping.config as bk_config
from decoRouter import Router as decoRouter
from alembic.config import Config
from alembic import command

router = decoRouter()

###################################################################################
## Configuration and initialization
# Configuration and initialization
###################################################################################


Expand All @@ -56,33 +56,29 @@ async def verify(self, token: str):
return None
return SimpleUser("user")



###################################################################################
## Event handlers
# Event handlers
###################################################################################


from alembic.config import Config
from alembic import command

def migrate_database() -> None:
alembic_cfg = Config()
alembic_cfg.set_main_option('script_location', os.path.abspath(os.path.dirname(os.path.realpath(__file__))+'/../alembic'))
alembic_cfg.set_main_option('script_location',
os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/../alembic'))
alembic_cfg.set_main_option('sqlalchemy.url', bk_config.DATABASE_URL)
command.upgrade(alembic_cfg, 'head')


###################################################################################
## Endpoints for event submission
# Endpoints for event submission
###################################################################################

# async def execute_db_operation(operation) -> None:
# global connection
# """Executes a previously prepared db.database operation."""
# try:
# connection.execute(operation)
# except:
# except Exception:
# pass

@router.post()
Expand Down Expand Up @@ -128,7 +124,7 @@ async def processor_logs(request) -> JSONResponse:
time = datetime.datetime.now()
try:
logs = str(payload.get("logs", ""))
except:
except Exception:
return JSONResponse({"error": "unable to read logs"}, 400)

if (logs_folder_str := config.mercure.processing_logs.logs_file_store) and (
Expand Down Expand Up @@ -349,13 +345,13 @@ async def post_task_event(request) -> JSONResponse:
if "timestamp" in payload:
try:
client_timestamp = float(payload.get("timestamp")) # type: ignore
except:
except Exception:
pass

if "time" in payload:
try:
event_time = datetime.datetime.fromisoformat(payload.get("time")) # type: ignore
except:
except Exception:
pass

# series_uid = payload.get("series_uid", "")
Expand Down Expand Up @@ -388,14 +384,14 @@ async def post_task_event(request) -> JSONResponse:
@requires("authenticated")
async def store_processor_output(request) -> JSONResponse:
payload = dict(await request.json())
values_dict = {k:payload[k] for k in ("task_id", "task_acc", "task_mrn", "module", "index", "settings", "output")}
values_dict = {k: payload[k] for k in ("task_id", "task_acc", "task_mrn", "module", "index", "settings", "output")}
query = db.processor_outputs_table.insert().values(**values_dict)
await db.database.execute(query)
return JSONResponse({"ok": ""})


###################################################################################
## Main entry function
# Main entry function
###################################################################################


Expand All @@ -422,6 +418,7 @@ async def server_error(request, exc) -> Response:

app: Starlette


def create_app() -> Starlette:
bk_config.read_bookkeeper_config()
app = Starlette(debug=bk_config.DEBUG_MODE, routes=router, lifespan=lifespan, exception_handlers=exception_handlers)
Expand All @@ -433,11 +430,12 @@ def create_app() -> Starlette:
app.mount("/query", query.query_app)
return app


def main(args=sys.argv[1:]) -> None:
global app
if "--reload" in args or os.getenv("MERCURE_ENV", "PROD").lower() == "dev":
# start_reloader will only return in a monitored subprocess
reloader = hupper.start_reloader("bookkeeper.main")
hupper.start_reloader("bookkeeper.main")
import logging

logging.getLogger("multipart.multipart").setLevel(logging.WARNING)
Expand All @@ -457,4 +455,4 @@ def main(args=sys.argv[1:]) -> None:
except Exception as e:
logger.error(f"Could not read configuration file: {e}")
logger.info("Going down.")
sys.exit(1)
sys.exit(1)
9 changes: 6 additions & 3 deletions app/bookkeeping/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Standard python includes
import os
from typing import Any, Optional
from typing import Optional
import daiquiri

# Starlette-related includes
Expand All @@ -16,14 +16,15 @@
# Create local logger instance
logger = daiquiri.getLogger("config")
bookkeeper_config: Config
config_filename:str = (os.getenv("MERCURE_CONFIG_FOLDER") or "/opt/mercure/config") + "/bookkeeper.env"
config_filename: str = (os.getenv("MERCURE_CONFIG_FOLDER") or "/opt/mercure/config") + "/bookkeeper.env"
DATABASE_URL: str
BOOKKEEPER_PORT: int
BOOKKEEPER_HOST: str
DATABASE_SCHEMA: Optional[str]
API_KEY: Optional[str]
DEBUG_MODE: bool


def read_bookkeeper_config() -> Config:
global bookkeeper_config, BOOKKEEPER_PORT, BOOKKEEPER_HOST, DATABASE_URL, DATABASE_SCHEMA, DEBUG_MODE, API_KEY
bookkeeper_config = Config(config_filename)
Expand All @@ -36,6 +37,7 @@ def read_bookkeeper_config() -> Config:
API_KEY = None
return bookkeeper_config


def set_api_key() -> None:
global API_KEY

Expand All @@ -46,6 +48,7 @@ def set_api_key() -> None:
c = read_config()
API_KEY = c.bookkeeper_api_key
if not API_KEY or API_KEY == "BOOKKEEPER_TOKEN_PLACEHOLDER":
raise Exception("No API key set in mercure.json or value unchanged from placeholder. Bookkeeper cannot function.")
raise Exception("No API key set in mercure.json or value"
" unchanged from placeholder. Bookkeeper cannot function.")
except (ResourceWarning, FileNotFoundError) as e:
raise e
15 changes: 9 additions & 6 deletions app/bookkeeping/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
logger = config.get_logger()

###################################################################################
## Definition of database tables
# Definition of database tables
###################################################################################
database: databases.Database
metadata: sqlalchemy.MetaData
Expand All @@ -36,14 +36,17 @@
processor_logs_table: sqlalchemy.Table
processor_outputs_table: sqlalchemy.Table


def init_database(url=None, schema=None) -> databases.Database:
global database, metadata, mercure_events, webgui_events, dicom_files, dicom_series, task_events, file_events, dicom_series_map, series_sequence_data, tasks_table, tests_table, processor_logs_table, processor_outputs_table
global database, metadata, mercure_events, webgui_events, dicom_files, dicom_series, task_events
global file_events, dicom_series_map, series_sequence_data, tasks_table, tests_table
global processor_logs_table, processor_outputs_table

database = databases.Database(url or bk_config.DATABASE_URL)
metadata = sqlalchemy.MetaData(schema=(schema or bk_config.DATABASE_SCHEMA))

# SQLite does not support JSONB natively, so we use TEXT instead
JSONB = sqlalchemy.types.Text() if 'sqlite://' in (url or bk_config.DATABASE_URL) else sqlalchemy.dialects.postgresql.JSONB
#

mercure_events = sqlalchemy.Table(
"mercure_events",
metadata,
Expand Down Expand Up @@ -190,12 +193,12 @@ def init_database(url=None, schema=None) -> databases.Database:
metadata,
sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True, autoincrement=True),
sqlalchemy.Column("time", sqlalchemy.DateTime(timezone=True), server_default=func.now()),
sqlalchemy.Column("task_id", sqlalchemy.String, sqlalchemy.ForeignKey("tasks.id"),nullable=True),
sqlalchemy.Column("task_id", sqlalchemy.String, sqlalchemy.ForeignKey("tasks.id"), nullable=True),
sqlalchemy.Column("task_acc", sqlalchemy.String),
sqlalchemy.Column("task_mrn", sqlalchemy.String),
sqlalchemy.Column("module", sqlalchemy.String),
sqlalchemy.Column("index", sqlalchemy.Integer),
sqlalchemy.Column("settings", JSONB),
sqlalchemy.Column("output", JSONB),
)
return database
return database
1 change: 0 additions & 1 deletion app/bookkeeping/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ def default(self, obj):
class CustomJSONResponse(JSONResponse):
def render(self, content: Any) -> bytes:
return json.dumps(content, cls=CustomJSONEncoder).encode("utf-8")

Loading

0 comments on commit a10cc79

Please sign in to comment.