Updated documentation to subclass-based extensions

This commit is contained in:
Joel Collins 2020-11-30 16:23:28 +00:00
parent 7a849c725a
commit bacc111a87
10 changed files with 249 additions and 318 deletions

View file

@ -2,42 +2,44 @@ from labthings import fields, find_component
from labthings.extensions import BaseExtension
from labthings.views import View
## Extension methods
# Create the extension class
class MyExtension(BaseExtension):
def __init__(self):
# Superclass init function
super().__init__("com.myname.myextension", version="0.0.0")
# Add our API Views (defined below MyExtension)
self.add_view(ExampleIdentifyView, "/identify")
self.add_view(ExampleRenameView, "/rename")
def identify(microscope):
"""
Demonstrate access to Microscope.camera, and Microscope.stage
"""
def identify(self, microscope):
"""
Demonstrate access to Microscope.camera, and Microscope.stage
"""
response = (
f"My name is {microscope.name}. "
f"My parent camera is {microscope.camera}, "
f"and my parent stage is {microscope.stage}."
)
response = (
f"My name is {microscope.name}. "
f"My parent camera is {microscope.camera}, "
f"and my parent stage is {microscope.stage}."
)
return response
return response
def rename(microscope, new_name):
"""
Rename the microscope
"""
microscope.name = new_name
microscope.save_settings()
def rename(self, microscope, new_name):
"""
Rename the microscope
"""
microscope.name = new_name
microscope.save_settings()
## Extension views
class ExampleIdentifyView(View):
def get(self):
# Find our microscope component
microscope = find_component("org.openflexure.microscope")
# Return our identify function's output
return identify(microscope)
return self.extension.identify(microscope)
class ExampleRenameView(View):
@ -53,21 +55,10 @@ class ExampleRenameView(View):
microscope = find_component("org.openflexure.microscope")
# Pass microscope and new name to our rename function
rename(microscope, new_name)
self.extension.rename(microscope, new_name)
# Return our identify function's output
return identify(microscope)
return self.extension.identify(microscope)
## Create extension
# Create your extension object
my_extension = BaseExtension("com.myname.myextension", version="0.0.0")
# Add methods to your extension
my_extension.add_method(identify, "identify")
my_extension.add_method(rename, "rename")
# Add API views to your extension
my_extension.add_view(ExampleIdentifyView, "/identify")
my_extension.add_view(ExampleRenameView, "/rename")
LABTHINGS_EXTENSIONS = (MyExtension,)