Skip to content
Open
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
24 changes: 22 additions & 2 deletions doc/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
9 changes: 8 additions & 1 deletion labgrid/remote/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pprint import pprint

import glob
import os

import attr
Expand All @@ -15,15 +16,21 @@ 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="##",
)
try:
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)
Expand Down