|
| 1 | +import inspect |
| 2 | +import json |
| 3 | +from pathlib import Path |
| 4 | +from typing import Annotated, TypeAlias, get_type_hints |
| 5 | + |
| 6 | +import typer |
| 7 | +from pydantic import create_model |
| 8 | +from ruamel.yaml import YAML |
| 9 | + |
| 10 | +from .backend import Backend |
| 11 | +from .controller import Controller |
| 12 | +from .exceptions import LaunchError |
| 13 | +from .transport.adapter import TransportAdapter |
| 14 | +from .transport.epics.options import EpicsOptions |
| 15 | +from .transport.rest.options import RestOptions |
| 16 | +from .transport.tango.options import TangoOptions |
| 17 | + |
| 18 | +# Define a type alias for transport options |
| 19 | +TransportOptions: TypeAlias = EpicsOptions | TangoOptions | RestOptions |
| 20 | + |
| 21 | + |
| 22 | +class FastCS: |
| 23 | + def __init__( |
| 24 | + self, |
| 25 | + controller: Controller, |
| 26 | + transport_options: TransportOptions, |
| 27 | + ): |
| 28 | + self._backend = Backend(controller) |
| 29 | + self._transport: TransportAdapter |
| 30 | + match transport_options: |
| 31 | + case EpicsOptions(): |
| 32 | + from .transport.epics.adapter import EpicsTransport |
| 33 | + |
| 34 | + self._transport = EpicsTransport( |
| 35 | + self._backend.mapping, |
| 36 | + self._backend.context, |
| 37 | + self._backend.dispatcher, |
| 38 | + transport_options, |
| 39 | + ) |
| 40 | + case TangoOptions(): |
| 41 | + from .transport.tango.adapter import TangoTransport |
| 42 | + |
| 43 | + self._transport = TangoTransport( |
| 44 | + self._backend.mapping, |
| 45 | + transport_options, |
| 46 | + ) |
| 47 | + case RestOptions(): |
| 48 | + from .transport.rest.adapter import RestTransport |
| 49 | + |
| 50 | + self._transport = RestTransport( |
| 51 | + self._backend.mapping, |
| 52 | + transport_options, |
| 53 | + ) |
| 54 | + |
| 55 | + def create_docs(self) -> None: |
| 56 | + self._transport.create_docs() |
| 57 | + |
| 58 | + def create_gui(self) -> None: |
| 59 | + self._transport.create_gui() |
| 60 | + |
| 61 | + def run(self) -> None: |
| 62 | + self._backend.run() |
| 63 | + self._transport.run() |
| 64 | + |
| 65 | + |
| 66 | +def launch(controller_class: type[Controller]) -> None: |
| 67 | + """ |
| 68 | + Serves as an entry point for starting FastCS applications. |
| 69 | +
|
| 70 | + By utilizing type hints in a Controller's __init__ method, this |
| 71 | + function provides a command-line interface to describe and gather the |
| 72 | + required configuration before instantiating the application. |
| 73 | +
|
| 74 | + Args: |
| 75 | + controller_class (type[Controller]): The FastCS Controller to instantiate. |
| 76 | + It must have a type-hinted __init__ method and no more than 2 arguments. |
| 77 | +
|
| 78 | + Raises: |
| 79 | + LaunchError: If the class's __init__ is not as expected |
| 80 | +
|
| 81 | + Example of the expected Controller implementation: |
| 82 | + class MyController(Controller): |
| 83 | + def __init__(self, my_arg: MyControllerOptions) -> None: |
| 84 | + ... |
| 85 | +
|
| 86 | + Typical usage: |
| 87 | + if __name__ == "__main__": |
| 88 | + launch(MyController) |
| 89 | + """ |
| 90 | + _launch(controller_class)() |
| 91 | + |
| 92 | + |
| 93 | +def _launch(controller_class: type[Controller]) -> typer.Typer: |
| 94 | + sig = inspect.signature(controller_class.__init__) |
| 95 | + args = inspect.getfullargspec(controller_class.__init__)[0] |
| 96 | + if len(args) == 1: |
| 97 | + fastcs_options = create_model( |
| 98 | + f"{controller_class.__name__}", |
| 99 | + transport=(TransportOptions, ...), |
| 100 | + __config__={"extra": "forbid"}, |
| 101 | + ) |
| 102 | + elif len(args) == 2: |
| 103 | + hints = get_type_hints(controller_class.__init__) |
| 104 | + if hints: |
| 105 | + options_type = list(hints.values())[-1] |
| 106 | + else: |
| 107 | + raise LaunchError( |
| 108 | + f"Expected typehinting in '{controller_class.__name__}" |
| 109 | + f".__init__' but received {sig}. Add a typehint for `{args[-1]}`." |
| 110 | + ) |
| 111 | + fastcs_options = create_model( |
| 112 | + f"{controller_class.__name__}", |
| 113 | + controller=(options_type, ...), |
| 114 | + transport=(TransportOptions, ...), |
| 115 | + __config__={"extra": "forbid"}, |
| 116 | + ) |
| 117 | + else: |
| 118 | + raise LaunchError( |
| 119 | + f"Expected no more than 2 arguments for '{controller_class.__name__}" |
| 120 | + f".__init__' but received {len(args)} as `{sig}`" |
| 121 | + ) |
| 122 | + |
| 123 | + launch_typer = typer.Typer() |
| 124 | + |
| 125 | + class LaunchContext: |
| 126 | + def __init__(self, controller_class, fastcs_options): |
| 127 | + self.controller_class = controller_class |
| 128 | + self.fastcs_options = fastcs_options |
| 129 | + |
| 130 | + @launch_typer.callback() |
| 131 | + def create_context(ctx: typer.Context): |
| 132 | + ctx.obj = LaunchContext( |
| 133 | + controller_class, |
| 134 | + fastcs_options, |
| 135 | + ) |
| 136 | + |
| 137 | + @launch_typer.command(help=f"Produce json schema for a {controller_class.__name__}") |
| 138 | + def schema(ctx: typer.Context): |
| 139 | + system_schema = ctx.obj.fastcs_options.model_json_schema() |
| 140 | + print(json.dumps(system_schema, indent=2)) |
| 141 | + |
| 142 | + @launch_typer.command(help=f"Start up a {controller_class.__name__}") |
| 143 | + def run( |
| 144 | + ctx: typer.Context, |
| 145 | + config: Annotated[ |
| 146 | + Path, |
| 147 | + typer.Argument( |
| 148 | + help=f"A yaml file matching the {controller_class.__name__} schema" |
| 149 | + ), |
| 150 | + ], |
| 151 | + ): |
| 152 | + """ |
| 153 | + Start the controller |
| 154 | + """ |
| 155 | + controller_class = ctx.obj.controller_class |
| 156 | + fastcs_options = ctx.obj.fastcs_options |
| 157 | + |
| 158 | + yaml = YAML(typ="safe") |
| 159 | + options_yaml = yaml.load(config) |
| 160 | + # To do: Handle a k8s "values.yaml" file |
| 161 | + instance_options = fastcs_options.model_validate(options_yaml) |
| 162 | + if hasattr(instance_options, "controller"): |
| 163 | + controller = controller_class(instance_options.controller) |
| 164 | + else: |
| 165 | + controller = controller_class() |
| 166 | + |
| 167 | + instance = FastCS( |
| 168 | + controller, |
| 169 | + instance_options.transport, |
| 170 | + ) |
| 171 | + |
| 172 | + if "gui" in options_yaml["transport"]: |
| 173 | + instance.create_gui() |
| 174 | + if "docs" in options_yaml["transport"]: |
| 175 | + instance.create_docs() |
| 176 | + instance.run() |
| 177 | + |
| 178 | + return launch_typer |
0 commit comments