Skip to content

Commit b03694c

Browse files
authored
Merge pull request #29 from gopherds/refactor
var names, linting
2 parents 4bcda18 + 9919b79 commit b03694c

File tree

2 files changed

+50
-47
lines changed

2 files changed

+50
-47
lines changed

execution.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package main
22

33
import (
44
"fmt"
5-
repl "github.com/gophergala2016/gophernotes/replpkg"
65
"go/token"
6+
7+
repl "github.com/gophergala2016/gophernotes/replpkg"
78
)
89

910
// REPLSession manages the I/O to/from the notebook
@@ -69,7 +70,7 @@ func HandleExecuteRequest(receipt MsgReceipt) {
6970
outContent.Data["text/plain"] = fmt.Sprint(val)
7071
outContent.Metadata = make(map[string]interface{})
7172
out.Content = outContent
72-
receipt.SendResponse(receipt.Sockets.IOPub_socket, out)
73+
receipt.SendResponse(receipt.Sockets.IOPubSocket, out)
7374
}
7475
} else {
7576
content["status"] = "error"
@@ -78,13 +79,13 @@ func HandleExecuteRequest(receipt MsgReceipt) {
7879
content["traceback"] = []string{stderr.String()}
7980
errormsg := NewMsg("pyerr", receipt.Msg)
8081
errormsg.Content = ErrMsg{"Error", err.Error(), []string{stderr.String()}}
81-
receipt.SendResponse(receipt.Sockets.IOPub_socket, errormsg)
82+
receipt.SendResponse(receipt.Sockets.IOPubSocket, errormsg)
8283
}
8384

8485
// send the output back to the notebook
8586
reply.Content = content
86-
receipt.SendResponse(receipt.Sockets.Shell_socket, reply)
87+
receipt.SendResponse(receipt.Sockets.ShellSocket, reply)
8788
idle := NewMsg("status", receipt.Msg)
8889
idle.Content = KernelStatus{"idle"}
89-
receipt.SendResponse(receipt.Sockets.IOPub_socket, idle)
90+
receipt.SendResponse(receipt.Sockets.IOPubSocket, idle)
9091
}

gophernotes.go

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,63 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6-
zmq "github.com/gophergala2016/gophernotes/Godeps/_workspace/src/github.com/alecthomas/gozmq"
76
"io"
87
"io/ioutil"
98
"log"
109
"os"
10+
11+
zmq "github.com/gophergala2016/gophernotes/Godeps/_workspace/src/github.com/alecthomas/gozmq"
1112
)
1213

1314
var logger *log.Logger
1415

1516
// ConnectionInfo stores the contents of the kernel connection file created by Jupyter.
1617
type ConnectionInfo struct {
17-
Signature_scheme string
18-
Transport string
19-
Stdin_port int
20-
Control_port int
21-
IOPub_port int
22-
HB_port int
23-
Shell_port int
24-
Key string
25-
IP string
18+
SignatureScheme string `json:"signature_scheme"`
19+
Transport string `json:"transport"`
20+
StdinPort int `json:"stdin_port"`
21+
ControlPort int `json:"control_port"`
22+
IOPubPort int `json:"iopub_port"`
23+
HBPort int `json:"hb_port"`
24+
ShellPort int `json:"shell_port"`
25+
Key string `json:"key"`
26+
IP string `json:"ip"`
2627
}
2728

2829
// SocketGroup holds the sockets needed to communicate with the kernel, and
2930
// the key for message signing.
3031
type SocketGroup struct {
31-
Shell_socket *zmq.Socket
32-
Control_socket *zmq.Socket
33-
Stdin_socket *zmq.Socket
34-
IOPub_socket *zmq.Socket
35-
Key []byte
32+
ShellSocket *zmq.Socket
33+
ControlSocket *zmq.Socket
34+
StdinSocket *zmq.Socket
35+
IOPubSocket *zmq.Socket
36+
Key []byte
3637
}
3738

3839
// PrepareSockets sets up the ZMQ sockets through which the kernel will communicate.
39-
func PrepareSockets(conn_info ConnectionInfo) (sg SocketGroup) {
40+
func PrepareSockets(connInfo ConnectionInfo) (sg SocketGroup) {
4041

42+
// TODO handle errors.
4143
context, _ := zmq.NewContext()
42-
sg.Shell_socket, _ = context.NewSocket(zmq.ROUTER)
43-
sg.Control_socket, _ = context.NewSocket(zmq.ROUTER)
44-
sg.Stdin_socket, _ = context.NewSocket(zmq.ROUTER)
45-
sg.IOPub_socket, _ = context.NewSocket(zmq.PUB)
44+
sg.ShellSocket, _ = context.NewSocket(zmq.ROUTER)
45+
sg.ControlSocket, _ = context.NewSocket(zmq.ROUTER)
46+
sg.StdinSocket, _ = context.NewSocket(zmq.ROUTER)
47+
sg.IOPubSocket, _ = context.NewSocket(zmq.PUB)
4648

47-
address := fmt.Sprintf("%v://%v:%%v", conn_info.Transport, conn_info.IP)
49+
address := fmt.Sprintf("%v://%v:%%v", connInfo.Transport, connInfo.IP)
4850

49-
sg.Shell_socket.Bind(fmt.Sprintf(address, conn_info.Shell_port))
50-
sg.Control_socket.Bind(fmt.Sprintf(address, conn_info.Control_port))
51-
sg.Stdin_socket.Bind(fmt.Sprintf(address, conn_info.Stdin_port))
52-
sg.IOPub_socket.Bind(fmt.Sprintf(address, conn_info.IOPub_port))
51+
sg.ShellSocket.Bind(fmt.Sprintf(address, connInfo.ShellPort))
52+
sg.ControlSocket.Bind(fmt.Sprintf(address, connInfo.ControlPort))
53+
sg.StdinSocket.Bind(fmt.Sprintf(address, connInfo.StdinPort))
54+
sg.IOPubSocket.Bind(fmt.Sprintf(address, connInfo.IOPubPort))
5355

5456
// Message signing key
55-
sg.Key = []byte(conn_info.Key)
57+
sg.Key = []byte(connInfo.Key)
5658

5759
// Start the heartbeat device
58-
HB_socket, _ := context.NewSocket(zmq.REP)
59-
HB_socket.Bind(fmt.Sprintf(address, conn_info.HB_port))
60-
go zmq.Device(zmq.FORWARDER, HB_socket, HB_socket)
60+
HBSocket, _ := context.NewSocket(zmq.REP)
61+
HBSocket.Bind(fmt.Sprintf(address, connInfo.HBPort))
62+
go zmq.Device(zmq.FORWARDER, HBSocket, HBSocket)
6163

6264
return
6365
}
@@ -78,8 +80,8 @@ func HandleShellMsg(receipt MsgReceipt) {
7880

7981
// KernelInfo holds information about the igo kernel, for kernel_info_reply messages.
8082
type KernelInfo struct {
81-
Protocol_version []int `json:"protocol_version"`
82-
Language string `json:"language"`
83+
ProtocolVersion []int `json:"protocol_version"`
84+
Language string `json:"language"`
8385
}
8486

8587
// KernelStatus holds a kernel state, for status broadcast messages.
@@ -91,7 +93,7 @@ type KernelStatus struct {
9193
func SendKernelInfo(receipt MsgReceipt) {
9294
reply := NewMsg("kernel_info_reply", receipt.Msg)
9395
reply.Content = KernelInfo{[]int{4, 0}, "go"}
94-
receipt.SendResponse(receipt.Sockets.Shell_socket, reply)
96+
receipt.SendResponse(receipt.Sockets.ShellSocket, reply)
9597
}
9698

9799
// ShutdownReply encodes a boolean indication of stutdown/restart
@@ -105,38 +107,38 @@ func HandleShutdownRequest(receipt MsgReceipt) {
105107
content := receipt.Msg.Content.(map[string]interface{})
106108
restart := content["restart"].(bool)
107109
reply.Content = ShutdownReply{restart}
108-
receipt.SendResponse(receipt.Sockets.Shell_socket, reply)
110+
receipt.SendResponse(receipt.Sockets.ShellSocket, reply)
109111
logger.Println("Shutting down in response to shutdown_request")
110112
os.Exit(0)
111113
}
112114

113115
// RunKernel is the main entry point to start the kernel. This is what is called by the
114116
// gophernotes executable.
115-
func RunKernel(connection_file string, logwriter io.Writer) {
117+
func RunKernel(connectionFile string, logwriter io.Writer) {
116118

117119
logger = log.New(logwriter, "gophernotes ", log.LstdFlags)
118120

119121
// set up the "Session" with the replpkg
120122
SetupExecutionEnvironment()
121123

122-
var conn_info ConnectionInfo
123-
bs, err := ioutil.ReadFile(connection_file)
124+
var connInfo ConnectionInfo
125+
bs, err := ioutil.ReadFile(connectionFile)
124126
if err != nil {
125127
log.Fatalln(err)
126128
}
127-
err = json.Unmarshal(bs, &conn_info)
129+
err = json.Unmarshal(bs, &connInfo)
128130
if err != nil {
129131
log.Fatalln(err)
130132
}
131-
logger.Printf("%+v\n", conn_info)
133+
logger.Printf("%+v\n", connInfo)
132134

133135
// Set up the ZMQ sockets through which the kernel will communicate
134-
sockets := PrepareSockets(conn_info)
136+
sockets := PrepareSockets(connInfo)
135137

136138
pi := zmq.PollItems{
137-
zmq.PollItem{Socket: sockets.Shell_socket, Events: zmq.POLLIN},
138-
zmq.PollItem{Socket: sockets.Stdin_socket, Events: zmq.POLLIN},
139-
zmq.PollItem{Socket: sockets.Control_socket, Events: zmq.POLLIN},
139+
zmq.PollItem{Socket: sockets.ShellSocket, Events: zmq.POLLIN},
140+
zmq.PollItem{Socket: sockets.StdinSocket, Events: zmq.POLLIN},
141+
zmq.PollItem{Socket: sockets.ControlSocket, Events: zmq.POLLIN},
140142
}
141143

142144
var msgparts [][]byte

0 commit comments

Comments
 (0)