Class: IRuby::Kernel

Inherits:
Object
  • Object
show all
Defined in:
lib/iruby/kernel.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, reply_socket, pub_socket) ⇒ Kernel

Returns a new instance of Kernel.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/iruby/kernel.rb', line 59

def initialize session, reply_socket, pub_socket
  @session = session
  @reply_socket = reply_socket
  @pub_socket = pub_socket
  @user_ns = BindingWithMime.new.send(:binding)
  @history = []
  @execution_count = 0
  #@compiler = CommandCompiler.new()
  @completer = KernelCompleter.new(@user_ns)

  # Build dict of handlers for message types
  @handlers = {}
  ['execute_request', 'complete_request', 'kernel_info_request'].each do |msg_type|
    @handlers[msg_type] = msg_type
  end
end

Instance Attribute Details

#user_nsObject

Returns the value of attribute user_ns.



44
45
46
# File 'lib/iruby/kernel.rb', line 44

def user_ns
  @user_ns
end

Class Method Details

.html(s) ⇒ Object



46
47
48
49
# File 'lib/iruby/kernel.rb', line 46

def self.html(s)
  @output_mime = "text/html"
  s
end

Instance Method Details

#abort_queueObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/iruby/kernel.rb', line 76

def abort_queue
  while true
    #begin
      ident = @reply_socket.recv(ZMQ::NOBLOCK)
    #rescue Exception => e
      #if e.errno == ZMQ::EAGAIN
        #break
      #else
        #assert self.reply_socket.rcvmore(), "Unexpected missing message part."
        #msg = self.reply_socket.recv_json()
      #end
    #end
    msg_type = msg['header']['msg_type']
    reply_type = msg_type.split('_')[0] + '_reply'
    @session.send(@reply_socket, reply_type, {status: 'aborted'}, msg)
    # reply_msg = @session.msg(reply_type, {status: 'aborted'}, msg)
    # @reply_socket.send(ident,ZMQ::SNDMORE)
    # @reply_socket.send(reply_msg.to_json)
    # We need to wait a bit for requests to come in. This can probably
    # be set shorter for true asynchronous clients.
    sleep(0.1)
  end
end

#complete_request(ident, parent) ⇒ Object



183
184
185
186
187
188
189
190
191
192
# File 'lib/iruby/kernel.rb', line 183

def complete_request(ident, parent)
  matches = {
    matches: @completer.complete(parent['content']['line'], parent['content']['text']),
    status: 'ok',
    matched_text: parent['content']['line'],
  }
  completion_msg = @session.send(@reply_socket, 'complete_reply',
                                 matches, parent, ident)
  return nil
end

#execute_request(ident, parent) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/iruby/kernel.rb', line 122

def execute_request(ident, parent)
  begin
    code = parent['content']['code']
  rescue
    STDERR.puts "Got bad msg: "
    STDERR.puts parent
    return
  end
  # pyin_msg = @session.msg()
  if ! parent['content'].fetch('silent', false)
    @execution_count += 1
  end
  self.send_status("busy", parent)
  @session.send(@pub_socket, 'pyin', {code: code}, parent)
  reply_content = {status: 'ok',
      payload: [],
      user_variables: {},
      user_expressions: {},
    }
  result = nil
  begin
    $displayhook.set_parent(parent)
    $stdout.set_parent(parent)

    eval("@mime_type=nil",@user_ns)
    result = eval(code, @user_ns)
  rescue Exception => e
    # $stderr.puts e.inspect
    #etype, evalue, tb = sys.exc_info()
    ename, evalue, tb = e.class.to_s, e.message, e.backtrace
    tb = format_exception(ename, evalue, tb)
    #tb = "1, 2, 3"
    exc_content = {
      ename: ename,
      evalue: evalue,
      traceback: tb,
      #etype: etype,
      #status: 'error',
    }
    # STDERR.puts exc_content
    @session.send(@pub_socket, 'pyerr', exc_content, parent)

    reply_content = exc_content
  end
  reply_content['execution_count'] = @execution_count
  
  # reply_msg = @session.msg('execute_reply', reply_content, parent)
  #$stdout.puts reply_msg
  #$stderr.puts reply_msg
  #@session.send(@reply_socket, ident + reply_msg)
  reply_msg = @session.send(@reply_socket, 'execute_reply', reply_content, parent, ident)
  if reply_msg['content']['status'] == 'error'
    abort_queue
  end
  if ! result.nil? and ! parent['content']['silent']
    output = ResponseWithMime.new(result, eval("@mime_type",@user_ns))
    $displayhook.display(output)
  end
  self.send_status("idle", parent)
end

#execution_countObject



51
52
53
# File 'lib/iruby/kernel.rb', line 51

def execution_count
  @execution_count
end

#kernel_info_request(ident, parent) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/iruby/kernel.rb', line 100

def kernel_info_request(ident, parent)
  reply_content = {
    protocol_version: [4, 0],

    # Language version number (mandatory).
    # It is Python version number (e.g., [2, 7, 3]) for the kernel
    # included in IPython.
    language_version: RUBY_VERSION.split('.').map { |x| x.to_i },

    # Programming language in which kernel is implemented (mandatory).
    # Kernel included in IPython returns 'python'.
    language: "ruby"
  }
  reply_msg = @session.send(@reply_socket, 'kernel_info_reply',
        reply_content, parent, ident
  )
end

#output_mimeObject



55
56
57
# File 'lib/iruby/kernel.rb', line 55

def output_mime
  @output_mime
end

#send_status(status, parent) ⇒ Object



118
119
120
# File 'lib/iruby/kernel.rb', line 118

def send_status(status, parent)
  @session.send(@pub_socket, "status", {execution_state: status}, parent)
end

#startObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/iruby/kernel.rb', line 194

def start()
  self.send_status("starting", nil)
  while true
    ident, msg = @session.recv(@reply_socket, 0)
    begin
      handler = @handlers[msg['header']['msg_type']]
    rescue
      handler = nil
    end
    if handler.nil?
      STDERR.puts "UNKNOWN MESSAGE TYPE: #{msg['header']['msg_type']} #{msg}"
    else
      # STDERR.puts 'handling ' + omsg.inspect
      send(handler, ident, msg)
    end
  end
end