Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use interface instead of struct for Python #44

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading