-
Notifications
You must be signed in to change notification settings - Fork 37
/
starlark.go
768 lines (662 loc) · 17.5 KB
/
starlark.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
package main
import (
"bytes"
"errors"
"fmt"
"log"
"net"
"net/url"
"sort"
"strconv"
"strings"
"sync"
"time"
re "github.com/magnetde/starlark-re"
"github.com/miekg/dns"
"github.com/qri-io/starlib/bsoup"
"github.com/qri-io/starlib/encoding/base64"
"github.com/qri-io/starlib/encoding/csv"
"github.com/qri-io/starlib/encoding/yaml"
"github.com/qri-io/starlib/hash"
"github.com/qri-io/starlib/html"
"github.com/qri-io/starlib/http"
"go.starlark.net/lib/json"
"go.starlark.net/lib/math"
starlark_time "go.starlark.net/lib/time"
"go.starlark.net/repl"
"go.starlark.net/resolve"
"go.starlark.net/starlark"
"go.starlark.net/syntax"
"golang.org/x/net/publicsuffix"
)
func init() {
// Enable some Starlark language features that are specific to the Go implementation.
resolve.AllowSet = true
resolve.AllowLambda = true
resolve.AllowRecursion = true
resolve.AllowGlobalReassign = true
starlark.Universe["json"] = json.Module
starlark.Universe["time"] = starlark_time.Module
starlark.Universe["math"] = math.Module
starlark.Universe["re"] = re.NewModule()
for name, loader := range starlib {
mod, err := loader()
if err != nil {
panic(err)
}
unwrapped, ok := mod[name]
if !ok {
panic(fmt.Errorf("wrong module name: %s", name))
}
starlark.Universe[name] = unwrapped
}
starlark.Universe["lookup_host"] = starlark.NewBuiltin("lookup_host", lookupHostStarlark)
starlark.Universe["lookup_addr"] = starlark.NewBuiltin("lookup_addr", lookupAddrStarlark)
starlark.Universe["urlparse"] = starlark.NewBuiltin("urlparse", urlparse)
starlark.Universe["parse_qs"] = starlark.NewBuiltin("parse_qs", parseQS)
starlark.Universe["publicsuffix"] = starlark.NewBuiltin("publicsuffix", publicsuffixStarlark)
starlark.Universe["privatesuffix"] = starlark.NewBuiltin("privatesuffix", privatesuffix)
starlark.Universe["CSVLog"] = starlark.NewBuiltin("CSVLog", customCSVLog)
}
var starlib = map[string]func() (starlark.StringDict, error){
"bsoup": bsoup.LoadModule,
"base64": base64.LoadModule,
"csv": csv.LoadModule,
"yaml": yaml.LoadModule,
"hash": hash.LoadModule,
"html": html.LoadModule,
"http": http.LoadModule,
}
type starlarkFunction func(...starlark.Value) (starlark.Value, error)
func newStarlarkThread() *starlark.Thread {
return &starlark.Thread{
Print: func(t *starlark.Thread, msg string) {
if starlarkLog.csv == nil {
// The log isn't set up (probably in test mode).
// Print to Stdout instead.
fmt.Println(msg)
return
}
starlarkLog.Log([]string{time.Now().Format("2006-01-02 15:04:05.000000"), "print", msg})
},
}
}
func (c *config) loadStarlarkScripts() {
if c.StarlarkFunctions == nil {
c.StarlarkFunctions = make(map[string][]starlarkFunction)
}
thread := newStarlarkThread()
thread.Load = repl.MakeLoad()
for _, script := range c.StarlarkScripts {
defs, err := starlark.ExecFile(thread, script, nil, nil)
if err != nil {
log.Printf("Error loading starlark script %s:\n%s", script, formatStarlarkError(err))
continue
}
// Collect the functions defined by the script.
for k, v := range defs {
if f, ok := v.(starlark.Callable); ok {
c.StarlarkFunctions[k] = append(c.StarlarkFunctions[k], func(args ...starlark.Value) (starlark.Value, error) {
return starlark.Call(newStarlarkThread(), f, starlark.Tuple(args), nil)
})
}
}
}
}
func callStarlarkFunctions(name string, args ...starlark.Value) {
for _, f := range getConfig().StarlarkFunctions[name] {
_, err := f(args...)
if err != nil {
logStarlarkError(err)
}
}
}
func formatStarlarkError(err error) string {
switch err := err.(type) {
case *starlark.EvalError:
return err.Backtrace()
default:
return err.Error()
}
}
func logStarlarkError(err error) {
if starlarkLog.csv == nil {
// The log isn't set up (probably in test mode).
// Print to Stdout instead.
fmt.Println(err)
return
}
starlarkLog.Log([]string{time.Now().Format("2006-01-02 15:04:05.000000"), "error", formatStarlarkError(err)})
}
func assignStarlarkString(dest *string, val starlark.Value) error {
switch val := val.(type) {
case starlark.String:
*dest = string(val)
return nil
default:
return fmt.Errorf("a string is required, not %s", val.Type())
}
}
// A StringSet is a set where all the elements are strings.
type StringSet struct {
frozen bool
itercount int
data map[string]bool
}
func (s *StringSet) elements() []string {
elems := make([]string, 0, len(s.data))
for k, v := range s.data {
if v {
elems = append(elems, k)
}
}
sort.Strings(elems)
return elems
}
func (s *StringSet) String() string {
b := new(bytes.Buffer)
b.WriteString("{")
for i, v := range s.elements() {
if i > 0 {
b.WriteString(", ")
}
fmt.Fprintf(b, "%q", v)
}
b.WriteString("}")
return b.String()
}
func (s *StringSet) Type() string {
return "StringSet"
}
func (s *StringSet) Freeze() {
if !s.frozen {
s.frozen = true
}
}
func (s *StringSet) Truth() starlark.Bool {
return starlark.Bool(len(s.data) > 0)
}
func (s *StringSet) Hash() (uint32, error) {
return 0, errors.New("unhashable type: StringSet")
}
type stringSetIterator struct {
ss *StringSet
elements []string
}
func (it *stringSetIterator) Next(p *starlark.Value) bool {
if len(it.elements) > 0 {
*p = starlark.String(it.elements[0])
it.elements = it.elements[1:]
return true
}
return false
}
func (it *stringSetIterator) Done() {
if !it.ss.frozen {
it.ss.itercount--
}
}
func (s *StringSet) Iterate() starlark.Iterator {
if !s.frozen {
s.itercount++
}
return &stringSetIterator{
ss: s,
elements: s.elements(),
}
}
func (s *StringSet) Len() int {
return len(s.data)
}
func stringSetChangeElement(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
s := fn.Receiver().(*StringSet)
if s.frozen {
return nil, errors.New("can't modify a frozen StringSet")
}
if s.itercount > 0 {
return nil, errors.New("can't modify a StringSet during iteration")
}
var item string
if err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 1, &item); err != nil {
return nil, err
}
switch fn.Name() {
case "add":
s.data[item] = true
return starlark.None, nil
case "remove":
if _, ok := s.data[item]; !ok {
return nil, fmt.Errorf("KeyError: %q", item)
}
fallthrough
case "discard":
delete(s.data, item)
return starlark.None, nil
}
panic("unreachable")
}
var stringSetAttrNames = []string{"add", "remove", "discard"}
func (s *StringSet) AttrNames() []string {
return stringSetAttrNames
}
func (s *StringSet) Attr(name string) (starlark.Value, error) {
switch name {
case "add", "remove", "discard":
return starlark.NewBuiltin(name, stringSetChangeElement).BindReceiver(s), nil
default:
return nil, nil
}
}
func (s *StringSet) Binary(op syntax.Token, y starlark.Value, side starlark.Side) (starlark.Value, error) {
if op == syntax.IN && side == starlark.Right {
switch y := y.(type) {
case starlark.String:
return starlark.Bool(s.data[string(y)]), nil
default:
return starlark.False, nil
}
}
return nil, nil
}
// A StringIntDict is a dictionary where the keys are strings and the values
// are integers.
type StringIntDict struct {
frozen bool
itercount int
data map[string]int
}
func (s *StringIntDict) String() string {
b := new(bytes.Buffer)
b.WriteString("{")
for i, k := range sortedKeys(s.data) {
if i > 0 {
b.WriteString(", ")
}
fmt.Fprintf(b, "%q: %d", k, s.data[k])
}
b.WriteString("}")
return b.String()
}
func (s *StringIntDict) Type() string {
return "StringIntDict"
}
func (s *StringIntDict) Freeze() {
if !s.frozen {
s.frozen = true
}
}
func (s *StringIntDict) Truth() starlark.Bool {
return starlark.Bool(len(s.data) > 0)
}
func (s *StringIntDict) Hash() (uint32, error) {
return 0, errors.New("unhashable type: StringIntDict")
}
type stringIntDictIterator struct {
ss *StringIntDict
elements []string
}
func (it *stringIntDictIterator) Next(p *starlark.Value) bool {
if len(it.elements) > 0 {
*p = starlark.String(it.elements[0])
it.elements = it.elements[1:]
return true
}
return false
}
func (it *stringIntDictIterator) Done() {
if !it.ss.frozen {
it.ss.itercount--
}
}
func (s *StringIntDict) Iterate() starlark.Iterator {
if !s.frozen {
s.itercount++
}
return &stringIntDictIterator{
ss: s,
elements: sortedKeys(s.data),
}
}
func (s *StringIntDict) Len() int {
return len(s.data)
}
func (s *StringIntDict) Get(k starlark.Value) (v starlark.Value, found bool, err error) {
ks, ok := k.(starlark.String)
if !ok {
return nil, false, nil
}
val, ok := s.data[string(ks)]
if !ok {
return nil, false, nil
}
return starlark.MakeInt(val), true, nil
}
func (s *StringIntDict) SetKey(k, v starlark.Value) error {
ks, ok := k.(starlark.String)
if !ok {
return fmt.Errorf("keys for StringIntDict must be String, not %s", k.Type())
}
vi, ok := v.(starlark.Int)
if !ok {
return fmt.Errorf("values for StringIntDict must be Int, not %s", v.Type())
}
vi64, ok := vi.Int64()
i := int(vi64)
if !ok || vi64 != int64(i) {
return fmt.Errorf("%d is too large to store in a StringIntDict", v)
}
s.data[string(ks)] = i
return nil
}
var stringIntDictAttrNames = []string{"get", "pop"}
func (s *StringIntDict) AttrNames() []string {
return stringIntDictAttrNames
}
func (s *StringIntDict) Attr(name string) (starlark.Value, error) {
switch name {
case "get", "pop":
return starlark.NewBuiltin(name, stringIntDictGet).BindReceiver(s), nil
default:
return nil, nil
}
}
func stringIntDictGet(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
pop := fn.Name() == "pop"
s := fn.Receiver().(*StringIntDict)
if pop {
if s.frozen {
return nil, errors.New("can't modify a frozen StringIntDict")
}
if s.itercount > 0 {
return nil, errors.New("can't modify a StringIntDict during iteration")
}
}
var key string
var defaultValue starlark.Value
if err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 1, &key, &defaultValue); err != nil {
return nil, err
}
if defaultValue == nil && !pop {
defaultValue = starlark.None
}
v, ok := s.data[key]
if !ok {
if defaultValue != nil {
return defaultValue, nil
}
return nil, fmt.Errorf("key %q not in dict", key)
}
if pop {
delete(s.data, key)
}
return starlark.MakeInt(v), nil
}
func (s *StringIntDict) Items() (result []starlark.Tuple) {
for _, k := range sortedKeys(s.data) {
result = append(result, starlark.Tuple{
starlark.String(k),
starlark.MakeInt(s.data[k]),
})
}
return result
}
func lookupHostStarlark(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var host, server string
if err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 1, &host, &server); err != nil {
return nil, err
}
var result string
var err error
if len(args) == 1 {
result, err = lookupHost(host)
} else {
result, err = lookupHost(host, server)
}
return starlark.String(result), err
}
func lookupAddrStarlark(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var addr, server string
if err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 1, &addr, &server); err != nil {
return nil, err
}
if len(args) == 1 {
names, err := net.LookupAddr(addr)
if err != nil {
var dnsError *net.DNSError
if errors.As(err, &dnsError) {
if dnsError.IsNotFound {
return starlark.String(""), nil
}
}
return nil, err
}
if len(names) == 0 {
return starlark.String(""), nil
}
return starlark.String(strings.TrimSuffix(names[0], ".")), nil
}
reverse, err := dns.ReverseAddr(addr)
if err != nil {
return nil, err
}
m := new(dns.Msg)
m.SetQuestion(reverse, dns.TypePTR)
resp, err := dns.Exchange(m, net.JoinHostPort(server, "53"))
if err != nil {
return nil, err
}
for _, a := range resp.Answer {
if a, ok := a.(*dns.PTR); ok {
return starlark.String(strings.TrimSuffix(a.Ptr, ".")), nil
}
}
return starlark.String(""), nil
}
func stringTuple(s []string) starlark.Tuple {
t := make(starlark.Tuple, len(s))
for i, v := range s {
t[i] = starlark.String(v)
}
return t
}
// A SyncDict wraps a starlark.Dict, and adds locking for concurrent access.
type SyncDict struct {
d starlark.Dict
lock sync.Mutex
}
func (d *SyncDict) Clear() error {
d.lock.Lock()
defer d.lock.Unlock()
return d.d.Clear()
}
func (d *SyncDict) Delete(k starlark.Value) (v starlark.Value, found bool, err error) {
d.lock.Lock()
defer d.lock.Unlock()
return d.d.Delete(k)
}
func (d *SyncDict) Get(k starlark.Value) (v starlark.Value, found bool, err error) {
d.lock.Lock()
defer d.lock.Unlock()
return d.d.Get(k)
}
func (d *SyncDict) Items() []starlark.Tuple {
d.lock.Lock()
defer d.lock.Unlock()
return d.d.Items()
}
func (d *SyncDict) Keys() []starlark.Value {
d.lock.Lock()
defer d.lock.Unlock()
return d.d.Keys()
}
func (d *SyncDict) Len() int {
d.lock.Lock()
defer d.lock.Unlock()
return d.d.Len()
}
func (d *SyncDict) SetKey(k, v starlark.Value) error {
k.Freeze()
v.Freeze()
d.lock.Lock()
defer d.lock.Unlock()
return d.d.SetKey(k, v)
}
func (d *SyncDict) String() string {
d.lock.Lock()
defer d.lock.Unlock()
return d.d.String()
}
func (d *SyncDict) Type() string {
return "SyncDict"
}
func (d *SyncDict) Freeze() {
// Do nothing; a SyncDict is thread-safe and doesn't need to actually freeze.
}
func (d *SyncDict) Truth() starlark.Bool {
d.lock.Lock()
defer d.lock.Unlock()
return d.Truth()
}
func (d *SyncDict) Hash() (uint32, error) {
return 0, fmt.Errorf("unhashable type: SyncDict")
}
func (d *SyncDict) doMethod(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
d.lock.Lock()
defer d.lock.Unlock()
switch fn.Name() {
case "setdefault":
if len(args) >= 2 {
args[1].Freeze()
}
}
innerMethod, err := d.d.Attr(fn.Name())
if err != nil {
return starlark.None, err
}
return starlark.Call(thread, innerMethod, args, kwargs)
}
func (d *SyncDict) AttrNames() []string {
return []string{
"clear",
"get",
"items",
"keys",
"pop",
"popitem",
"setdefault",
"values",
}
}
func (d *SyncDict) Attr(name string) (starlark.Value, error) {
switch name {
case "clear", "get", "items", "keys", "pop", "popitem", "setdefault", "values":
return starlark.NewBuiltin(name, d.doMethod), nil
default:
return nil, nil
}
}
type urlParseResult struct {
starlark.Tuple
}
func (u urlParseResult) Type() string {
return "ParseResult"
}
func (u urlParseResult) AttrNames() []string {
return []string{"scheme", "netloc", "path", "params", "query", "fragment", "hostname", "port"}
}
func (u urlParseResult) Attr(name string) (starlark.Value, error) {
switch name {
default:
return nil, nil
case "scheme":
return u.Tuple[0], nil
case "netloc":
return u.Tuple[1], nil
case "path":
return u.Tuple[2], nil
case "params":
return u.Tuple[3], nil
case "query":
return u.Tuple[4], nil
case "fragment":
return u.Tuple[5], nil
case "hostname":
netloc := string(u.Tuple[1].(starlark.String))
if netloc == "" {
return starlark.None, nil
}
host, _, err := net.SplitHostPort(netloc)
if err != nil {
return starlark.String(netloc), nil
}
return starlark.String(host), nil
case "port":
netloc := string(u.Tuple[1].(starlark.String))
if netloc == "" {
return starlark.None, nil
}
_, port, err := net.SplitHostPort(netloc)
if err != nil {
return starlark.None, nil
}
p, err := strconv.Atoi(port)
if err != nil {
return nil, err
}
return starlark.MakeInt(p), nil
}
}
func urlparse(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var urlString string
if err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 1, &urlString); err != nil {
return nil, err
}
parsed, err := url.Parse(urlString)
if err != nil {
return nil, err
}
return urlParseResult{starlark.Tuple{
starlark.String(parsed.Scheme),
starlark.String(parsed.Host),
starlark.String(parsed.Path),
starlark.String(""),
starlark.String(parsed.RawQuery),
starlark.String(parsed.Fragment),
}}, nil
}
func parseQS(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var queryString string
if err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 1, &queryString); err != nil {
return nil, err
}
parsed, _ := url.ParseQuery(queryString)
// Ignore errors like the Python original does.
d := starlark.NewDict(len(parsed))
for k, values := range parsed {
t := make(starlark.Tuple, len(values))
for i, v := range values {
t[i] = starlark.String(v)
}
d.SetKey(starlark.String(k), t)
}
return d, nil
}
func publicsuffixStarlark(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var hostname string
if err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 1, &hostname); err != nil {
return nil, err
}
suffix, _ := publicsuffix.PublicSuffix(hostname)
return starlark.String(suffix), nil
}
func privatesuffix(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var hostname string
if err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 1, &hostname); err != nil {
return nil, err
}
suffix, err := publicsuffix.EffectiveTLDPlusOne(hostname)
if err != nil {
return starlark.None, nil
}
return starlark.String(suffix), nil
}