Code formatting

This commit is contained in:
Joel Collins 2020-10-13 15:02:19 +01:00
parent 0234b20ce3
commit 2bfb988460
60 changed files with 392 additions and 396 deletions

View file

@ -1,8 +1,9 @@
#!/usr/bin/env python
import atexit
import logging
import logging.handlers
import sys
import time
import atexit
import logging, logging.handlers
# Look for debug flag
if "-d" in sys.argv or "--debug" in sys.argv:
@ -17,33 +18,27 @@ root_log.setLevel(log_level)
import os
import pkg_resources
from flask import Flask, send_file, abort
from datetime import datetime
import pkg_resources
from flask import Flask, abort, send_file
from flask_cors import CORS, cross_origin
from openflexure_microscope.api.utilities import list_routes, init_default_extensions
from openflexure_microscope.config import JSONEncoder
from openflexure_microscope.paths import (
OPENFLEXURE_VAR_PATH,
OPENFLEXURE_EXTENSIONS_PATH,
settings_file_path,
logs_file_path,
)
from labthings import create_app
from labthings.extensions import find_extensions
from openflexure_microscope.api.microscope import default_microscope as api_microscope
from openflexure_microscope.api.utilities import init_default_extensions, list_routes
from openflexure_microscope.api.v2 import views
from openflexure_microscope.config import JSONEncoder
from openflexure_microscope.paths import (
OPENFLEXURE_EXTENSIONS_PATH,
OPENFLEXURE_VAR_PATH,
logs_file_path,
settings_file_path,
)
# Handle logging
access_log = logging.getLogger('werkzeug')
access_log = logging.getLogger("werkzeug")
# Block the access logs from propagating up to the root logger
access_log.propagate = False
@ -207,6 +202,7 @@ atexit.register(cleanup)
# Start the app
if __name__ == "__main__":
from labthings import Server
logging.info("Starting OpenFlexure Microscope Server...")
server = Server(app)
server.run(host="0.0.0.0", port=5000, debug=False, zeroconf=True)

View file

@ -1,18 +1,16 @@
from labthings import fields, find_component, current_action
from labthings.extensions import BaseExtension
from labthings.views import View, ActionView, PropertyView
from openflexure_microscope.devel import JsonResponse, request, abort
from openflexure_microscope.utilities import set_properties
import time
import numpy as np
import logging
from scipy import ndimage
import time
from contextlib import contextmanager
from threading import Event, Thread
from threading import Thread, Event
import numpy as np
from labthings import current_action, fields, find_component
from labthings.extensions import BaseExtension
from labthings.views import ActionView, PropertyView, View
from scipy import ndimage
from openflexure_microscope.devel import JsonResponse, abort, request
from openflexure_microscope.utilities import set_properties
### Autofocus utilities
@ -447,4 +445,3 @@ autofocus_extension_v2.add_view(
autofocus_extension_v2.add_view(
MoveAndMeasureAPI, "/move_and_measure", endpoint="move_and_measure"
)

View file

@ -1,21 +1,18 @@
from labthings.extensions import BaseExtension
from labthings.views import View, PropertyView
from labthings import fields, find_component
from openflexure_microscope.paths import settings_file_path, check_rw
from openflexure_microscope.config import OpenflexureSettingsFile
from openflexure_microscope.captures.capture_manager import BASE_CAPTURE_PATH
from openflexure_microscope.captures.capture import build_captures_from_exif
from openflexure_microscope.api.utilities.gui import build_gui
from flask import abort
import logging
import os
import psutil
from sys import platform
import psutil
from flask import abort
from labthings import fields, find_component
from labthings.extensions import BaseExtension
from labthings.views import PropertyView, View
from openflexure_microscope.api.utilities.gui import build_gui
from openflexure_microscope.captures.capture import build_captures_from_exif
from openflexure_microscope.captures.capture_manager import BASE_CAPTURE_PATH
from openflexure_microscope.config import OpenflexureSettingsFile
from openflexure_microscope.paths import check_rw, settings_file_path
AS_SETTINGS_PATH = settings_file_path("autostorage_settings.json")

View file

@ -1,19 +1,18 @@
from labthings import find_component
from labthings.views import View, ActionView
from labthings.extensions import BaseExtension
from flask import abort
from contextlib import contextmanager
import logging
from contextlib import contextmanager
# Type hinting
from typing import Tuple
from flask import abort
from labthings import find_component
from labthings.extensions import BaseExtension
from labthings.views import ActionView, View
from .recalibrate_utils import (
recalibrate_camera,
auto_expose_and_freeze_settings,
flat_lens_shading_table,
recalibrate_camera,
)
@ -125,5 +124,9 @@ lst_extension_v2.add_method(
)
lst_extension_v2.add_view(RecalibrateView, "/recalibrate", endpoint="recalibrate")
lst_extension_v2.add_view(FlattenLSTView, "/flatten_lens_shading_table", endpoint="flatten_lens_shading_table")
lst_extension_v2.add_view(DeleteLSTView, "/delete_lens_shading_table", endpoint="delete_lens_shading_table")
lst_extension_v2.add_view(
FlattenLSTView, "/flatten_lens_shading_table", endpoint="flatten_lens_shading_table"
)
lst_extension_v2.add_view(
DeleteLSTView, "/delete_lens_shading_table", endpoint="delete_lens_shading_table"
)

View file

@ -1,9 +1,9 @@
import numpy as np
import time
import logging
import time
import numpy as np
from picamerax import PiCamera
from picamerax.array import PiRGBArray, PiBayerArray
from picamerax.array import PiBayerArray, PiRGBArray
def rgb_image(camera, resize=None, **kwargs):

View file

@ -1,21 +1,24 @@
import datetime
import itertools
import logging
import time
import uuid
import datetime
from typing import Tuple
from functools import reduce
from typing import Tuple
from labthings import (
current_action,
fields,
find_component,
find_extension,
update_action_progress,
)
from labthings.extensions import BaseExtension
from labthings.views import ActionView, View
from openflexure_microscope.captures.capture_manager import generate_basename
from labthings import fields, find_component, find_extension, current_action, update_action_progress
from labthings.views import View, ActionView
from labthings.extensions import BaseExtension
from openflexure_microscope.devel import abort
import time
### Grid construction
@ -56,9 +59,6 @@ def flatten_grid(grid):
### Capturing
class ScanExtension(BaseExtension):
def __init__(self):
self._images_to_be_captured: int = 1
@ -83,7 +83,12 @@ class ScanExtension(BaseExtension):
if namemode == "coordinates":
filename = "{}_{}_{}_{}".format(basename, *microscope.stage.position)
else:
filename = "{}_{}".format(basename, str(self._images_captured_so_far).zfill(len(str(self._images_to_be_captured))))
filename = "{}_{}".format(
basename,
str(self._images_captured_so_far).zfill(
len(str(self._images_to_be_captured))
),
)
folder = "SCAN_{}".format(basename)
# Do capture
@ -97,14 +102,14 @@ class ScanExtension(BaseExtension):
annotations=annotations,
tags=tags,
metadata=metadata,
cache_key=folder
cache_key=folder,
)
def progress(self):
progress = (self._images_captured_so_far / self._images_to_be_captured) * 100
logging.info(progress)
return progress
### Scanning
def tile(
self,
@ -164,7 +169,9 @@ class ScanExtension(BaseExtension):
autofocus_enabled = False
# Construct an x-y grid (worry about z later)
x_y_grid = construct_grid(initial_position, stride_size[:2], grid[:2], style=style)
x_y_grid = construct_grid(
initial_position, stride_size[:2], grid[:2], style=style
)
# Keep the initial Z position the same as our current position
initial_z = initial_position[2]
@ -245,7 +252,6 @@ class ScanExtension(BaseExtension):
end = time.time()
logging.info(f"Scan took {end - start} seconds")
def stack(
self,
microscope,
@ -304,6 +310,7 @@ class ScanExtension(BaseExtension):
scan_extension_v2 = ScanExtension()
class TileScanAPI(ActionView):
args = {
"filename": fields.String(missing=None, example=None),
@ -365,4 +372,5 @@ class TileScanAPI(ActionView):
tags=args.get("tags"),
)
scan_extension_v2.add_view(TileScanAPI, "/tile", endpoint="tile")

View file

@ -1,21 +1,17 @@
from openflexure_microscope.devel import (
JsonResponse,
request,
)
from flask import send_file, abort, url_for
import uuid
import os
import zipfile
import tempfile
import logging
import os
import tempfile
import uuid
import zipfile
from flask import abort, send_file, url_for
from labthings import fields, find_component, update_action_progress
from labthings.views import View, ActionView, PropertyView
from labthings.schema import Schema, pre_dump
from labthings.extensions import BaseExtension
from labthings.schema import Schema, pre_dump
from labthings.utilities import description_from_view
from labthings.views import ActionView, PropertyView, View
from openflexure_microscope.devel import JsonResponse, request
class ZipObjectSchema(Schema):
@ -137,9 +133,7 @@ class ZipBuilderAPIView(ActionView):
microscope = find_component("org.openflexure.microscope")
# Return a handle on the autofocus task
return default_zip_manager.marshaled_build_zip_from_capture_ids(
microscope, ids
)
return default_zip_manager.marshaled_build_zip_from_capture_ids(microscope, ids)
class ZipListAPIView(PropertyView):
@ -191,7 +185,9 @@ zip_extension_v2 = BaseExtension(
description="Build and download capture collections as ZIP files",
)
zip_extension_v2.add_view(ZipGetterAPIView, "/get/<string:session_id>", endpoint="get_id")
zip_extension_v2.add_view(
ZipGetterAPIView, "/get/<string:session_id>", endpoint="get_id"
)
zip_extension_v2.add_view(ZipListAPIView, "/get", endpoint="get")
zip_extension_v2.add_view(ZipBuilderAPIView, "/build", endpoint="build")

View file

@ -1 +1 @@
from .tools import devtools_extension_v2
from .tools import devtools_extension_v2

View file

@ -1,14 +1,16 @@
import logging
import time
from labthings import fields
from labthings.extensions import BaseExtension
from labthings.views import ActionView
import logging
import time
class RaiseException(ActionView):
def post(self):
raise Exception("The developer raised an exception")
class SleepFor(ActionView):
schema = {"TimeAsleep": fields.Float()}
args = {"time": fields.Float(description="Time to sleep, in seconds", example=0.5)}
@ -22,6 +24,7 @@ class SleepFor(ActionView):
logging.info("Waking up!")
return {"TimeAsleep": (end - start)}
devtools_extension_v2 = BaseExtension(
"org.openflexure.dev.tools",
version="0.1.0",
@ -29,4 +32,4 @@ devtools_extension_v2 = BaseExtension(
)
devtools_extension_v2.add_view(RaiseException, "/raise")
devtools_extension_v2.add_view(SleepFor, "/sleep")
devtools_extension_v2.add_view(SleepFor, "/sleep")

View file

@ -1,7 +1,6 @@
from openflexure_microscope.microscope import Microscope
import logging
from openflexure_microscope.microscope import Microscope
default_microscope = Microscope()

View file

@ -1,8 +1,9 @@
import errno
import logging
import os
import errno
from flask import Blueprint, current_app, url_for
from werkzeug.exceptions import BadRequest
from flask import url_for, Blueprint, current_app
from . import gui

View file

@ -1,5 +1,5 @@
from .actions import enabled_root_actions
from .captures import *
from .instrument import *
from .streams import *
from .stage import *
from .streams import *

View file

@ -2,11 +2,11 @@
Top-level representation of enabled actions
"""
from flask import url_for
from . import camera, stage, system
from labthings import current_labthing
from labthings.views import View
from labthings.utilities import description_from_view
from labthings.views import View
from . import camera, stage, system
_actions = {
"capture": {

View file

@ -1,14 +1,13 @@
from openflexure_microscope.api.utilities import get_bool, JsonResponse
from labthings.views import View, ActionView
from labthings import find_component, fields
from openflexure_microscope.utilities import filter_dict
from openflexure_microscope.api.v2.views.captures import CaptureSchema
import logging
import io
from flask import request, abort, url_for, redirect, send_file
import logging
from flask import abort, redirect, request, send_file, url_for
from labthings import fields, find_component
from labthings.views import ActionView, View
from openflexure_microscope.api.utilities import JsonResponse, get_bool
from openflexure_microscope.api.v2.views.captures import CaptureSchema
from openflexure_microscope.utilities import filter_dict
class CaptureAPI(ActionView):
@ -58,11 +57,10 @@ class CaptureAPI(ActionView):
resize=resize,
bayer=args.get("bayer"),
annotations=args.get("annotations"),
tags=args.get("tags")
tags=args.get("tags"),
)
class RAMCaptureAPI(ActionView):
"""
Take a non-persistant image capture.
@ -77,12 +75,8 @@ class RAMCaptureAPI(ActionView):
missing=None, example={"width": 640, "height": 480}
), # TODO: Validate keys
}
responses = {
200: {
"content_type": "image/jpeg"
}
}
responses = {200: {"content_type": "image/jpeg"}}
def post(self, args):
"""

View file

@ -1,12 +1,11 @@
from openflexure_microscope.api.utilities import JsonResponse
from labthings.views import View, ActionView
from labthings import find_component, fields
from openflexure_microscope.utilities import axes_to_array, filter_dict
import logging
from flask import Blueprint, request
from labthings import fields, find_component
from labthings.views import ActionView, View
import logging
from openflexure_microscope.api.utilities import JsonResponse
from openflexure_microscope.utilities import axes_to_array, filter_dict
class MoveStageAPI(ActionView):
@ -65,5 +64,3 @@ class ZeroStageAPI(ActionView):
# TODO: Make schema for microscope state
return microscope.state["stage"]

View file

@ -1,8 +1,9 @@
from labthings.views import View, ActionView
import subprocess
import os
import subprocess
from sys import platform
from labthings.views import ActionView, View
def is_raspberrypi(raise_on_errors=False):
"""

View file

@ -1,15 +1,14 @@
import logging
from flask import abort, request, redirect, url_for, send_file
from openflexure_microscope.api.utilities import get_bool, JsonResponse
from flask import abort, redirect, request, send_file, url_for
from labthings import Schema, fields, find_component
from labthings.views import View, PropertyView
from labthings.utilities import description_from_view
from labthings.marshalling import marshal_with
from labthings.utilities import description_from_view
from labthings.views import PropertyView, View
from marshmallow import pre_dump
from openflexure_microscope.api.utilities import JsonResponse, get_bool
class InstrumentSchema(Schema):
id = fields.UUID()
@ -130,11 +129,7 @@ class CaptureView(View):
class CaptureDownload(View):
tags = ["captures"]
responses = {
200: {
"content_type": "image/jpeg"
}
}
responses = {200: {"content_type": "image/jpeg"}}
def get(self, id, filename):
"""

View file

@ -1,12 +1,12 @@
from openflexure_microscope.api.utilities import JsonResponse
from labthings import find_component
from labthings.views import View, PropertyView
from labthings.utilities import get_by_path, set_by_path, create_from_path
from flask import request, abort
import logging
from flask import abort, request
from labthings import find_component
from labthings.utilities import create_from_path, get_by_path, set_by_path
from labthings.views import PropertyView, View
from openflexure_microscope.api.utilities import JsonResponse
class SettingsProperty(PropertyView):
def get(self):
@ -34,9 +34,7 @@ class SettingsProperty(PropertyView):
class NestedSettingsProperty(View):
tags = ["properties"]
responses = {
404: {"description": "Settings key cannot be found"}
}
responses = {404: {"description": "Settings key cannot be found"}}
def get(self, route):
"""
@ -80,9 +78,7 @@ class StateProperty(PropertyView):
class NestedStateProperty(View):
tags = ["properties"]
responses = {
404: {"description": "Status key cannot be found"}
}
responses = {404: {"description": "Status key cannot be found"}}
def get(self, route):
"""
@ -110,9 +106,7 @@ class ConfigurationProperty(PropertyView):
class NestedConfigurationProperty(View):
tags = ["properties"]
responses = {
404: {"description": "Status key cannot be found"}
}
responses = {404: {"description": "Status key cannot be found"}}
def get(self, route):
"""

View file

@ -1,8 +1,8 @@
from labthings import find_component, fields, schema
from labthings.views import PropertyView
import logging
from labthings import fields, find_component, schema
from labthings.views import PropertyView
class StageTypeProperty(PropertyView):
"""The type of the stage"""

View file

@ -1,21 +1,17 @@
from openflexure_microscope.api.utilities import gen, JsonResponse
from labthings import find_component
from labthings.utilities import get_by_path, set_by_path, create_from_path
from labthings.views import View, PropertyView
from flask import Response
from labthings import find_component
from labthings.utilities import create_from_path, get_by_path, set_by_path
from labthings.views import PropertyView, View
from openflexure_microscope.api.utilities import JsonResponse, gen
class MjpegStream(PropertyView):
"""
Real-time MJPEG stream from the microscope camera
"""
responses = {
200: {
"content_type": "multipart/x-mixed-replace"
}
}
responses = {200: {"content_type": "multipart/x-mixed-replace"}}
def get(self):
"""
@ -44,12 +40,7 @@ class SnapshotStream(PropertyView):
Single JPEG snapshot from the camera stream
"""
responses = {
200: {
"content_type": "image/jpeg",
"description": "Snapshot taken"
}
}
responses = {200: {"content_type": "image/jpeg", "description": "Snapshot taken"}}
def get(self):
"""