Loading openflexure_microscope/__init__.py +1 −1 Original line number Diff line number Diff line __all__ = ['Microscope', 'config', 'task', 'lock', 'utilities'] __all__ = ["Microscope", "config", "task", "lock", "utilities"] __version__ = "0.1.0" from .microscope import Microscope Loading openflexure_microscope/api/__init__.py +1 −1 Original line number Diff line number Diff line __all__ = ['utilities'] __all__ = ["utilities"] from . import utilities openflexure_microscope/api/app.py +20 −27 Original line number Diff line number Diff line Loading @@ -6,8 +6,7 @@ import logging import sys import os from flask import ( Flask, jsonify, send_file) from flask import Flask, jsonify, send_file from serial import SerialException from datetime import datetime Loading Loading @@ -39,7 +38,7 @@ from openflexure_microscope.stage.mock import MockStage # Handle logging is_gunicorn = "gunicorn" in os.environ.get("SERVER_SOFTWARE", "") DEFAULT_LOGFILE = os.path.join(USER_CONFIG_DIR, 'openflexure_microscope.log') DEFAULT_LOGFILE = os.path.join(USER_CONFIG_DIR, "openflexure_microscope.log") if (__name__ == "__main__") or (not is_gunicorn): # If imported, but not by gunicorn Loading @@ -48,18 +47,15 @@ if (__name__ == "__main__") or (not is_gunicorn): else: # Direct standard Python logging to file and console root = logging.getLogger() error_formatter = logging.Formatter("[%(asctime)s] [%(threadName)s] [%(levelname)s] %(message)s") error_formatter = logging.Formatter( "[%(asctime)s] [%(threadName)s] [%(levelname)s] %(message)s" ) rotating_logfile = logging.handlers.RotatingFileHandler( DEFAULT_LOGFILE, maxBytes=1000000, backupCount=7 DEFAULT_LOGFILE, maxBytes=1000000, backupCount=7 ) error_handlers = [ rotating_logfile, logging.StreamHandler() ] error_handlers = [rotating_logfile, logging.StreamHandler()] for handler in error_handlers: handler.setFormatter(error_formatter) Loading Loading @@ -88,7 +84,7 @@ def uri(suffix, api_version, base=None): app = Flask(__name__) app.url_map.strict_slashes = False CORS(app, resources=r'/api/*') CORS(app, resources=r"/api/*") # Make errors more API friendly handler = JSONExceptionHandler(app) Loading Loading @@ -121,10 +117,7 @@ def attach_microscope(): # Attach devices to microscope logging.debug("Attaching devices to microscope...") api_microscope.attach( api_camera, api_stage ) api_microscope.attach(api_camera, api_stage) # Restore loaded capture array to camera object logging.debug("Restoring captures...") Loading @@ -140,26 +133,26 @@ def attach_microscope(): # Base routes base_blueprint = blueprints.base.construct_blueprint(api_microscope) app.register_blueprint(base_blueprint, url_prefix=uri('', 'v1')) app.register_blueprint(base_blueprint, url_prefix=uri("", "v1")) # Stage routes stage_blueprint = blueprints.stage.construct_blueprint(api_microscope) app.register_blueprint(stage_blueprint, url_prefix=uri('/stage', 'v1')) app.register_blueprint(stage_blueprint, url_prefix=uri("/stage", "v1")) # Camera routes camera_blueprint = blueprints.camera.construct_blueprint(api_microscope) app.register_blueprint(camera_blueprint, url_prefix=uri('/camera', 'v1')) app.register_blueprint(camera_blueprint, url_prefix=uri("/camera", "v1")) # Plugin routes plugin_blueprint = blueprints.plugins.construct_blueprint(api_microscope) app.register_blueprint(plugin_blueprint, url_prefix=uri('/plugin', 'v1')) app.register_blueprint(plugin_blueprint, url_prefix=uri("/plugin", "v1")) # Task routes task_blueprint = blueprints.task.construct_blueprint(api_microscope) app.register_blueprint(task_blueprint, url_prefix=uri('/task', 'v1')) app.register_blueprint(task_blueprint, url_prefix=uri("/task", "v1")) @app.route('/routes') @app.route("/routes") def routes(): """ List of all connected API routes Loading @@ -173,7 +166,7 @@ def routes(): return jsonify(list_routes(app)) @app.route('/log') @app.route("/log") def err_log(): """ Most recent 1mb of log output Loading @@ -188,7 +181,7 @@ def err_log(): return send_file( DEFAULT_LOGFILE, as_attachment=True, attachment_filename='openflexure_microscope_{}.log'.format(timestamp) attachment_filename="openflexure_microscope_{}.log".format(timestamp), ) Loading Loading @@ -219,4 +212,4 @@ def cleanup(): atexit.register(cleanup) if __name__ == "__main__": app.run(host='0.0.0.0', port="5000", threaded=True, debug=True, use_reloader=False) app.run(host="0.0.0.0", port="5000", threaded=True, debug=True, use_reloader=False) openflexure_microscope/api/exceptions.py +2 −5 Original line number Diff line number Diff line Loading @@ -17,17 +17,14 @@ class JSONExceptionHandler(object): def std_handler(self, error): if isinstance(error, HTTPException): message = error.description elif hasattr(error, 'message'): elif hasattr(error, "message"): message = error.message else: message = str(error) status_code = error.code if isinstance(error, HTTPException) else 500 response = { 'status_code': status_code, 'message': escape(message) } response = {"status_code": status_code, "message": escape(message)} return jsonify(response), status_code def init_app(self, app): Loading openflexure_microscope/api/utilities.py +8 −12 Original line number Diff line number Diff line Loading @@ -10,7 +10,9 @@ class JsonResponse: """ # Try to load as json try: self.json = request.get_json() #: dict: Dictionary representation of request JSON self.json = ( request.get_json() ) #: dict: Dictionary representation of request JSON # If malformed JSON is passed, make an empty dictionary except BadRequest as e: logging.error(e) Loading Loading @@ -55,15 +57,12 @@ def gen(camera): # the obtained frame is a jpeg frame = camera.get_frame() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') yield (b"--frame\r\n" b"Content-Type: image/jpeg\r\n\r\n" + frame + b"\r\n") def get_bool(get_arg): """Convert GET request argument string to a Python bool""" if (get_arg == 'true' or get_arg == 'True' or get_arg == '1'): if get_arg == "true" or get_arg == "True" or get_arg == "1": return True else: return False Loading @@ -80,10 +79,7 @@ def list_routes(app): endpoint = rule.endpoint methods = list(rule.methods) url = url_for(rule.endpoint, **options) line = { 'endpoint': endpoint, 'methods': methods } line = {"endpoint": endpoint, "methods": methods} output[url] = line return output Loading
openflexure_microscope/__init__.py +1 −1 Original line number Diff line number Diff line __all__ = ['Microscope', 'config', 'task', 'lock', 'utilities'] __all__ = ["Microscope", "config", "task", "lock", "utilities"] __version__ = "0.1.0" from .microscope import Microscope Loading
openflexure_microscope/api/__init__.py +1 −1 Original line number Diff line number Diff line __all__ = ['utilities'] __all__ = ["utilities"] from . import utilities
openflexure_microscope/api/app.py +20 −27 Original line number Diff line number Diff line Loading @@ -6,8 +6,7 @@ import logging import sys import os from flask import ( Flask, jsonify, send_file) from flask import Flask, jsonify, send_file from serial import SerialException from datetime import datetime Loading Loading @@ -39,7 +38,7 @@ from openflexure_microscope.stage.mock import MockStage # Handle logging is_gunicorn = "gunicorn" in os.environ.get("SERVER_SOFTWARE", "") DEFAULT_LOGFILE = os.path.join(USER_CONFIG_DIR, 'openflexure_microscope.log') DEFAULT_LOGFILE = os.path.join(USER_CONFIG_DIR, "openflexure_microscope.log") if (__name__ == "__main__") or (not is_gunicorn): # If imported, but not by gunicorn Loading @@ -48,18 +47,15 @@ if (__name__ == "__main__") or (not is_gunicorn): else: # Direct standard Python logging to file and console root = logging.getLogger() error_formatter = logging.Formatter("[%(asctime)s] [%(threadName)s] [%(levelname)s] %(message)s") error_formatter = logging.Formatter( "[%(asctime)s] [%(threadName)s] [%(levelname)s] %(message)s" ) rotating_logfile = logging.handlers.RotatingFileHandler( DEFAULT_LOGFILE, maxBytes=1000000, backupCount=7 DEFAULT_LOGFILE, maxBytes=1000000, backupCount=7 ) error_handlers = [ rotating_logfile, logging.StreamHandler() ] error_handlers = [rotating_logfile, logging.StreamHandler()] for handler in error_handlers: handler.setFormatter(error_formatter) Loading Loading @@ -88,7 +84,7 @@ def uri(suffix, api_version, base=None): app = Flask(__name__) app.url_map.strict_slashes = False CORS(app, resources=r'/api/*') CORS(app, resources=r"/api/*") # Make errors more API friendly handler = JSONExceptionHandler(app) Loading Loading @@ -121,10 +117,7 @@ def attach_microscope(): # Attach devices to microscope logging.debug("Attaching devices to microscope...") api_microscope.attach( api_camera, api_stage ) api_microscope.attach(api_camera, api_stage) # Restore loaded capture array to camera object logging.debug("Restoring captures...") Loading @@ -140,26 +133,26 @@ def attach_microscope(): # Base routes base_blueprint = blueprints.base.construct_blueprint(api_microscope) app.register_blueprint(base_blueprint, url_prefix=uri('', 'v1')) app.register_blueprint(base_blueprint, url_prefix=uri("", "v1")) # Stage routes stage_blueprint = blueprints.stage.construct_blueprint(api_microscope) app.register_blueprint(stage_blueprint, url_prefix=uri('/stage', 'v1')) app.register_blueprint(stage_blueprint, url_prefix=uri("/stage", "v1")) # Camera routes camera_blueprint = blueprints.camera.construct_blueprint(api_microscope) app.register_blueprint(camera_blueprint, url_prefix=uri('/camera', 'v1')) app.register_blueprint(camera_blueprint, url_prefix=uri("/camera", "v1")) # Plugin routes plugin_blueprint = blueprints.plugins.construct_blueprint(api_microscope) app.register_blueprint(plugin_blueprint, url_prefix=uri('/plugin', 'v1')) app.register_blueprint(plugin_blueprint, url_prefix=uri("/plugin", "v1")) # Task routes task_blueprint = blueprints.task.construct_blueprint(api_microscope) app.register_blueprint(task_blueprint, url_prefix=uri('/task', 'v1')) app.register_blueprint(task_blueprint, url_prefix=uri("/task", "v1")) @app.route('/routes') @app.route("/routes") def routes(): """ List of all connected API routes Loading @@ -173,7 +166,7 @@ def routes(): return jsonify(list_routes(app)) @app.route('/log') @app.route("/log") def err_log(): """ Most recent 1mb of log output Loading @@ -188,7 +181,7 @@ def err_log(): return send_file( DEFAULT_LOGFILE, as_attachment=True, attachment_filename='openflexure_microscope_{}.log'.format(timestamp) attachment_filename="openflexure_microscope_{}.log".format(timestamp), ) Loading Loading @@ -219,4 +212,4 @@ def cleanup(): atexit.register(cleanup) if __name__ == "__main__": app.run(host='0.0.0.0', port="5000", threaded=True, debug=True, use_reloader=False) app.run(host="0.0.0.0", port="5000", threaded=True, debug=True, use_reloader=False)
openflexure_microscope/api/exceptions.py +2 −5 Original line number Diff line number Diff line Loading @@ -17,17 +17,14 @@ class JSONExceptionHandler(object): def std_handler(self, error): if isinstance(error, HTTPException): message = error.description elif hasattr(error, 'message'): elif hasattr(error, "message"): message = error.message else: message = str(error) status_code = error.code if isinstance(error, HTTPException) else 500 response = { 'status_code': status_code, 'message': escape(message) } response = {"status_code": status_code, "message": escape(message)} return jsonify(response), status_code def init_app(self, app): Loading
openflexure_microscope/api/utilities.py +8 −12 Original line number Diff line number Diff line Loading @@ -10,7 +10,9 @@ class JsonResponse: """ # Try to load as json try: self.json = request.get_json() #: dict: Dictionary representation of request JSON self.json = ( request.get_json() ) #: dict: Dictionary representation of request JSON # If malformed JSON is passed, make an empty dictionary except BadRequest as e: logging.error(e) Loading Loading @@ -55,15 +57,12 @@ def gen(camera): # the obtained frame is a jpeg frame = camera.get_frame() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') yield (b"--frame\r\n" b"Content-Type: image/jpeg\r\n\r\n" + frame + b"\r\n") def get_bool(get_arg): """Convert GET request argument string to a Python bool""" if (get_arg == 'true' or get_arg == 'True' or get_arg == '1'): if get_arg == "true" or get_arg == "True" or get_arg == "1": return True else: return False Loading @@ -80,10 +79,7 @@ def list_routes(app): endpoint = rule.endpoint methods = list(rule.methods) url = url_for(rule.endpoint, **options) line = { 'endpoint': endpoint, 'methods': methods } line = {"endpoint": endpoint, "methods": methods} output[url] = line return output