Skip to content

Commit

Permalink
Implement hooks around of cell execution
Browse files Browse the repository at this point in the history
The following four events are implemented in this commit.

- `pre_execute` -- occurred before every code execution
- `pre_run_cell` -- occurred before every non-silent code execution
- `post_execute` -- occurred after every code execution
- `post_run_cell` -- occurred after every non-silent code execution

`pre_execute` and `post_execute` events do not take any argument.
`pre_run_cell` event takes one argument, that is an `IRuby::ExecutionInfo`
object.  `post_run_cell` event takes one argument, that is the result of
the code execution.
  • Loading branch information
mrkn committed May 24, 2021
1 parent 6b54a0e commit 095acbd
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
35 changes: 32 additions & 3 deletions lib/iruby/kernel.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module IRuby
ExecutionInfo = Struct.new(:raw_cell, :store_history, :silent)

class Kernel
RED = "\e[31m"
RESET = "\e[0m"
Expand All @@ -9,6 +11,13 @@ class Kernel

attr_reader :session

EVENTS = [
:pre_execute,
:pre_run_cell,
:post_run_cell,
:post_execute
].freeze

def initialize(config_file, session_adapter_name=nil)
@config = MultiJson.load(File.read(config_file))
IRuby.logger.debug("IRuby kernel start with config #{@config}")
Expand All @@ -20,11 +29,14 @@ def initialize(config_file, session_adapter_name=nil)

init_parent_process_poller

@events = EventManager.new(EVENTS)
@execution_count = 0
@backend = create_backend
@running = true
end

attr_reader :events

def create_backend
PryBackend.new
rescue Exception => e
Expand Down Expand Up @@ -83,18 +95,31 @@ def send_status(status)

def execute_request(msg)
code = msg[:content]['code']
@execution_count += 1 if msg[:content]['store_history']
@session.send(:publish, :execute_input, code: code, execution_count: @execution_count)
store_history = msg[:content]['store_history']
silent = msg[:content]['silent']

@execution_count += 1 if store_history

unless silent
@session.send(:publish, :execute_input, code: code, execution_count: @execution_count)
end

events.trigger(:pre_execute)
unless silent
exec_info = ExecutionInfo.new(code, store_history, silent)
events.trigger(:pre_run_cell, exec_info)
end

content = {
status: :ok,
payload: [],
user_expressions: {},
execution_count: @execution_count
}

result = nil
begin
result = @backend.eval(code, msg[:content]['store_history'])
result = @backend.eval(code, store_history)
rescue SystemExit
content[:payload] << { source: :ask_exit }
rescue Exception => e
Expand All @@ -103,6 +128,10 @@ def execute_request(msg)
content[:status] = :error
content[:execution_count] = @execution_count
end

events.trigger(:post_execute)
events.trigger(:post_run_cell, result) unless silent

@session.send(:reply, :execute_reply, content)
@session.send(:publish, :execute_result,
data: Display.display(result),
Expand Down
54 changes: 54 additions & 0 deletions test/iruby/kernel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,59 @@ def inspect
}
})
end

def test_events_around_of_execute_request
event_history = []

@kernel.events.register(:pre_execute) do
event_history << :pre_execute
end

@kernel.events.register(:pre_run_cell) do |exec_info|
event_history << [:pre_run_cell, exec_info]
end

@kernel.events.register(:post_execute) do
event_history << :post_execute
end

@kernel.events.register(:post_run_cell) do |result|
event_history << [:post_run_cell, result]
end

msg = {
content: {
"code" => "true",
"silent" => false,
"store_history" => false,
"user_expressions" => {},
"allow_stdin" => false,
"stop_on_error" => true,
}
}
@kernel.execute_request(msg)

msg = {
content: {
"code" => "true",
"silent" => true,
"store_history" => false,
"user_expressions" => {},
"allow_stdin" => false,
"stop_on_error" => true,
}
}
@kernel.execute_request(msg)

assert_equal([
:pre_execute,
[:pre_run_cell, IRuby::ExecutionInfo.new("true", false, false)],
:post_execute,
[:post_run_cell, true],
:pre_execute,
:post_execute
],
event_history)
end
end
end

0 comments on commit 095acbd

Please sign in to comment.