File tree Expand file tree Collapse file tree 2 files changed +30
-3
lines changed
Expand file tree Collapse file tree 2 files changed +30
-3
lines changed Original file line number Diff line number Diff line change @@ -3995,12 +3995,32 @@ something like ``{{ env['FOOBAR'] }}`` to insert the content of environment
39953995variable ``FOOBAR ``.
39963996In addition to ``env `` the template also has access to the following variables:
39973997
3998- isolated
3999- ``True `` or ``False ``, depending on the :code: `--isolated ` command line option.
3998+ drop_ins
3999+ An alphabetically sorted list of files in a directory named after the
4000+ currently processed template with ".d" appended. For example if the template
4001+ is named ``/etc/labgrid/configuration.yaml `` the list will contain files
4002+ matching ``/etc/labgrid/configuration.yaml.d/*.yaml `` pattern. The paths are
4003+ relative to the directory containing the template (i.e. ``/etc/labgrid ``
4004+ in our example). These files can be included using the ``include `` directive.
4005+ A template loading all its drop-ins at would look like this
4006+
4007+ .. code-block ::
4008+
4009+ # for f in drop_ins
4010+ {% include f %}
4011+ # endfor
4012+
4013+ .. warning ::
4014+ Use the ``{% include f %} `` form instead of ``# include f ``, otherwise
4015+ Jinja2 would strip newline characters before concatenation, and thus break
4016+ the YAML formatting.
40004017
40014018hostname
40024019 The hostname of the exporter host. Can be used to e.g. construct URLs to the
40034020 current host (``http://{{ hostname }}/ ``).
40044021
4022+ isolated
4023+ ``True `` or ``False ``, depending on the :code: `--isolated ` command line option.
4024+
40054025name
40064026 The name of the exporter.
Original file line number Diff line number Diff line change 11from pprint import pprint
22
3+ import glob
34import os
45
56import attr
@@ -15,15 +16,21 @@ class ResourceConfig:
1516 template_env = attr .ib (default = attr .Factory (dict ), validator = attr .validators .instance_of (dict ))
1617
1718 def __attrs_post_init__ (self ):
19+ _dirname = os .path .dirname (self .filename )
1820 env = jinja2 .Environment (
19- loader = jinja2 .FileSystemLoader (os . path . dirname ( self . filename ) ),
21+ loader = jinja2 .FileSystemLoader (_dirname ),
2022 line_statement_prefix = "#" ,
2123 line_comment_prefix = "##" ,
2224 )
2325 try :
2426 template = env .get_template (os .path .basename (self .filename ))
2527 except jinja2 .TemplateNotFound :
2628 raise NoConfigFoundError (f"{ self .filename } could not be found" )
29+ drop_ins = [
30+ os .path .relpath (p , _dirname ) for p in sorted (glob .glob (os .path .join (self .filename + ".d" , "*.yaml" )))
31+ ]
32+
33+ self .template_env ["drop_ins" ] = drop_ins
2734 rendered = template .render (self .template_env )
2835 pprint (("rendered" , rendered ))
2936 self .data = load (rendered )
You can’t perform that action at this time.
0 commit comments