Skip to content

Commit e01b978

Browse files
authored
feat: Use interface instead of struct for Python (#44)
1 parent b1e2e38 commit e01b978

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

python/embedded_python.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
type EmbeddedPython struct {
1010
e *embed_util.EmbeddedFiles
11-
*Python
11+
Python
1212
}
1313

1414
// NewEmbeddedPython creates a new EmbeddedPython instance. The embedded source code and python binaries are

python/python.go

+18-10
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,29 @@ import (
99
"strings"
1010
)
1111

12-
type Python struct {
12+
type Python interface {
13+
GetExeName() string
14+
GetExePath() (string, error)
15+
AddPythonPath(p string)
16+
PythonCmd(args ...string) (*exec.Cmd, error)
17+
PythonCmd2(args []string) (*exec.Cmd, error)
18+
}
19+
20+
type python struct {
1321
pythonHome string
1422
pythonPath []string
1523
}
1624

17-
type PythonOpt func(o *Python)
25+
type PythonOpt func(o *python)
1826

1927
func WithPythonHome(home string) PythonOpt {
20-
return func(o *Python) {
28+
return func(o *python) {
2129
o.pythonHome = home
2230
}
2331
}
2432

25-
func NewPython(opts ...PythonOpt) *Python {
26-
ep := &Python{}
33+
func NewPython(opts ...PythonOpt) Python {
34+
ep := &python{}
2735

2836
for _, o := range opts {
2937
o(ep)
@@ -32,7 +40,7 @@ func NewPython(opts ...PythonOpt) *Python {
3240
return ep
3341
}
3442

35-
func (ep *Python) GetExeName() string {
43+
func (ep *python) GetExeName() string {
3644
suffix := ""
3745
if runtime.GOOS == "windows" {
3846
suffix = ".exe"
@@ -42,7 +50,7 @@ func (ep *Python) GetExeName() string {
4250
return "python" + suffix
4351
}
4452

45-
func (ep *Python) GetExePath() (string, error) {
53+
func (ep *python) GetExePath() (string, error) {
4654
if ep.pythonHome == "" {
4755
p, err := exec.LookPath(ep.GetExeName())
4856
if err != nil {
@@ -63,15 +71,15 @@ func (ep *Python) GetExePath() (string, error) {
6371
}
6472
}
6573

66-
func (ep *Python) AddPythonPath(p string) {
74+
func (ep *python) AddPythonPath(p string) {
6775
ep.pythonPath = append(ep.pythonPath, p)
6876
}
6977

70-
func (ep *Python) PythonCmd(args ...string) (*exec.Cmd, error) {
78+
func (ep *python) PythonCmd(args ...string) (*exec.Cmd, error) {
7179
return ep.PythonCmd2(args)
7280
}
7381

74-
func (ep *Python) PythonCmd2(args []string) (*exec.Cmd, error) {
82+
func (ep *python) PythonCmd2(args []string) (*exec.Cmd, error) {
7583
exePath, err := ep.GetExePath()
7684
if err != nil {
7785
return nil, err

0 commit comments

Comments
 (0)