|
1 | | -from dataclasses import dataclass |
2 | | -from typing import Any |
| 1 | +import json |
| 2 | +from pathlib import Path |
| 3 | +from typing import Annotated, Any, get_type_hints |
| 4 | + |
| 5 | +import typer |
| 6 | +from pydantic import create_model |
| 7 | +from ruamel.yaml import YAML |
3 | 8 |
|
4 | 9 | from .backend import Backend |
5 | 10 | from .controller import Controller |
|
8 | 13 | from .transport.tango.options import TangoOptions |
9 | 14 |
|
10 | 15 |
|
11 | | -@dataclass |
12 | | -class FastCSOptions: |
13 | | - transport: EpicsOptions | TangoOptions |
14 | | - controller: Any = None |
15 | | - |
16 | | - |
17 | 16 | class FastCS: |
18 | 17 | def __init__( |
19 | 18 | self, |
20 | | - controller: Controller, |
21 | | - options: FastCSOptions, |
| 19 | + controller: type[Controller], |
| 20 | + controller_options: Any, |
| 21 | + transport_options: EpicsOptions | TangoOptions, |
22 | 22 | ): |
23 | | - self.backend = Backend(controller) |
24 | | - self.transport: TransportAdapter |
25 | | - match options.transport: |
| 23 | + self._controller = controller() |
| 24 | + self._controller.startup(controller_options) |
| 25 | + |
| 26 | + self._backend = Backend(self._controller) |
| 27 | + self._transport: TransportAdapter |
| 28 | + match transport_options: |
26 | 29 | case TangoOptions(): |
27 | 30 | from .transport.tango.adapter import TangoTransport |
28 | 31 |
|
29 | | - self.transport = TangoTransport( |
30 | | - self.backend.mapping, |
31 | | - options.transport, |
| 32 | + self._transport = TangoTransport( |
| 33 | + self._backend.mapping, |
| 34 | + transport_options, |
32 | 35 | ) |
33 | 36 | case EpicsOptions(): |
34 | 37 | from .transport.epics.adapter import EpicsTransport |
35 | 38 |
|
36 | | - self.transport = EpicsTransport( |
37 | | - self.backend.mapping, |
38 | | - self.backend.context, |
39 | | - self.backend.dispatcher, |
40 | | - options.transport, |
| 39 | + self._transport = EpicsTransport( |
| 40 | + self._backend.mapping, |
| 41 | + self._backend.context, |
| 42 | + self._backend.dispatcher, |
| 43 | + transport_options, |
41 | 44 | ) |
42 | 45 |
|
43 | 46 | def create_docs(self) -> None: |
44 | | - self.transport.create_docs() |
| 47 | + self._transport.create_docs() |
45 | 48 |
|
46 | 49 | def create_gui(self) -> None: |
47 | | - self.transport.create_gui() |
| 50 | + self._transport.create_gui() |
48 | 51 |
|
49 | 52 | def run(self) -> None: |
50 | | - self.backend.run() |
51 | | - self.transport.run() |
| 53 | + self._backend.run() |
| 54 | + self._transport.run() |
| 55 | + |
| 56 | + |
| 57 | +def launch(controller_class: type[Controller]): |
| 58 | + try: |
| 59 | + options_type = get_type_hints(controller_class.startup)["config"] |
| 60 | + except KeyError as e: |
| 61 | + raise KeyError( |
| 62 | + "Missing type hint for config argument, expected: " |
| 63 | + "`def startup(self, config: <type hint>)`" |
| 64 | + ) from e |
| 65 | + |
| 66 | + fastcs_options = create_model( |
| 67 | + f"{controller_class.__name__}", |
| 68 | + controller=(options_type, ...), |
| 69 | + transport=(EpicsOptions | TangoOptions, ...), |
| 70 | + ) |
| 71 | + |
| 72 | + _launch_typer = typer.Typer() |
| 73 | + |
| 74 | + class _LaunchContext: |
| 75 | + def __init__(self, controller_class, fastcs_options): |
| 76 | + self.controller_class = controller_class |
| 77 | + self.fastcs_options = fastcs_options |
| 78 | + |
| 79 | + @_launch_typer.callback() |
| 80 | + def create_context(ctx: typer.Context): |
| 81 | + ctx.obj = _LaunchContext( |
| 82 | + controller_class, |
| 83 | + fastcs_options, |
| 84 | + ) |
| 85 | + |
| 86 | + @_launch_typer.command( |
| 87 | + help=f"Produce json schema for a {controller_class.__name__}" |
| 88 | + ) |
| 89 | + def schema(ctx: typer.Context): |
| 90 | + system_schema = ctx.obj.fastcs_options.model_json_schema() |
| 91 | + print(json.dumps(system_schema, indent=2)) |
| 92 | + |
| 93 | + @_launch_typer.command(help=f"Start up a {controller_class.__name__}") |
| 94 | + def run( |
| 95 | + ctx: typer.Context, |
| 96 | + config: Annotated[ |
| 97 | + Path, |
| 98 | + typer.Argument( |
| 99 | + help=f"A yaml file matching the {controller_class.__name__} schema" |
| 100 | + ), |
| 101 | + ], |
| 102 | + ): |
| 103 | + """ |
| 104 | + Start the controller |
| 105 | + """ |
| 106 | + controller_class = ctx.obj.controller_class |
| 107 | + fastcs_options = ctx.obj.fastcs_options |
| 108 | + |
| 109 | + yaml = YAML(typ="safe") |
| 110 | + options_yaml = yaml.load(config) |
| 111 | + # To do: Handle a k8s "values.yaml" file |
| 112 | + instance_options = fastcs_options.parse_obj(options_yaml) |
| 113 | + |
| 114 | + instance = FastCS( |
| 115 | + controller_class, |
| 116 | + instance_options.controller, |
| 117 | + instance_options.transport, |
| 118 | + ) |
| 119 | + |
| 120 | + if hasattr(instance_options.transport, "gui"): |
| 121 | + instance.create_gui() |
| 122 | + if hasattr(instance_options.transport, "docs"): |
| 123 | + instance.create_docs() |
| 124 | + instance.run() |
| 125 | + |
| 126 | + _launch_typer() |
0 commit comments