Commit f2588a50 authored by jtc42's avatar jtc42
Browse files

Add more complete Capture object schemas

parent 2d6ead5f
Loading
Loading
Loading
Loading
+23 −1
Original line number Diff line number Diff line
@@ -14,6 +14,28 @@ from labthings.server.find import find_component
from marshmallow import pre_dump


class InstrumentSchema(Schema):
    id = fields.UUID()
    configuration = fields.Dict()
    settings = fields.Dict()
    state = fields.Dict()

class CaptureMetadataImageSchema(Schema):
    id = fields.UUID()
    acquisitionDate = fields.String(format="date")
    format = fields.String()
    name = fields.String()
    tags = fields.List(fields.String())
    annotations = fields.Dict()


class CaptureMetadataSchema(Schema):
    experimenter = fields.Dict()  # TODO: Make schema
    experimenterGroup = fields.Dict()  # TODO: Make schema
    dataset = fields.Dict()  # TODO: Make schema
    image = fields.Nested(CaptureMetadataImageSchema())
    instrument = fields.Nested(InstrumentSchema())

class CaptureSchema(Schema):
    id = fields.String()
    file = fields.String(
@@ -21,7 +43,7 @@ class CaptureSchema(Schema):
    )
    exists = fields.Bool(data_key="available")
    name = fields.String()
    metadata = fields.Dict()
    metadata = fields.Nested(CaptureMetadataSchema())

    links = fields.Dict()

+9 −7
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ import os
import shutil
import glob
import datetime
import yaml
import json
import logging
from PIL import Image
@@ -35,11 +34,9 @@ def pull_usercomment_dict(filepath):
        try:
            return json.loads(exif_dict["Exif"][37510].decode())
        except json.decoder.JSONDecodeError:
            # TODO: Remove YAML support in a later version
            logging.warning(
                f"Capture {filepath} has metadata stored in YAML format. This is now deprecated in favour of JSON."
            logging.error(
                f"Capture {filepath} has old, corrupt, or missing OpenFlexure metadata. Unable to reload to server."
            )
            return yaml.load(exif_dict["Exif"][37510].decode())
    else:
        return None

@@ -68,6 +65,7 @@ def build_captures_from_exif(capture_path):
        exif = pull_usercomment_dict(f)
        if exif:
            capture = capture_from_exif(f, exif)
            if capture:
                captures.append(capture)
        else:
            logging.error("Invalid data at {}. Skipping.".format(f))
@@ -95,7 +93,11 @@ def capture_from_exif(path, exif_dict):
    capture.split_file_path(capture.file)

    # Image metadata
    try:
        image_metadata = exif_dict.pop("image")
    except KeyError as e:
        logging.error(f"Unable to obtain OpenFlexure metadata from file {path}")
        return None

    # Populate capture parameters
    capture.id = image_metadata.get("id")