Skip to content

Commit

Permalink
Merge jinja
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarbette committed May 10, 2024
2 parents b17d302 + 62ae4a4 commit 02d3524
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
18 changes: 10 additions & 8 deletions npf/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ def suggest(self, w, max = None):
class SectionFactory:
varPattern = "([a-zA-Z0-9_:-]+)[=](" + Variable.VALUE_REGEX + ")?"
namePattern = re.compile(
"^(?P<tags>" + Variable.TAGS_REGEX + "[:])?(?P<name>info|config|variables|exit|pypost|pyexit|late_variables|include (?P<includeName>[a-zA-Z0-9_./-]+)(?P<includeParams>([ \t]+" +
varPattern + ")+)?|(init-)?file(:?[@](?P<fileRole>[a-zA-Z0-9]+))? (?P<fileName>[a-zA-Z0-9_.${}-]+)(:? (?P<fileNoparse>noparse))?|require|"
"import(:?[@](?P<importRole>[a-zA-Z0-9]+)(:?[-](?P<importMulti>[*0-9]+))?)?[ \t]+(?P<importModule>" + Variable.VALUE_REGEX + ")(?P<importParams>([ \t]+" +
varPattern + ")+)?|" +
"sendfile(:?[@](?P<sendfileRole>[a-zA-Z0-9]+))?[ \t]+(?P<sendfilePath>.*)|" +
"(:?script|init|exit)(:?[@](?P<scriptRole>[a-zA-Z0-9]+)(:?[-](?P<scriptMulti>[*0-9]+))?)?(?P<scriptParams>([ \t]+" + varPattern + ")*))$")
"^(?P<tags>" + Variable.TAGS_REGEX + "[:])?(?P<name>info|config|variables|exit|pypost|pyexit|late_variables|"
"include (?P<includeName>[a-zA-Z0-9_./-]+)(?P<includeParams>([ \t]+" + varPattern + ")+)?|"
"(init-)?file(:?[@](?P<fileRole>[a-zA-Z0-9]+))? (?P<fileName>[a-zA-Z0-9_.${}-]+)(:? (?P<fileNoparse>noparse))?(:? (?P<fileJinja>jinja))?|"
"require|"
"import(:?[@](?P<importRole>[a-zA-Z0-9]+)(:?[-](?P<importMulti>[*0-9]+))?)?[ \t]+(?P<importModule>" + Variable.VALUE_REGEX + ")(?P<importParams>([ \t]+" + varPattern + ")+)?|"
"sendfile(:?[@](?P<sendfileRole>[a-zA-Z0-9]+))?[ \t]+(?P<sendfilePath>.*)|" +
"(:?script|init|exit)(:?[@](?P<scriptRole>[a-zA-Z0-9]+)(:?[-](?P<scriptMulti>[*0-9]+))?)?(?P<scriptParams>([ \t]+" + varPattern + ")*))$")

@staticmethod
def build(test, data):
Expand Down Expand Up @@ -100,7 +101,7 @@ def build(test, data):

if sectionName.startswith('file'):
s = SectionFile(matcher.group('fileName').strip(), role=matcher.group('fileRole'),
noparse=matcher.group('fileNoparse'))
noparse=matcher.group('fileNoparse'), jinja=matcher.group('fileJinja'))
return s
if sectionName.startswith('init-file'):
s = SectionInitFile(matcher.group('fileName').strip(), role=matcher.group('fileRole'),
Expand Down Expand Up @@ -257,12 +258,13 @@ def finish(self, test):


class SectionFile(Section):
def __init__(self, filename, role=None, noparse=False):
def __init__(self, filename, role=None, noparse=False, jinja=False):
super().__init__('file')
self.content = ''
self.filename = filename
self._role = role
self.noparse = noparse
self.jinja = jinja

def get_role(self):
return self._role
Expand Down
5 changes: 5 additions & 0 deletions npf/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,11 @@ def build_file_list(self, v, self_role=None, files=None) -> List[Tuple[str, str,
p = SectionVariable.replace_variables(v, s.content, role,default_role_map = self.config.get_dict("default_role_map"))
else:
p = s.content
if s.jinja:
from jinja2 import Environment, BaseLoader
env = Environment(loader=BaseLoader)
template = env.from_string(p)
p = template.render(**v)
create_list.append((s.filename, p, role))
return create_list

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
'pygtrie',
'packaging',
'importlib_metadata',
'npf-web-extension >= 0.6.4'
'npf-web-extension >= 0.6.4',
'jinja2'
]

setuptools.setup(
Expand Down

0 comments on commit 02d3524

Please sign in to comment.