Wednesday 1 September 2021

Is it possible to use a Jinja2 template to reverse engineer the parameters from a document?

The general workflow of Jinja2 is params + Jinja2 template = generated document.

from jinja2 import Template
t = Template("Hello !")
t.render(something="World")
>>> u'Hello World!'

Is it possible to use a Jinja2 template to reverse engineer the parameters from a document? In other words I am looking for the following : Jinja2 template + generated document = params.

from jinja2 import Template
t = Template("Hello !")
t.reverse("Hello World!")
>>> {"something" : "World"}

The json output is not a requirement, but it would be handy.

If not what is a good approach to create logic like this?

Context: I use Jinja2 to generate Cisco Switch configuration files, and it would be a nice feature to be able to pull up documents generated in the past, and instead of showing the 1000+ line config scripts, I would like to list just the parameters. I know it can be solved by storing all params in a simple DB, but currently i don't have a DB set up, and I would avoid it, if possible.



from Is it possible to use a Jinja2 template to reverse engineer the parameters from a document?

No comments:

Post a Comment