@@ -8,10 +8,26 @@ class Kernel
88 @events = EventManager . new ( [ :initialized ] )
99
1010 class << self
11+ # Return the event manager defined in the `IRuby::Kernel` class.
12+ # This event manager can handle the following event:
13+ #
14+ # - `initialized`: The event occurred after the initialization of
15+ # a `IRuby::Kernel` instance is finished
16+ #
17+ # @example Registering initialized event
18+ # IRuby::Kernel.events.register(:initialized) do |result|
19+ # STDERR.puts "IRuby kernel has been initialized"
20+ # end
21+ #
22+ # @see IRuby::EventManager
23+ # @see IRuby::Kernel#events
1124 attr_reader :events
25+
26+ # Returns the singleton kernel instance
1227 attr_accessor :instance
1328 end
1429
30+ # Returns a session object
1531 attr_reader :session
1632
1733 EVENTS = [
@@ -40,22 +56,51 @@ def initialize(config_file, session_adapter_name=nil)
4056 self . class . events . trigger ( :initialized , self )
4157 end
4258
59+ # Returns the event manager defined in a `IRuby::Kernel` instance.
60+ # This event manager can handle the following events:
61+ #
62+ # - `pre_execute`: The event occurred before running the code
63+ #
64+ # - `pre_run_cell`: The event occurred before running the code and
65+ # if the code execution is not silent
66+ #
67+ # - `post_execute`: The event occurred after running the code
68+ #
69+ # - `post_run_cell`: The event occurred after running the code and
70+ # if the code execution is not silent
71+ #
72+ # The callback functions of `pre_run_cell` event must take one argument
73+ # to get an `ExecutionInfo` object.
74+ # The callback functions of `post_run_cell` event must take one argument
75+ # to get the result of the code execution.
76+ #
77+ # @example Registering post_run_cell event
78+ # IRuby::Kernel.instance.events.register(:post_run_cell) do |result|
79+ # STDERR.puts "The result of the last execution: %p" % result
80+ # end
81+ #
82+ # @see IRuby::EventManager
83+ # @see IRuby::ExecutionInfo
84+ # @see IRuby::Kernel.events
4385 attr_reader :events
4486
87+ # @private
4588 def create_backend
4689 PryBackend . new
4790 rescue Exception => e
4891 IRuby . logger . warn "Could not load PryBackend: #{ e . message } \n #{ e . backtrace . join ( "\n " ) } " unless LoadError === e
4992 PlainBackend . new
5093 end
5194
95+ # @private
5296 def run
5397 send_status :starting
5498 while @running
5599 dispatch
56100 end
57101 end
58102
103+ # @private
59104 def dispatch
60105 msg = @session . recv ( :reply )
61106 IRuby . logger . debug "Kernel#dispatch: msg = #{ msg } "
@@ -72,6 +117,7 @@ def dispatch
72117 @session . send ( :publish , :error , error_content ( e ) )
73118 end
74119
120+ # @private
75121 def kernel_info_request ( msg )
76122 @session . send ( :reply , :kernel_info_reply ,
77123 protocol_version : '5.0' ,
@@ -93,11 +139,13 @@ def kernel_info_request(msg)
93139 status : :ok )
94140 end
95141
142+ # @private
96143 def send_status ( status )
97144 IRuby . logger . debug "Send status: #{ status } "
98145 @session . send ( :publish , :status , execution_state : status )
99146 end
100147
148+ # @private
101149 def execute_request ( msg )
102150 code = msg [ :content ] [ 'code' ]
103151 store_history = msg [ :content ] [ 'store_history' ]
@@ -147,6 +195,7 @@ def execute_request(msg)
147195 @session . send ( :reply , :execute_reply , content )
148196 end
149197
198+ # @private
150199 def error_content ( e )
151200 rindex = e . backtrace . rindex { |line | line . start_with? ( @backend . eval_path ) } || -1
152201 backtrace = SyntaxError === e && rindex == -1 ? [ ] : e . backtrace [ 0 ..rindex ]
@@ -155,12 +204,14 @@ def error_content(e)
155204 traceback : [ "#{ RED } #{ e . class } #{ RESET } : #{ e . message } " , *backtrace ] }
156205 end
157206
207+ # @private
158208 def is_complete_request ( msg )
159209 # FIXME: the code completeness should be judged by using ripper or other Ruby parser
160210 @session . send ( :reply , :is_complete_reply ,
161211 status : :unknown )
162212 end
163213
214+ # @private
164215 def complete_request ( msg )
165216 # HACK for #26, only complete last line
166217 code = msg [ :content ] [ 'code' ]
@@ -176,36 +227,43 @@ def complete_request(msg)
176227 status : :ok )
177228 end
178229
230+ # @private
179231 def connect_request ( msg )
180232 @session . send ( :reply , :connect_reply , Hash [ %w( shell_port iopub_port stdin_port hb_port ) . map { |k | [ k , @config [ k ] ] } ] )
181233 end
182234
235+ # @private
183236 def shutdown_request ( msg )
184237 @session . send ( :reply , :shutdown_reply , msg [ :content ] )
185238 @running = false
186239 end
187240
241+ # @private
188242 def history_request ( msg )
189243 # we will just send back empty history for now, pending clarification
190244 # as requested in ipython/ipython#3806
191245 @session . send ( :reply , :history_reply , history : [ ] )
192246 end
193247
248+ # @private
194249 def inspect_request ( msg )
195250 # not yet implemented. See (#119).
196251 @session . send ( :reply , :inspect_reply , status : :ok , found : false , data : { } , metadata : { } )
197252 end
198253
254+ # @private
199255 def comm_open ( msg )
200256 comm_id = msg [ :content ] [ 'comm_id' ]
201257 target_name = msg [ :content ] [ 'target_name' ]
202258 Comm . comm [ comm_id ] = Comm . target [ target_name ] . new ( target_name , comm_id )
203259 end
204260
261+ # @private
205262 def comm_msg ( msg )
206263 Comm . comm [ msg [ :content ] [ 'comm_id' ] ] . handle_msg ( msg [ :content ] [ 'data' ] )
207264 end
208265
266+ # @private
209267 def comm_close ( msg )
210268 comm_id = msg [ :content ] [ 'comm_id' ]
211269 Comm . comm [ comm_id ] . handle_close ( msg [ :content ] [ 'data' ] )
0 commit comments