Commit bacc111a authored by Joel Collins's avatar Joel Collins
Browse files

Updated documentation to subclass-based extensions

parent 7a849c72
Loading
Loading
Loading
Loading
+24 −24
Original line number Diff line number Diff line
@@ -2,7 +2,13 @@ from labthings import find_component
from labthings.extensions import BaseExtension


def identify():
# Create the extension class
class MyExtension(BaseExtension):
    def __init__(self):
        # Superclass init function
        super().__init__("com.myname.myextension", version="0.0.0")

    def identify(self):
        """
        Demonstrate access to Microscope.camera, and Microscope.stage
        """
@@ -16,8 +22,7 @@ def identify():

        return response


def rename(new_name):
    def rename(self, new_name):
        """
        Rename the microscope
        """
@@ -28,9 +33,4 @@ def rename(new_name):
        microscope.save_settings()


# 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")
LABTHINGS_EXTENSIONS = (MyExtension,)
+0 −41
Original line number Diff line number Diff line
from labthings import find_component
from labthings.extensions import BaseExtension


# Create the extension class
class MyExtension(BaseExtension):
    def __init__(self):

        # Create some instance variable
        self.state_variable = "An example of a persistant instance variable"

        # Superclass init function
        super().__init__("com.myname.myextension", version="0.0.0")

    def identify(self):
        """
        Demonstrate access to Microscope.camera, and Microscope.stage
        """
        microscope = find_component("org.openflexure.microscope")

        response = (
            f"My name is {microscope.name}. "
            f"My parent camera is {microscope.camera}, "
            f"and my parent stage is {microscope.stage}."
        )

        return response

    def rename(self, new_name):
        """
        Rename the microscope
        """

        microscope = find_component("org.openflexure.microscope")

        microscope.name = new_name
        microscope.save_settings()


# Create your extension object
my_extension = MyExtension()
+32 −41
Original line number Diff line number Diff line
@@ -2,14 +2,20 @@ 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):
    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}, "
@@ -18,26 +24,22 @@ def identify(microscope):

        return response


def rename(microscope, new_name):
    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)


## Create extension

# Create your extension object
my_extension = BaseExtension("com.myname.myextension", version="0.0.0")
        return self.extension.identify(microscope)

# 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,)
+18 −24
Original line number Diff line number Diff line
@@ -2,7 +2,22 @@ from labthings import Schema, 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 rename(self, microscope, new_name):
        """
        Rename the microscope
        """
        microscope.name = new_name
        microscope.save_settings()


# Define which properties of a Microscope object we care about,
@@ -15,18 +30,7 @@ class MicroscopeIdentifySchema(Schema):
    stage = fields.String()  # Stage object (represented as a string)


def rename(microscope, new_name):
    """
    Rename the microscope
    """

    microscope.name = new_name
    microscope.save_settings()


## Extension views


class ExampleIdentifyView(View):
    # Format our returned object using MicroscopeIdentifySchema
    schema = MicroscopeIdentifySchema()
@@ -54,21 +58,11 @@ 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 microscope object,
        # let schema handle formatting the output
        return 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(rename, "rename")

# Add API views to your extension
my_extension.add_view(ExampleIdentifyView, "/identify")
my_extension.add_view(ExampleRenameView, "/rename")
LABTHINGS_EXTENSIONS = (MyExtension,)
+19 −23
Original line number Diff line number Diff line
@@ -2,7 +2,22 @@ from labthings import Schema, fields, find_component
from labthings.extensions import BaseExtension
from labthings.views import PropertyView

## 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 rename(self, microscope, new_name):
        """
        Rename the microscope
        """
        microscope.name = new_name
        microscope.save_settings()


# Define which properties of a Microscope object we care about,
@@ -15,16 +30,7 @@ class MicroscopeIdentifySchema(Schema):
    stage = fields.String()  # Stage object (represented as a string)


def rename(microscope, new_name):
    """
    Rename the microscope
    """

    microscope.name = new_name
    microscope.save_settings()


## Extension views
## Extension viewss

# Since we only have a GET method here, it'll register as a read-only property
class ExampleIdentifyView(PropertyView):
@@ -68,21 +74,11 @@ class ExampleRenameView(PropertyView):
        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 microscope object,
        # let schema handle formatting the output
        return 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(rename, "rename")

# Add API views to your extension
my_extension.add_view(ExampleIdentifyView, "/identify")
my_extension.add_view(ExampleRenameView, "/rename")
LABTHINGS_EXTENSIONS = (MyExtension,)
Loading