Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
feat: Allow to use external Python
Browse files Browse the repository at this point in the history
  • Loading branch information
codablock committed Jun 17, 2024
1 parent a48ebf0 commit 3e435dc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
17 changes: 12 additions & 5 deletions jinja2.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

type Jinja2 struct {
ep *python.EmbeddedPython
ep python.Python
extractDir string
jinja2Lib *embed_util.EmbeddedFiles
rendererSrc *embed_util.EmbeddedFiles
Expand Down Expand Up @@ -69,9 +69,13 @@ func NewJinja2(name string, parallelism int, opts ...Jinja2Opt) (*Jinja2, error)
}
tmpDir = filepath.Join(tmpDir, name)

j2.ep, err = python.NewEmbeddedPythonWithTmpDir(tmpDir+"-python", true)
if err != nil {
return nil, err
if j2.defaultOptions.python != nil {
j2.ep = j2.defaultOptions.python
} else {
j2.ep, err = python.NewEmbeddedPythonWithTmpDir(tmpDir+"-python", true)
if err != nil {
return nil, err
}
}
j2.jinja2Lib, err = embed_util.NewEmbeddedFilesWithTmpDir(data.Data, tmpDir+"-jinja2-lib", true)
if err != nil {
Expand Down Expand Up @@ -120,7 +124,10 @@ func (j *Jinja2) Close() {
func (j *Jinja2) Cleanup() {
_ = j.rendererSrc.Cleanup()
_ = j.jinja2Lib.Cleanup()
_ = j.ep.Cleanup()

if ep, ok := j.ep.(*python.EmbeddedPython); ok {
_ = ep.Cleanup()
}
}

func (j *Jinja2) RenderStrings(jobs []*RenderJob, opts ...Jinja2Opt) error {
Expand Down
36 changes: 36 additions & 0 deletions jinja2_external_python_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package jinja2

import (
"fmt"
"github.com/kluctl/go-embed-python/python"
"github.com/stretchr/testify/assert"
"math/rand"
"testing"
)

func TestExternalPython(t *testing.T) {
rndName := fmt.Sprintf("test-%d", rand.Uint32())
ep, err := python.NewEmbeddedPython(rndName)
assert.NoError(t, err)

j2 := newJinja2(t, WithPython(ep))
t1 := newTemplateFile(t, `{{ "a" }}`)

r1, err := j2.RenderFile(t1)
assert.NoError(t, err)
assert.Equal(t, "a", r1)
}

func TestSystemPython(t *testing.T) {
ep := python.NewPython()
if _, err := ep.GetExePath(); err != nil {
t.Skipf("skipping due to missing python binary")
}

j2 := newJinja2(t, WithPython(ep))
t1 := newTemplateFile(t, `{{ "a" }}`)

r1, err := j2.RenderFile(t1)
assert.NoError(t, err)
assert.Equal(t, "a", r1)
}
9 changes: 9 additions & 0 deletions opts.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package jinja2

import "github.com/kluctl/go-embed-python/python"

type jinja2Options struct {
DebugTrace bool `json:"debugTrace"`
NonStrict bool `json:"nonStrict"`
Expand All @@ -13,6 +15,7 @@ type jinja2Options struct {
Extensions []string `json:"extensions"`

// not passed to renderer
python python.Python
pythonPath []string
embeddedExtractDir string
templateIgnoreRootPath string
Expand All @@ -28,6 +31,12 @@ func WithDebugTrace(debugTrace bool) Jinja2Opt {
}
}

func WithPython(p python.Python) Jinja2Opt {
return func(o *jinja2Options) {
o.python = p
}
}

func WithPythonPath(p string) Jinja2Opt {
return func(o *jinja2Options) {
o.pythonPath = append(o.pythonPath, p)
Expand Down

0 comments on commit 3e435dc

Please sign in to comment.