Class: IRuby::Kernel
- Inherits:
-
Object
- Object
- IRuby::Kernel
- Defined in:
- lib/iruby/kernel.rb
Constant Summary collapse
- RED =
"\e[31m"
- WHITE =
"\e[37m"
- RESET =
"\e[0m"
Class Attribute Summary collapse
-
.instance ⇒ Object
Returns the value of attribute instance.
Instance Method Summary collapse
- #complete_request(ident, msg) ⇒ Object
- #connect_request(ident, msg) ⇒ Object
- #create_backend ⇒ Object
- #display(obj, options = {}) ⇒ Object
- #execute_request(ident, msg) ⇒ Object
- #history_request(ident, msg) ⇒ Object
-
#initialize(config_file) ⇒ Kernel
constructor
A new instance of Kernel.
- #kernel_info_request(ident, msg) ⇒ Object
- #object_info_request(ident, msg) ⇒ Object
- #run ⇒ Object
- #send_status(status) ⇒ Object
- #shutdown_request(ident, msg) ⇒ Object
Constructor Details
#initialize(config_file) ⇒ Kernel
Returns a new instance of Kernel.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/iruby/kernel.rb', line 11 def initialize(config_file) @config = MultiJson.load(File.read(config_file)) #puts 'Starting the kernel' #puts config #puts 'Use Ctrl-\\ (NOT Ctrl-C!) to terminate.' Kernel.instance = self c = ZMQ::Context.new connection = "#{@config['transport']}://#{@config['ip']}:%d" @reply_socket = c.socket(ZMQ::XREP) @reply_socket.bind(connection % @config['shell_port']) @pub_socket = c.socket(ZMQ::PUB) @pub_socket.bind(connection % @config['iopub_port']) Thread.new do begin hb_socket = c.socket(ZMQ::REP) hb_socket.bind(connection % @config['hb_port']) ZMQ::Device.new(hb_socket, hb_socket) rescue Exception => ex STDERR.puts "Kernel heartbeat died: #{ex.}\n"#{ex.backtrace.join("\n")}" end end @session = Session.new('kernel', @config) $stdout = OStream.new(@session, @pub_socket, 'stdout') $stderr = OStream.new(@session, @pub_socket, 'stderr') @execution_count = 0 @backend = create_backend @running = true end |
Class Attribute Details
.instance ⇒ Object
Returns the value of attribute instance.
8 9 10 |
# File 'lib/iruby/kernel.rb', line 8 def instance @instance end |
Instance Method Details
#complete_request(ident, msg) ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'lib/iruby/kernel.rb', line 134 def complete_request(ident, msg) content = { matches: @backend.complete(msg[:content]['line'], msg[:content]['text']), status: 'ok', matched_text: msg[:content]['line'], } @session.send(@reply_socket, 'complete_reply', content, ident) end |
#connect_request(ident, msg) ⇒ Object
143 144 145 146 147 148 149 150 151 |
# File 'lib/iruby/kernel.rb', line 143 def connect_request(ident, msg) content = { shell_port: config['shell_port'], iopub_port: config['iopub_port'], stdin_port: config['stdin_port'], hb_port: config['hb_port'] } @session.send(@reply_socket, 'connect_reply', content, ident) end |
#create_backend ⇒ Object
49 50 51 52 53 54 |
# File 'lib/iruby/kernel.rb', line 49 def create_backend PryBackend.new rescue Exception => ex STDERR.puts ex. unless LoadError === ex PlainBackend.new end |
#display(obj, options = {}) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/iruby/kernel.rb', line 69 def display(obj, ={}) unless obj.nil? content = { data: Display.new(obj, ).data, metadata: {}, execution_count: @execution_count } @session.send(@pub_socket, 'pyout', content) end nil end |
#execute_request(ident, msg) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/iruby/kernel.rb', line 97 def execute_request(ident, msg) begin code = msg[:content]['code'] rescue STDERR.puts "Got bad message: #{msg.inspect}" return end @execution_count += 1 unless msg[:content].fetch('silent', false) send_status('busy') @session.send(@pub_socket, 'pyin', code: code) result = nil begin result = @backend.eval(code) content = { status: 'ok', payload: [], user_variables: {}, user_expressions: {}, execution_count: @execution_count } rescue Exception => e content = { ename: e.class.to_s, evalue: e., etype: e.class.to_s, status: 'error', traceback: ["#{RED}#{e.class}#{RESET}: #{e.}", *e.backtrace.map { |l| "#{WHITE}#{l}#{RESET}" }], execution_count: @execution_count } @session.send(@pub_socket, 'pyerr', content) end @session.send(@reply_socket, 'execute_reply', content, ident) display(result) unless msg[:content]['silent'] send_status('idle') end |
#history_request(ident, msg) ⇒ Object
158 159 160 161 162 163 164 165 |
# File 'lib/iruby/kernel.rb', line 158 def history_request(ident, msg) # we will just send back empty history for now, pending clarification # as requested in ipython/ipython#3806 content = { history: [] } @session.send(@reply_socket, 'history_reply', content, ident) end |
#kernel_info_request(ident, msg) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/iruby/kernel.rb', line 77 def kernel_info_request(ident, msg) 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(&:to_i), # Programming language in which kernel is implemented (mandatory). # Kernel included in IPython returns 'python'. language: 'ruby' } @session.send(@reply_socket, 'kernel_info_reply', content, ident) end |
#object_info_request(ident, msg) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/iruby/kernel.rb', line 167 def object_info_request(ident, msg) o = @backend.eval(msg[:content]['oname']) content = { oname: msg[:content]['oname'], found: true, ismagic: false, isalias: false, docstring: '', # TODO type_class: o.class.to_s, type_class: o.class.superclass.to_s, string_form: o.inspect } content[:length] = o.length if o.respond_to?(:length) @session.send(@reply_socket, 'object_info_reply', content, ident) rescue Exception content = { oname: msg[:content]['oname'], found: false } @session.send(@reply_socket, 'object_info_reply', content, ident) end |
#run ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/iruby/kernel.rb', line 56 def run send_status('starting') while @running ident, msg = @session.recv(@reply_socket, 0) type = msg[:header]['msg_type'] if type =~ /_request\Z/ && respond_to?(type) send(type, ident, msg) else STDERR.puts "Unknown message type: #{msg[:header]['msg_type']} #{msg.inspect}" end end end |
#send_status(status) ⇒ Object
93 94 95 |
# File 'lib/iruby/kernel.rb', line 93 def send_status(status) @session.send(@pub_socket, 'status', execution_state: status) end |
#shutdown_request(ident, msg) ⇒ Object
153 154 155 156 |
# File 'lib/iruby/kernel.rb', line 153 def shutdown_request(ident, msg) @session.send(@reply_socket, 'shutdown_reply', msg[:content], ident) @running = false end |