fmartingr
/
jeeves
Archived
1
0
Fork 0

WIP: Template action

This commit is contained in:
Felipe Martin 2020-04-30 18:30:55 +02:00
parent 2a3d1262ad
commit 85a9118d0f
Signed by: fmartingr
GPG Key ID: 716BC147715E716F
2 changed files with 46 additions and 0 deletions

View File

@ -1,7 +1,10 @@
from jeeves.core.actions.shell import ScriptAction
from jeeves.core.actions.docker import DockerBuildAction, DockerRunAction
from jeeves.core.actions.files import TemplateAction
__all__ = [
# Files
TemplateAction,
# Shell
ScriptAction,
# Docker

View File

@ -0,0 +1,43 @@
from typing import Text
import os
from jinja2 import Template
from jeeves.core.objects import Result
from jeeves.core.actions.base import Action
class TemplateAction(Action):
"""
Parses the source jinja2 template into the output path
"""
id = "contrib/template"
verbose_name = "Render template"
class Parameters(Action.Parameters):
"""
+----------------+------+-----------+----------------------------------------------+
| Parameter name | Type | Mandatory | Description |
+================+======+===========+==============================================+
| ``src`` | text | yes | The source template |
| ``dest`` | text | yes | The destination file (relative to workspace) |
+----------------+------+-----------+----------------------------------------------+
"""
src: Text
dest: Text
def execute(self, **kwargs):
workspace = kwargs.get("workspace")
arguments = kwargs.get("arguments")
source_path = os.path.join(os.getcwd(), self.parameters.src)
assert os.path.exists(source_path), "Source template does not exist"
template = Template(open(source_path, "r").read())
with open(os.path.join(workspace.path, self.parameters.dest), "w") as handler:
handler.write(template.render(**arguments))
return Result(success=True)