Skip to content

Commit

Permalink
Refactoring code for yunabe#58
Browse files Browse the repository at this point in the history
  • Loading branch information
jamdagni86 committed May 25, 2018
1 parent 6fb07fe commit 815ce4b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
51 changes: 49 additions & 2 deletions cmd/lgo-internal/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,29 @@ import (
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"os"
"os/user"
"path"
"path/filepath"
"runtime/debug"
"sort"
"strings"
"time"
"unicode/utf8"

"github.com/golang/glog"
"github.com/yunabe/lgo/cmd/lgo-internal/liner"
"github.com/yunabe/lgo/cmd/runner"
"github.com/yunabe/lgo/core"
"github.com/yunabe/lgo/converter"
"github.com/yunabe/lgo/core"
scaffold "github.com/yunabe/lgo/jupyter/gojupyterscaffold"
)

const jupyterStartupDirectoryPath = ".ipython/profile_default/startup"

type handlers struct {
runner *runner.LgoRunner
execCount int
Expand Down Expand Up @@ -250,7 +257,7 @@ func (*handlers) HandleGoFmt(req *scaffold.GoFmtRequest) (*scaffold.GoFmtReply,
}
return &scaffold.GoFmtReply{
Status: "ok",
Code: formatted,
Code: formatted,
}, nil
}

Expand All @@ -270,9 +277,49 @@ func kernelMain(lgopath string, sessID *runner.SessionID) {
glog.Fatalf("Failed to create a server: %v", err)
}

loadStartupScripts(server)

// Start the server loop
server.Loop()
// clean-up
glog.Infof("Clean the session: %s", sessID.Marshal())
runner.CleanSession(lgopath, sessID)
}

func loadStartupScripts(server *scaffold.Server) {
u, err := user.Current()
if err != nil {
glog.Errorf("Error while fetching current user directory: %v\n", err)
return
}

profileDirPath := path.Join(u.HomeDir, jupyterStartupDirectoryPath)
fileNames, err := ioutil.ReadDir(profileDirPath)
if err != nil {
glog.Errorf("Error while reading startup files from the profile directory: %v\n", err)
return
}

var files []string
for _, file := range fileNames {
if !file.IsDir() && filepath.Ext(file.Name()) == ".go" {
files = append(files, file.Name())
}
}

if files == nil || len(files) == 0 {
glog.Info("No startup files found, returning")
return
}

sort.Strings(files)
for _, file := range files {
code, err := ioutil.ReadFile(path.Join(profileDirPath, file))
if err != nil {
glog.Errorf("Error while loading startup file %s: %+v", file, err)
continue
}
server.ExecuteScript(string(code))
glog.Infof("Loaded %s\n", file)
}
}
5 changes: 5 additions & 0 deletions jupyter/gojupyterscaffold/gojupyterscaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,8 @@ func (s *Server) Loop() {

// TODO: Support stdin.
}

func (s *Server) ExecuteScript(src string) {
req := &message{Content: &ExecuteRequest{Code: src}}
s.execQueue.queue <- &executeQueueItem{req, s.shell}
}

0 comments on commit 815ce4b

Please sign in to comment.