Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

potential fix for #24 #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion cmd/lgo-internal/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,17 @@ func (h *handlers) HandleExecuteRequest(ctx context.Context, r *scaffold.Execute
fmt.Fprintf(os.Stderr, "panic: %v\n\n%s", p, debug.Stack())
}
}()

var f func(core.LgoContext, string) error
trimmedCode := strings.TrimSpace(r.Code)
if strings.Index(trimmedCode, "\n") == -1 && strings.HasPrefix(trimmedCode, "%"){
f = h.runner.RunMagics
} else {
f = h.runner.Run
}

// Print the err in the notebook
if err = h.runner.Run(lgoCtx, r.Code); err != nil {
if err = f(lgoCtx, r.Code); err != nil {
runner.PrintError(os.Stderr, err)
}
}()
Expand Down
37 changes: 37 additions & 0 deletions cmd/lgo-internal/magics/line_magics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package magics

import (
"time"
"github.com/yunabe/lgo/core"
"io/ioutil"
"fmt"
"strings"
)

var magics = map[string]func(ctx core.LgoContext, source string, runner func(core.LgoContext, string) error) error{
"run": run,
"time": timeMagic,
}

func run(ctx core.LgoContext, source string, runner func(core.LgoContext, string) error) error {
srcBytes, err := ioutil.ReadFile(strings.TrimSpace(source))
if err != nil {
core.LgoPrintln(fmt.Sprintf("Error while reading the file %s: %v", source, err))
return err
}

runner(ctx, string(srcBytes))

return nil
}

func timeMagic(ctx core.LgoContext, source string, runner func(core.LgoContext, string) error) error {
start := time.Now()
runner(ctx, source)
core.LgoPrintln(fmt.Sprintf("time taken: %v", time.Since(start)))
return nil
}

func GetRegisteredMagics() map[string]func(ctx core.LgoContext, source string, runner func(core.LgoContext, string) error) error {
return magics
}
22 changes: 22 additions & 0 deletions cmd/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/yunabe/lgo/cmd/install"
"github.com/yunabe/lgo/converter"
"github.com/yunabe/lgo/core"
"github.com/yunabe/lgo/cmd/lgo-internal/magics"
)

/*
Expand Down Expand Up @@ -259,3 +260,24 @@ func (rn *LgoRunner) Inspect(ctx context.Context, src string, index int) (string
}
return strings.Replace(buf.String(), lgoExportPrefix, "", -1), nil
}

func (rn *LgoRunner) RunMagics(ctx core.LgoContext, src string) error {
rn.execCount++
magicFunctionWithArgs := strings.TrimSpace(src)[1:]
magic := magicFunctionWithArgs
magicFunctionIndex := strings.Index(magicFunctionWithArgs, " ")
var arguments string
if magicFunctionIndex != -1 {
magic = magicFunctionWithArgs[:magicFunctionIndex]
arguments = magicFunctionWithArgs[magicFunctionIndex:]
}

if f, ok := magics.GetRegisteredMagics()[magic]; ok {
f(ctx, arguments, rn.Run)
} else {
core.LgoPrintln(fmt.Sprintf("UsageError: Line magic function `%%%s` not found.", magic))
return nil
}

return nil
}