|
| 1 | +import uuid |
| 2 | + |
| 3 | +from labthings.server.quick import create_app |
| 4 | +from labthings.server.view import View |
| 5 | +from labthings.server.decorators import ( |
| 6 | + ThingProperty, |
| 7 | + PropertySchema, |
| 8 | + use_args, |
| 9 | +) |
| 10 | + |
| 11 | +from labthings.server.types import value_to_field |
| 12 | + |
| 13 | +from components.pdf_component import PdfComponent |
| 14 | + |
| 15 | + |
| 16 | +class BasePropertyResource(View): |
| 17 | + def __init__(self): |
| 18 | + super().__init__() |
| 19 | + """ |
| 20 | + Properties to be added by constructor function: |
| 21 | + property_object: Object containing the property |
| 22 | + property_name: String name of property of object |
| 23 | + """ |
| 24 | + |
| 25 | + def get(self): |
| 26 | + return getattr(self.property_object, self.property_name) |
| 27 | + |
| 28 | + def post(self, args): |
| 29 | + setattr(self.property_object, self.property_name, args) |
| 30 | + return getattr(self.property_object, self.property_name) |
| 31 | + |
| 32 | + |
| 33 | +def gen_property(property_object, property_name, name: str = None, post=True): |
| 34 | + |
| 35 | + # Create a class name |
| 36 | + if not name: |
| 37 | + name = type(property_object).__name__ + f"_{property_name}" |
| 38 | + |
| 39 | + # Generate a basic property class |
| 40 | + generated_class = type( |
| 41 | + name, |
| 42 | + (BasePropertyResource, object), |
| 43 | + {"property_object": property_object, "property_name": property_name,}, |
| 44 | + ) |
| 45 | + |
| 46 | + # Override read-write capabilities |
| 47 | + if not post: |
| 48 | + generated_class.post = None |
| 49 | + |
| 50 | + # Add decorators for arguments etc |
| 51 | + initial_property_value = getattr(property_object, property_name) |
| 52 | + generated_class = PropertySchema(value_to_field(initial_property_value))( |
| 53 | + generated_class |
| 54 | + ) |
| 55 | + generated_class = ThingProperty(generated_class) |
| 56 | + |
| 57 | + return generated_class |
| 58 | + |
| 59 | + |
| 60 | +# Create LabThings Flask app |
| 61 | +app, labthing = create_app( |
| 62 | + __name__, |
| 63 | + prefix="/api", |
| 64 | + title=f"My Lab Device API", |
| 65 | + description="Test LabThing-based API", |
| 66 | + version="0.1.0", |
| 67 | +) |
| 68 | + |
| 69 | +# Attach an instance of our component |
| 70 | +my_component = PdfComponent() |
| 71 | +labthing.add_component(my_component, "org.labthings.example.mycomponent") |
| 72 | + |
| 73 | +# Add routes for the API views we created |
| 74 | +labthing.add_view(gen_property(my_component, "magic_denoise"), "/denoise") |
| 75 | + |
| 76 | +# Start the app |
| 77 | +if __name__ == "__main__": |
| 78 | + app.run(host="0.0.0.0", port="5000", threaded=True, debug=True, use_reloader=False) |
0 commit comments