From a714c22060fb114eca324911d4a70df339e4adee Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Fri, 31 May 2019 14:41:59 +0200 Subject: [PATCH] configd/templates add support for helpers.glob() to enable template traversal, which is practical when trying to merge several files into the same output. required for https://github.com/opnsense/core/issues/3505 --- .../service/modules/addons/template_helpers.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/opnsense/service/modules/addons/template_helpers.py b/src/opnsense/service/modules/addons/template_helpers.py index 13af3f981..898a16fbc 100644 --- a/src/opnsense/service/modules/addons/template_helpers.py +++ b/src/opnsense/service/modules/addons/template_helpers.py @@ -28,6 +28,8 @@ package : configd """ +import os +import glob import collections import netaddr @@ -167,3 +169,18 @@ class Helpers(object): elif type(lst) in (collections.OrderedDict, dict): return [lst] return lst + + @staticmethod + def glob(pathname): + """ glob within template directory scope + :param pathname: relative path name + :return: list + """ + result = list() + template_path = os.path.realpath("%s/../../templates/" % os.path.dirname(__file__)) + for sfilename in glob.glob("%s/%s" % (template_path, pathname)): + sfilename = os.path.realpath(sfilename) + if sfilename.startswith(template_path): + result.append(sfilename[len(template_path):].lstrip('/')) + + return result