Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/source/explanation/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
4 changes: 4 additions & 0 deletions docs/source/how-to/configuration/create-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
94 changes: 94 additions & 0 deletions docs/source/how-to/configuration/use-templates.md
Original file line number Diff line number Diff line change
@@ -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).
1 change: 1 addition & 0 deletions docs/source/how-to/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading