diff --git a/doc/configuration.rst b/doc/configuration.rst index 08429d67f..685aa26b6 100644 --- a/doc/configuration.rst +++ b/doc/configuration.rst @@ -3995,12 +3995,32 @@ something like ``{{ env['FOOBAR'] }}`` to insert the content of environment variable ``FOOBAR``. In addition to ``env`` the template also has access to the following variables: -isolated - ``True`` or ``False``, depending on the :code:`--isolated` command line option. +drop_ins + An alphabetically sorted list of files in a directory named after the + currently processed template with ".d" appended. For example if the template + is named ``/etc/labgrid/configuration.yaml`` the list will contain files + matching ``/etc/labgrid/configuration.yaml.d/*.yaml`` pattern. The paths are + relative to the directory containing the template (i.e. ``/etc/labgrid`` + in our example). These files can be included using the ``include`` directive. + A template loading all its drop-ins at would look like this + +.. code-block:: + + # for f in drop_ins + {% include f %} + # endfor + +.. warning:: + Use the ``{% include f %}`` form instead of ``# include f``, otherwise + Jinja2 would strip newline characters before concatenation, and thus break + the YAML formatting. hostname The hostname of the exporter host. Can be used to e.g. construct URLs to the current host (``http://{{ hostname }}/``). +isolated + ``True`` or ``False``, depending on the :code:`--isolated` command line option. + name The name of the exporter. diff --git a/labgrid/remote/config.py b/labgrid/remote/config.py index 5efc05332..fd63c013e 100644 --- a/labgrid/remote/config.py +++ b/labgrid/remote/config.py @@ -1,5 +1,6 @@ from pprint import pprint +import glob import os import attr @@ -15,8 +16,9 @@ class ResourceConfig: template_env = attr.ib(default=attr.Factory(dict), validator=attr.validators.instance_of(dict)) def __attrs_post_init__(self): + _dirname = os.path.dirname(self.filename) env = jinja2.Environment( - loader=jinja2.FileSystemLoader(os.path.dirname(self.filename)), + loader=jinja2.FileSystemLoader(_dirname), line_statement_prefix="#", line_comment_prefix="##", ) @@ -24,6 +26,11 @@ def __attrs_post_init__(self): template = env.get_template(os.path.basename(self.filename)) except jinja2.TemplateNotFound: raise NoConfigFoundError(f"{self.filename} could not be found") + drop_ins = [ + os.path.relpath(p, _dirname) for p in sorted(glob.glob(os.path.join(self.filename + ".d", "*.yaml"))) + ] + + self.template_env["drop_ins"] = drop_ins rendered = template.render(self.template_env) pprint(("rendered", rendered)) self.data = load(rendered)