def _get_scala_interpreter(self): """Ensure that we have a scala interpreter around and set up the stdout/err handlers if needed. Returns ------- scala_intp : scala_interpreter.SparkInterpreter """ if self._interp is None: assert isinstance(self.kernel, MetaKernel) self.kernel.Display(TextOutput("Intitializing scala interpreter....")) self._interp = get_scala_interpreter() # Ensure that spark is available in the python session as well. self.kernel.cell_magics['python'].env['spark'] = self._interp.spark_session self.kernel.cell_magics['python'].env['sc'] = self._interp.sc sc = self._interp.sc self.kernel.Display(TextOutput(dedent("""\ Web ui available at {webui} Spark context available as 'sc' (master = {master}, app id = {app_id}) Spark session available as 'spark' """.format( master=sc.master, app_id=sc.applicationId, webui=self._interp.web_ui_url ) ))) self._is_complete_ready = True self._interp.register_stdout_handler(self.kernel.Write) self._interp.register_stderr_handler(self.kernel.Error) # Set up the callbacks self._initialize_pipes() return self._interp
def do_execute_direct(self, code): """ Execute gnuplot code """ if self._first: self._first = False self.handle_plot_settings() if self.inline_plotting: code = self.add_inline_image_statements(code) success = True try: result = super(GnuplotKernel, self).do_execute_direct(code, silent=True) except GnuplotError as e: result = TextOutput(e.message) success = False if self.reset_code: super(GnuplotKernel, self).do_execute_direct( self.reset_code, silent=True) if self.inline_plotting: if success: self.display_images() self.delete_image_files() self.bad_prompt_warning() # No empty strings return result if (result and result.output) else None
def eval(self, code, raw): """Evaluates Scala code. Parameters ---------- code: str Code to execute raw: bool True to return the raw result of the evalution, False to wrap it with MetaKernel classes Returns ------- metakernel.process_metakernel.TextOutput or metakernel.ExceptionWrapper or the raw result of the evaluation """ intp = self._get_scala_interpreter() try: res = intp.interpret(code.strip()) if raw: self.res = intp.last_result() return self.res else: if res: return TextOutput(res) except ScalaException as ex: # Get the kernel response so far resp = self.kernel.kernel_resp # Wrap the exception for MetaKernel use resp['status'] = 'error' tb = ex.scala_message.split('\n') first = tb[0] assert isinstance(first, str) eclass, _, emessage = first.partition(':') return ExceptionWrapper(eclass, emessage, tb)
def do_execute_direct(self, code, silent=False): loop = asyncio.get_event_loop() try: fut = asyncio.Future() asyncio.ensure_future(self.execute_scala_async(code, fut)) res = loop.run_until_complete(fut) if res: return TextOutput(res) except ScalaException as e: return self.Error(e.scala_message)
def _get_scala_interpreter(self): """Ensure that we have a scala interpreter around and set up the stdout/err handlers if needed. Returns ------- scala_intp : scala_interpreter.ScalaInterpreter """ if self._interp is None: assert isinstance(self.kernel, MetaKernel) self.kernel.Display(TextOutput("Intitializing Scala interpreter ...")) self._interp = get_scala_interpreter() # Ensure that spark is available in the python session as well. if 'python' in self.kernel.cell_magics: # tests if in scala mode self.kernel.cell_magics['python'].env['spark'] = self._interp.spark_session self.kernel.cell_magics['python'].env['sc'] = self._interp.sc # Display some information about the Spark session sc = self._interp.sc self.kernel.Display(TextOutput(dedent("""\ Spark Web UI available at {webui} SparkContext available as 'sc' (version = {version}, master = {master}, app id = {app_id}) SparkSession available as 'spark' """.format( version=sc.version, master=sc.master, app_id=sc.applicationId, webui=self._interp.web_ui_url ) ))) # Let down the guard: the interpreter is ready for use self._is_complete_ready = True # Send stdout to the MetaKernel.Write method # and stderr to MetaKernel.Error self._interp.register_stdout_handler(self.kernel.Write) self._interp.register_stderr_handler(self.kernel.Error) return self._interp
def eval(self, code, raw): intp = self._get_scala_interpreter() try: res = intp.interpret(code.strip()) if raw: self.res = intp.last_result() return self.res else: if res: return TextOutput(res) except ScalaException as e: return self.kernel.Error(e.scala_message)
def eval(self, code, raw): intp = self._get_scala_interpreter() try: res = intp.interpret(code.strip()) if raw: self.res = intp.last_result() return self.res else: if res: return TextOutput(res) except ScalaException as e: resp = self.kernel.kernel_resp resp['status'] = 'error' tb = e.scala_message.split('\n') first = tb[0] assert isinstance(first, str) eclass, _, emessage = first.partition(':') from metakernel import ExceptionWrapper return ExceptionWrapper(eclass, emessage, tb[1:])
def do_execute_direct_single_command_expred(self, code, stream_handler=None): """Execute the code in the subprocess. Use the stream_handler to process lines as they are emitted by the subprocess. """ self.payload = [] if not code.strip(): self.kernel_resp = { 'status': 'ok', 'execution_count': self.execution_count, 'payload': [], 'user_expressions': {}, } return interrupted = False output = '' try: output = self.wrapper.run_command(code.rstrip(), timeout=-1, stream_handler=stream_handler) # TODO: instead of proccess_response, it would be better # to capture the prints and warnings at the moment they are sended. output = self.process_response(output) if stream_handler is not None: output = '' except MMASyntaxError as e: self.kernel_resp = { 'status': 'error', 'execution_count': self.execution_count, 'ename': e.name, 'evalue': e.val, 'traceback': e.traceback, } return TextOutput("null:") except KeyboardInterrupt as e: interrupted = True output = self.wrapper.child.before if 'REPL not responding to interrupt' in str(e): output += '\n%s' % e except EOF: output = self.wrapper.child.before + 'Restarting' self._start() if interrupted: self.kernel_resp = { 'status': 'abort', 'execution_count': self.execution_count, } exitcode, trace = self.check_exitcode() if exitcode: self.kernel_resp = { 'status': 'error', 'execution_count': self.execution_count, 'ename': '', 'evalue': str(exitcode), 'traceback': trace, } else: self.kernel_resp = { 'status': 'ok', 'execution_count': self.execution_count, 'payload': [], 'user_expressions': {}, } return TextOutput(output)
def do_execute_direct_single_command(self, code, stream_handler=None, envelop=True): """Execute the code in the subprocess. Use the stream_handler to process lines as they are emitted by the subprocess. """ self.payload = [] if not code.strip(): self.kernel_resp = { 'status': 'ok', 'execution_count': self.execution_count, 'payload': [], 'user_expressions': {}, } return interrupted = False output = '' if envelop: code = code.replace("\\", "\\\\").replace("\"", "\\\"") code = code.replace("\n", "\\n") if code.strip()[-1] == ";": code = self.open_envel + code + self.close_envel + ";" else: code = self.open_envel + code + self.close_envel # Ugly, but I didn't find another effective way # to properly clean the buffer try: remaining = "" while True: remaining += self.myspawner.read_nonblocking(100, .1) except TIMEOUT as e: pass try: self.bufferout = "" output = self.wrapper.run_command(code, timeout=None, stream_handler=stream_handler) if (stream_handler is not None): output = self.bufferout + output self.bufferout = "" output = self.process_response(output) self.bufferout = "" except MMASyntaxError as e: if e.name == "sntxi": raise e else: self.kernel_resp = { 'status': 'error', 'execution_count': self.execution_count, 'ename': e.name, 'evalue': e.val, 'traceback': e.traceback, } return TextOutput("null:") except KeyboardInterrupt as e: interrupted = True output = self.wrapper.child.before if 'REPL not responding to interrupt' in str(e): output += '\n%s' % e except EOF: output = self.wrapper.child.before + 'Restarting' self._start() if interrupted: self.kernel_resp = { 'status': 'abort', 'execution_count': self.execution_count, } exitcode, trace = self.check_exitcode() if exitcode: self.kernel_resp = { 'status': 'error', 'execution_count': self.execution_count, 'ename': '', 'evalue': str(exitcode), 'traceback': trace, } else: self.kernel_resp = { 'status': 'ok', 'execution_count': self.execution_count, 'payload': [], 'user_expressions': {}, } return TextOutput(output)
def do_execute_direct(self, code): self.payload = [] resp = None codelines = [codeline.strip() for codeline in code.splitlines()] # Remove every empty line at the end of the cell while len(codelines) > 0 and codelines[-1].strip() == "": del codelines[-1] lastcommand = "" i = 0 lencodelines = len(codelines) # Go through each line in the input while i < lencodelines: if codelines[i].strip() == "" and lastcommand == "": i += 1 continue lastcommand += codelines[i] i = i + 1 # continue until the end of the current command while i <= lencodelines: # If the line is empty, check if we have now a complete command if i == lencodelines or codelines[i].strip() == "": # Check first if the command is something more # than a comment, and if it is complete. try: lastcommand = self.clean_comments(lastcommand).strip() if lastcommand == "": i = i + 1 resp = None continue except MMASyntaxError as e: if i < lencodelines: lastcommand += "\n" + codelines[i] i += 1 continue else: self.Error(e) self.kernel_resp = { 'status': 'error', 'execution_count': self.execution_count, 'ename': e.name, 'evalue': e.val, 'traceback': e.traceback, } return TextOutput("null:") try: resp = self.do_execute_direct_single_command( lastcommand, stream_handler=self.stream_handler) resp = self.postprocess_response(resp.output) i = i + 1 if i < lencodelines: self.post_execute(resp, lastcommand, False) lastcommand = "" break except MMASyntaxError as e: # if the error is due to unbalanced # parentheses or open string, if e.name == "sntxi": self.wrapper.run_command("$Line=$Line-2;", timeout=None, stream_handler=None) if i < lencodelines: lastcommand += "\n" + codelines[i] i += 1 continue else: self.Error(e) self.kernel_resp = { 'status': 'error', 'execution_count': self.execution_count, 'ename': e.name, 'evalue': e.val, 'traceback': e.traceback, } return TextOutput("null:") else: self.post_execute(resp, lastcommand, False) lastcommand = "" i += 1 break else: if i < lencodelines: lastcommand += "\n" + codelines[i] i += 1 return resp
def do_execute_direct_single_command_expred(self, code, stream_handler=None): """Execute the code in the subprocess. Use the stream_handler to process lines as they are emitted by the subprocess. """ self.payload = [] if not code.strip(): self.kernel_resp = { "status": "ok", "execution_count": self.execution_count, "payload": [], "user_expressions": {}, } return interrupted = False output = "" try: output = self.wrapper.run_command( code.rstrip(), timeout=-1, stream_handler=stream_handler ) # TODO: instead of proccess_response, it would be better # to capture the prints and warnings at the moment they are sended. output = self.process_response(output) if stream_handler is not None: output = "" except MMASyntaxError as e: self.kernel_resp = { "status": "error", "execution_count": self.execution_count, "ename": e.name, "evalue": e.val, "traceback": e.traceback, } return TextOutput("null:") except KeyboardInterrupt as e: interrupted = True output = self.wrapper.child.before if "REPL not responding to interrupt" in str(e): output += "\n%s" % e except EOF: output = self.wrapper.child.before + "Restarting" self._start() if interrupted: self.kernel_resp = { "status": "abort", "execution_count": self.execution_count, } exitcode, trace = self.check_exitcode() if exitcode: self.kernel_resp = { "status": "error", "execution_count": self.execution_count, "ename": "", "evalue": str(exitcode), "traceback": trace, } else: self.kernel_resp = { "status": "ok", "execution_count": self.execution_count, "payload": [], "user_expressions": {}, } return TextOutput(output)
def do_execute_direct_single_command(self, code, stream_handler=None, envelop=True): """Execute the code in the subprocess. Use the stream_handler to process lines as they are emitted by the subprocess. """ self.payload = [] if not code.strip(): self.kernel_resp = { "status": "ok", "execution_count": self.execution_count, "payload": [], "user_expressions": {}, } return interrupted = False output = "" if envelop: code = code.replace("\\", "\\\\").replace('"', '\\"') code = code.replace("\n", "\\n") if code.strip()[-1] == ";": code = self.open_envel + code + self.close_envel + ";" else: code = self.open_envel + code + self.close_envel # Ugly, but I didn't find another effective way # to properly clean the buffer try: remaining = "" while True: remaining += self.myspawner.read_nonblocking(100, 0.1) except TIMEOUT as e: pass try: self.bufferout = "" output = self.wrapper.run_command( code, timeout=None, stream_handler=stream_handler ) if stream_handler is not None: output = self.bufferout + output self.bufferout = "" output = self.process_response(output) self.bufferout = "" except MMASyntaxError as e: if e.name == "sntxi": raise e else: self.kernel_resp = { "status": "error", "execution_count": self.execution_count, "ename": e.name, "evalue": e.val, "traceback": e.traceback, } return TextOutput("null:") except KeyboardInterrupt as e: interrupted = True self.wrapper.child.before = "a" output = self.wrapper.interrupt() try: rest = self.myspawner.read_nonblocking(500, 1) except TIMEOUT as f: rest = "" pass self.myspawner.sendline("a") try: rest = self.myspawner.read_nonblocking(500, 1) except TIMEOUT as f: rest = "" pass if "REPL not responding to interrupt" in str(e): output += "\n%s" % e except EOF: output = self.wrapper.child.before + "Restarting" # self._start() if interrupted: self.kernel_resp = { "status": "abort", "execution_count": self.execution_count, } exitcode, trace = self.check_exitcode() if exitcode: self.kernel_resp = { "status": "error", "execution_count": self.execution_count, "ename": "", "evalue": str(exitcode), "traceback": trace, } else: self.kernel_resp = { "status": "ok", "execution_count": self.execution_count, "payload": [], "user_expressions": {}, } return TextOutput(output)