-
Notifications
You must be signed in to change notification settings - Fork 8
/
plugin.go
82 lines (66 loc) · 1.46 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2016 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package sublime
import (
"path/filepath"
"github.com/limetext/backend/log"
"github.com/limetext/backend/packages"
"github.com/limetext/gopy"
)
// Sublime plugin which is a single python file
type plugin struct {
path string
name string
}
func newPlugin(fn string) packages.Package {
return &plugin{path: fn}
}
// TODO: implement unload
func (p *plugin) Load() {
dir, file := filepath.Split(p.Path())
p.name = file
name := filepath.Base(dir) + "." + file[:len(file)-3]
s, err := py.NewUnicode(name)
if err != nil {
log.Warn(err)
return
}
defer s.Decref()
log.Debug("Loading plugin %s", name)
l := py.NewLock()
defer l.Unlock()
if r, err := module.Base().CallMethodObjArgs("reload_plugin", s); err != nil {
log.Warn(err)
return
} else if r != nil {
r.Decref()
}
}
func (p *plugin) UnLoad() {}
func (p *plugin) Name() string {
return p.name
}
func (p *plugin) Path() string {
return p.path
}
func (p *plugin) FileChanged(name string) {
p.Load()
}
func isPlugin(filename string) bool {
return filepath.Ext(filename) == ".py"
}
var (
pluginRecord = &packages.Record{isPlugin, newPlugin}
module *py.Module
)
func pyAddPath(p string) {
l := py.NewLock()
defer l.Unlock()
py.AddToPath(p)
}
func pyImport(name string) (*py.Module, error) {
l := py.NewLock()
defer l.Unlock()
return py.Import(name)
}