• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

uwefladrich / scriptengine / 7020063116

28 Nov 2023 02:17PM UTC coverage: 91.371% (-0.3%) from 91.671%
7020063116

Pull #100

github

uwefladrich
Pass Jinja errors in creation of Jobs
Pull Request #100: Better Jinja2 error messages

23 of 38 new or added lines in 4 files covered. (60.53%)

1 existing line in 1 file now uncovered.

1906 of 2086 relevant lines covered (91.37%)

0.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

41.03
/src/scriptengine/tasks/base/template.py
1
"""Template task for ScriptEngine."""
1✔
2

3
import os
1✔
4
import stat
1✔
5
from pathlib import Path
1✔
6

7
import jinja2
1✔
8

9
from scriptengine.exceptions import ScriptEngineTaskRunError
1✔
10
from scriptengine.jinja import filters as j2filters
1✔
11
from scriptengine.jinja import render as j2render
1✔
12
from scriptengine.tasks.core import Task, timed_runner
1✔
13

14

15
class Template(Task):
1✔
16
    """Template task, renders a template with Jinja2 and writes result to file.
1✔
17

18
    The task needs a source (a Jinja2 template file) and a destination. It
19
    renders the template given the context passed in kwargs and writes the
20
    result to the destination file.
21

22
    Args:
23
        dictionary (dict): Must at least contain the following keys:
24
            - src: Source file name (a Jinja2 template)
25
            - dst: Destination file name
26
    """
27

28
    _required_arguments = (
1✔
29
        "src",
30
        "dst",
31
    )
32

33
    def __init__(self, arguments):
1✔
34
        Template.check_arguments(arguments)
×
35
        super().__init__(arguments)
×
36

37
    def __str__(self):
1✔
38
        return f"Template: {self.src} --> {self.dst}"
×
39

40
    @timed_runner
1✔
41
    def run(self, context):
1✔
42
        src = self.getarg("src", context)
×
43
        dst = Path(self.getarg("dst", context))
×
44
        self.log_info(f"Jinja2 render: {src} --> {dst}")
×
45

46
        # Prepare the Jinja render environment with the proper search path
47
        search_path = (
×
48
            Path("."),
49
            Path(".") / "templates",
50
            Path(context["se"]["cli"]["cwd"]),
51
            Path(context["se"]["cli"]["cwd"]) / "templates",
52
        )
53
        self.log_debug(f"Template search path: {tuple(map(str, search_path))}")
×
54
        j2loader = jinja2.FileSystemLoader(search_path)
×
55
        j2env = jinja2.Environment(loader=j2loader)
×
56
        for name, function in j2filters().items():
×
57
            j2env.filters[name] = function
×
58

NEW
59
        try:
×
NEW
60
            output = j2render(j2env.get_template(src).render(context), context)
×
NEW
61
        except jinja2.TemplateError as e:
×
NEW
62
            self.log_error(f"Jinja2 error {type(e).__name__}: {e}")
×
NEW
63
            raise ScriptEngineTaskRunError
×
64

65
        with dst.open(mode="w") as f:
×
66
            f.write(output + "\n")
×
67

68
        if self.getarg("executable", context, default=False):
×
69
            umask = os.umask(0)
×
70
            os.umask(umask)
×
71
            dst.chmod(
×
72
                dst.stat().st_mode
73
                | (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) & ~umask
74
            )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc