Skip to content

Commit 0167f96

Browse files
committed
Corrected database call in 'validate_and_sanitise' function; simplified LIF file registration and processing parameters; corrected 'murfey_workflow' call
1 parent e796663 commit 0167f96

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

src/murfey/server/api/clem.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
CLEMTIFFFile,
2424
)
2525
from murfey.util.db import Session as MurfeySession
26-
from murfey.util.models import LifFileInfo, TiffSeriesInfo
26+
from murfey.util.models import TiffSeriesInfo
2727

2828
# Use backport from importlib_metadata for Python <3.10
2929
if sys.version_info.major == 3 and sys.version_info.minor < 10:
30-
from importlib_metadata import entry_points
30+
from importlib_metadata import EntryPoint, entry_points
3131
else:
32-
from importlib.metadata import entry_points
32+
from importlib.metadata import EntryPoint, entry_points
3333

3434
# Set up logger
3535
logger = getLogger("murfey.server.api.clem")
@@ -52,21 +52,29 @@
5252
"""
5353

5454

55-
def validate_and_sanitise(file: Path, session_id: int) -> Path:
55+
def validate_and_sanitise(
56+
file: Path,
57+
session_id: int,
58+
db: Session,
59+
) -> Path:
5660
"""
5761
Performs validation and sanitisation on the incoming file paths, ensuring that
5862
no forbidden characters are present and that the the path points only to allowed
5963
sections of the file server.
6064
6165
Returns the file path as a sanitised string that can be converted into a Path
6266
object again.
67+
68+
NOTE: Due to the instrument name query, 'db' now needs to be passed as an
69+
explicit variable to this function from within a FastAPI endpoint, as using the
70+
instance that was imported directly won't load it in the correct state.
6371
"""
6472

6573
# Resolve symlinks and directory changes to get full file path
6674
full_path = path.normpath(path.realpath(file))
6775

6876
instrument_name = (
69-
murfey_db.exec(select(MurfeySession).where(MurfeySession.id == session_id))
77+
db.exec(select(MurfeySession).where(MurfeySession.id == session_id))
7078
.one()
7179
.instrument_name
7280
)
@@ -144,7 +152,7 @@ def get_db_entry(
144152
# Validate file path if provided
145153
if file_path is not None:
146154
try:
147-
file_path = validate_and_sanitise(file_path, session_id)
155+
file_path = validate_and_sanitise(file_path, session_id, db)
148156
except Exception:
149157
raise Exception
150158

@@ -220,7 +228,7 @@ def register_lif_file(
220228
# Add metadata information if provided
221229
if master_metadata is not None:
222230
try:
223-
master_metadata = validate_and_sanitise(master_metadata, session_id)
231+
master_metadata = validate_and_sanitise(master_metadata, session_id, db)
224232
clem_lif_file.master_metadata = str(master_metadata)
225233
except Exception:
226234
logger.warning(traceback.format_exc())
@@ -621,21 +629,23 @@ def register_image_stack(
621629
@router.post("/sessions/{session_id}/lif_to_stack") # API posts to this URL
622630
def lif_to_stack(
623631
session_id: int, # Used by the decorator
624-
lif_info: LifFileInfo,
632+
lif_file: Path,
625633
):
626634
# Get command line entry point
627635
murfey_workflows = entry_points().select(
628636
group="murfey.workflows", name="lif_to_stack"
629637
)
630638

631639
# Use entry point if found
632-
if murfey_workflows:
633-
murfey_workflows[0].load()(
640+
if len(murfey_workflows) == 1:
641+
workflow: EntryPoint = list(murfey_workflows)[0]
642+
workflow.load()(
634643
# Match the arguments found in murfey.workflows.lif_to_stack
635-
file=lif_info.name,
644+
file=lif_file,
636645
root_folder="images",
637646
messenger=_transport_object,
638647
)
648+
return True
639649
# Raise error if Murfey workflow not found
640650
else:
641651
raise RuntimeError("The relevant Murfey workflow was not found")
@@ -653,7 +663,8 @@ def tiff_to_stack(
653663

654664
# Use entry point if found
655665
if murfey_workflows:
656-
murfey_workflows[0].load()(
666+
workflow: EntryPoint = list(murfey_workflows)[0]
667+
workflow.load()(
657668
# Match the arguments found in murfey.workflows.tiff_to_stack
658669
file=tiff_info.tiff_files[0], # Pass it only one file from the list
659670
root_folder="images",

0 commit comments

Comments
 (0)