diff --git a/docs/source/explanation/configuration.md b/docs/source/explanation/configuration.md index 5cc854f..5b97901 100644 --- a/docs/source/explanation/configuration.md +++ b/docs/source/explanation/configuration.md @@ -117,9 +117,20 @@ The following built-in resolvers are available: | `env` | Environment variable | The value of the named environment variable. An error is raised if it is not set. | | `path` | A file or directory path | The absolute, normalized path, resolved relative to pyAML's configuration root. The target is not loaded as part of loading the configuration. | | `file` | A YAML, YML, or JSON file path | The path to a file which should be loaded and expanded into the configuration as part of loading it. Relative paths use the configuration root. | +| `template` | A template name and positional arguments | `${template:NAME,ARG1,ARG2}` inserts a copy of a registered template with its parameters substituted. | Configuration files can also be included without an explicit `file` expression. A string ending in `.yaml`, `.yml`, or `.json` is loaded automatically for convenience. This makes it easy to split the configuration into several files if one wishes. ```{warning} If you include a `.yaml`, `.yml`, or `.json` in the configuration file which you do not want to be loaded and expanded into the configuration (for example a lattice in JSON format), remember to put `${path:filename}` or you will get an error when loading the configuration. ``` + +## Configuration Templates + +Templates reduce repetition in YAML or JSON configuration files. A root-level `templates` list defines reusable configuration bodies. The loader registers these definitions before expanding the file, then removes the list: `templates` is loader metadata, an exception to the constructor-argument rule above. + +A template call produces an independent copy of its body. After parameter substitution, the loader expands any resolver expressions in that copy. Use the call as the whole value where a configuration object is expected, such as an entry in `devices`. + +Template names must be unique within a loading session. Included files share that session's templates, while independent `Accelerator.load()` calls have separate registries; loading one file does not make its templates available to a later call. + +See [Use Configuration Templates](../how-to/configuration/use-templates.md) for the syntax. diff --git a/docs/source/how-to/configuration/create-configuration.md b/docs/source/how-to/configuration/create-configuration.md index 40f1314..c4127a9 100644 --- a/docs/source/how-to/configuration/create-configuration.md +++ b/docs/source/how-to/configuration/create-configuration.md @@ -213,6 +213,10 @@ devices: Values can also come from environment variables with `${env:NAME}`. See [Resolvers](../../explanation/configuration.md#resolvers) for all options. +## Reuse Configuration with Templates + +If several devices share the same structure, define it once and supply names or catalog keys as parameters. See [Use Configuration Templates](./use-templates.md) for a complete example. + ## Use Your Own Classes The configuration is not limited to pyAML classes. Any class that can be imported can be used in the configuration, for example a magnet model specific to your facility: diff --git a/docs/source/how-to/configuration/use-templates.md b/docs/source/how-to/configuration/use-templates.md new file mode 100644 index 0000000..6a3b026 --- /dev/null +++ b/docs/source/how-to/configuration/use-templates.md @@ -0,0 +1,94 @@ +# Use Configuration Templates + +Use a template when several devices share the same configuration and only names or catalog keys differ. Define the common fields once, then supply the differences as arguments. + +## Define and Use a Template + +Save this complete example as `accelerator.yaml`: + +```yaml +class: pyaml.accelerator.Accelerator +facility: my_facility +machine: storage_ring +energy: 1.0e9 +controls: + - class: tango.pyaml.controlsystem.TangoControlSystem + tango_host: my_host + name: live + catalog: + class: tango.pyaml.tango_catalog.TangoCatalog + disconnected: True +templates: + - name: quadrupole + parameters: + - name + - catalog_key + config: + class: pyaml.magnet.quadrupole.Quadrupole + name: "{name}" + model: + class: pyaml.magnet.identity_model.IdentityMagnetModel + unit: 1/m + physics: "domain/family/{catalog_key}/strength" +devices: + - ${template:quadrupole,QF_001,QF.01} + - ${template:quadrupole,QD_001,QD.01} +``` + +Each template has a unique `name`, an ordered list of `parameters`, and a `config` body. + +```{warning} +Unquoted braces can be interpreted as YAML syntax. For example, `name: {name}` creates a mapping with the key `name` and a null value, rather than the string placeholder intended for substitution. You can write `name: "{name}"` instead. Always quote strings containing substitution placeholders to avoid mistakes. +``` + +Each `${template:NAME,args}` call inserts a copy of the body with its placeholders replaced. Here, the first argument sets the device name and the second supplies the non-repeatable part of the catalog key prefix. The result is two quadrupoles, `QF_001` and `QD_001`, with their own model configurations. + +## Example to run: + +Run the example in Python with the following snippet (requires `tango-pyaml`): + +```python +from pathlib import Path +from tempfile import TemporaryDirectory + +from pyaml.accelerator import Accelerator + +configuration = """ +class: pyaml.accelerator.Accelerator +facility: my_facility +machine: storage_ring +energy: 1.0e9 +controls: + - class: tango.pyaml.controlsystem.TangoControlSystem + tango_host: my_host + name: live + catalog: + class: tango.pyaml.tango_catalog.TangoCatalog + disconnected: True +templates: + - name: quadrupole + parameters: + - name + - catalog_key + config: + class: pyaml.magnet.quadrupole.Quadrupole + name: "{name}" + model: + class: pyaml.magnet.identity_model.IdentityMagnetModel + unit: 1/m + physics: "domain/family/{catalog_key}/strength" +devices: + - ${template:quadrupole,QF_001,QF.01} + - ${template:quadrupole,QD_001,QD.01} +""" + +with TemporaryDirectory() as directory: + path = Path(directory) / "accelerator.yaml" + path.write_text(configuration, encoding="utf-8") + + accelerator = Accelerator.load(str(path)) + print(accelerator.live["QF_001"]) + print(accelerator.live["QD_001"]) +``` + +For a lower-level loading behavior and explanation, see [Configuration Templates](../../explanation/configuration.md#configuration-templates). diff --git a/docs/source/how-to/index.md b/docs/source/how-to/index.md index 150b94b..8851698 100644 --- a/docs/source/how-to/index.md +++ b/docs/source/how-to/index.md @@ -22,6 +22,7 @@ installation/developer-installation :caption: Configuration configuration/create-configuration +configuration/use-templates configuration/use-schema-registry configuration/validate-configuration configuration/generate-json-schema