Skip to content

Commit

Permalink
feat: Use interface instead of struct for Python (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
codablock authored Jun 17, 2024
1 parent b1e2e38 commit e01b978
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion python/embedded_python.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type EmbeddedPython struct {
e *embed_util.EmbeddedFiles
*Python
Python
}

// NewEmbeddedPython creates a new EmbeddedPython instance. The embedded source code and python binaries are
Expand Down
28 changes: 18 additions & 10 deletions python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,29 @@ import (
"strings"
)

type Python struct {
type Python interface {
GetExeName() string
GetExePath() (string, error)
AddPythonPath(p string)
PythonCmd(args ...string) (*exec.Cmd, error)
PythonCmd2(args []string) (*exec.Cmd, error)
}

type python struct {
pythonHome string
pythonPath []string
}

type PythonOpt func(o *Python)
type PythonOpt func(o *python)

func WithPythonHome(home string) PythonOpt {
return func(o *Python) {
return func(o *python) {
o.pythonHome = home
}
}

func NewPython(opts ...PythonOpt) *Python {
ep := &Python{}
func NewPython(opts ...PythonOpt) Python {
ep := &python{}

for _, o := range opts {
o(ep)
Expand All @@ -32,7 +40,7 @@ func NewPython(opts ...PythonOpt) *Python {
return ep
}

func (ep *Python) GetExeName() string {
func (ep *python) GetExeName() string {
suffix := ""
if runtime.GOOS == "windows" {
suffix = ".exe"
Expand All @@ -42,7 +50,7 @@ func (ep *Python) GetExeName() string {
return "python" + suffix
}

func (ep *Python) GetExePath() (string, error) {
func (ep *python) GetExePath() (string, error) {
if ep.pythonHome == "" {
p, err := exec.LookPath(ep.GetExeName())
if err != nil {
Expand All @@ -63,15 +71,15 @@ func (ep *Python) GetExePath() (string, error) {
}
}

func (ep *Python) AddPythonPath(p string) {
func (ep *python) AddPythonPath(p string) {
ep.pythonPath = append(ep.pythonPath, p)
}

func (ep *Python) PythonCmd(args ...string) (*exec.Cmd, error) {
func (ep *python) PythonCmd(args ...string) (*exec.Cmd, error) {
return ep.PythonCmd2(args)
}

func (ep *Python) PythonCmd2(args []string) (*exec.Cmd, error) {
func (ep *python) PythonCmd2(args []string) (*exec.Cmd, error) {
exePath, err := ep.GetExePath()
if err != nil {
return nil, err
Expand Down

0 comments on commit e01b978

Please sign in to comment.