1
1
package main
2
2
3
- // This file deals with executing user code and sending output messages.
4
-
5
3
import (
6
- "fmt"
7
- eval "github.com/sbinet/go-eval"
8
- repl "github.com/dwhitena/gophernotes/replpkg"
9
- "go/token"
4
+ "fmt"
5
+ repl "github.com/dwhitena/gophernotes/replpkg"
6
+ "go/token"
10
7
)
11
8
12
- // World holds the user namespace for the REPL.
9
+ // REPLSession manages the I/O to/from the notebook
13
10
var REPLSession * repl.Session
14
- var World * eval.World
15
11
var fset * token.FileSet
16
- // ExecCounter is incremented each time we run user code.
12
+
13
+ // ExecCounter is incremented each time we run user code in the notebook
17
14
var ExecCounter int = 0
18
15
16
+ // SetupExecutionEnvironment initializes the REPL session and set of tmp files
19
17
func SetupExecutionEnvironment () {
20
18
21
- var err error
22
- REPLSession , err = repl .NewSession ()
23
- if err != nil {
24
- panic (err )
25
- }
26
-
27
- World = eval .NewWorld ()
28
- fset = token .NewFileSet ()
29
- }
19
+ var err error
20
+ REPLSession , err = repl .NewSession ()
21
+ if err != nil {
22
+ panic (err )
23
+ }
30
24
31
- // RunCode runs the given user code, returning the expression value and/or an error.
32
- func RunCode (text string ) (val interface {}, err error ) {
33
- var code eval.Code
34
- code , err = World .Compile (fset , text )
35
- if err != nil {
36
- return nil , err
37
- }
38
- val , err = code .Run ()
39
- return
25
+ fset = token .NewFileSet ()
40
26
}
41
27
42
28
// OutputMsg holds the data for a pyout message.
43
29
type OutputMsg struct {
44
- Execcount int `json:"execution_count"`
45
- Data map [string ]string `json:"data"`
46
- Metadata map [string ]interface {} `json:"metadata"`
30
+ Execcount int `json:"execution_count"`
31
+ Data map [string ]string `json:"data"`
32
+ Metadata map [string ]interface {} `json:"metadata"`
47
33
}
48
34
35
+ // ErrMsg encodes the traceback of errors output to the notebook
49
36
type ErrMsg struct {
50
- EName string `json:"ename"`
51
- EValue string `json:"evalue"`
52
- Traceback []string `json:"traceback"`
37
+ EName string `json:"ename"`
38
+ EValue string `json:"evalue"`
39
+ Traceback []string `json:"traceback"`
53
40
}
54
41
55
42
// HandleExecuteRequest runs code from an execute_request method, and sends the various
56
43
// reply messages.
57
44
func HandleExecuteRequest (receipt MsgReceipt ) {
58
- reply := NewMsg ("execute_reply" , receipt .Msg )
59
- content := make (map [string ]interface {})
60
- reqcontent := receipt .Msg .Content .(map [string ]interface {})
61
- code := reqcontent ["code" ].(string )
62
- silent := reqcontent ["silent" ].(bool )
63
- if ! silent {
64
- ExecCounter ++
65
- }
66
- content ["execution_count" ] = ExecCounter
67
-
68
- // this is where the magic will happen, need to replace the "RunCode" of go-eval
69
- // with customer gore functionality
70
45
71
- //testerr := REPLSession.Eval(code)
72
- val , err , stderr := REPLSession .Eval (code )
73
- //val, err := RunCode(code)
46
+ reply := NewMsg ("execute_reply" , receipt .Msg )
47
+ content := make (map [string ]interface {})
48
+ reqcontent := receipt .Msg .Content .(map [string ]interface {})
49
+ code := reqcontent ["code" ].(string )
50
+ silent := reqcontent ["silent" ].(bool )
51
+ if ! silent {
52
+ ExecCounter ++
53
+ }
54
+ content ["execution_count" ] = ExecCounter
74
55
56
+ // the compilation/execution magic happen here
57
+ val , err , stderr := REPLSession .Eval (code )
75
58
59
+ if err == nil {
60
+ content ["status" ] = "ok"
61
+ content ["payload" ] = make ([]map [string ]interface {}, 0 )
62
+ content ["user_variables" ] = make (map [string ]string )
63
+ content ["user_expressions" ] = make (map [string ]string )
64
+ if len (val ) > 0 && ! silent {
65
+ var out_content OutputMsg
66
+ out := NewMsg ("pyout" , receipt .Msg )
67
+ out_content .Execcount = ExecCounter
68
+ out_content .Data = make (map [string ]string )
69
+ out_content .Data ["text/plain" ] = fmt .Sprint (val )
70
+ out_content .Metadata = make (map [string ]interface {})
71
+ out .Content = out_content
72
+ receipt .SendResponse (receipt .Sockets .IOPub_socket , out )
73
+ }
74
+ } else {
75
+ content ["status" ] = "error"
76
+ content ["ename" ] = "ERROR"
77
+ content ["evalue" ] = err .Error ()
78
+ //content["traceback"] = []string{err.Error()}
79
+ content ["traceback" ] = []string {stderr .String ()}
80
+ errormsg := NewMsg ("pyerr" , receipt .Msg )
81
+ //errormsg.Content = ErrMsg{"Error", err.Error(), []string{err.Error()}}
82
+ errormsg .Content = ErrMsg {"Error" , err .Error (), []string {stderr .String ()}}
83
+ receipt .SendResponse (receipt .Sockets .IOPub_socket , errormsg )
84
+ }
76
85
77
- if err == nil {
78
- content ["status" ] = "ok"
79
- content ["payload" ] = make ([]map [string ]interface {}, 0 )
80
- content ["user_variables" ] = make (map [string ]string )
81
- content ["user_expressions" ] = make (map [string ]string )
82
- if len (val ) > 0 && ! silent {
83
- var out_content OutputMsg
84
- out := NewMsg ("pyout" , receipt .Msg )
85
- out_content .Execcount = ExecCounter
86
- out_content .Data = make (map [string ]string )
87
- out_content .Data ["text/plain" ] = fmt .Sprint (val )
88
- out_content .Metadata = make (map [string ]interface {})
89
- out .Content = out_content
90
- receipt .SendResponse (receipt .Sockets .IOPub_socket , out )
91
- }
92
- } else {
93
- content ["status" ] = "error"
94
- content ["ename" ] = "ERROR"
95
- content ["evalue" ] = err .Error ()
96
- //content["traceback"] = []string{err.Error()}
97
- content ["traceback" ] = []string {stderr .String ()}
98
- errormsg := NewMsg ("pyerr" , receipt .Msg )
99
- //errormsg.Content = ErrMsg{"Error", err.Error(), []string{err.Error()}}
100
- errormsg .Content = ErrMsg {"Error" , err .Error (), []string {stderr .String ()}}
101
- receipt .SendResponse (receipt .Sockets .IOPub_socket , errormsg )
102
- }
103
- reply .Content = content
104
- receipt .SendResponse (receipt .Sockets .Shell_socket , reply )
105
- idle := NewMsg ("status" , receipt .Msg )
106
- idle .Content = KernelStatus {"idle" }
107
- receipt .SendResponse (receipt .Sockets .IOPub_socket , idle )
108
- }
86
+ // send the output back to the notebook
87
+ reply .Content = content
88
+ receipt .SendResponse (receipt .Sockets .Shell_socket , reply )
89
+ idle := NewMsg ("status" , receipt .Msg )
90
+ idle .Content = KernelStatus {"idle" }
91
+ receipt .SendResponse (receipt .Sockets .IOPub_socket , idle )
92
+ }
0 commit comments