def cache(self, code, *args, **kwargs): """Version of cache that compiles Coconut code first.""" try: compiled = memoized_parse_block(code) except CoconutException: logger.display_exc() return None else: return super(CoconutCompiler, self).cache(compiled, *args, **kwargs)
def using_path(path): """Removes a path at the beginning and end.""" if os.path.exists(path): rm_path(path) try: yield finally: try: rm_path(path) except OSError: logger.display_exc()
def remove_when_done(path): """Removes a path when done.""" try: yield finally: try: if os.path.isdir(path): shutil.rmtree(path) elif os.path.isfile(path): os.remove(path) except OSError as err: logger.display_exc()
def handling_exceptions(self): """Perform proper exception handling.""" try: with handling_broken_process_pool(): yield except SystemExit as err: self.register_error(err.code) except BaseException as err: if isinstance(err, CoconutException): logger.display_exc() elif not isinstance(err, KeyboardInterrupt): traceback.print_exc() printerr(report_this_text) self.register_error(errmsg=err.__class__.__name__)
def using_dest(): """Makes and removes the dest folder.""" try: os.mkdir(dest) except Exception: shutil.rmtree(dest) os.mkdir(dest) try: yield finally: try: rm_path(dest) except OSError: logger.display_exc()
def magic(line, cell=None): """Provides %coconut and %%coconut magics.""" try: if cell is None: code = line else: # first line in block is cmd, rest is code line = line.strip() if line: cmd(line) code = cell compiled = parse(code) except CoconutException: logger.display_exc() else: ipython.run_cell(compiled, shell_futures=False)
def magic(line, cell=None): """Provides %coconut and %%coconut magics.""" try: if cell is None: code = line else: # first line in block is cmd, rest is code line = line.strip() if line: cmd(line, interact=False) code = cell compiled = parse(code) except CoconutException: logger.display_exc() else: ipython.run_cell(compiled, shell_futures=False)
def handling_exceptions(self): """Perform proper exception handling.""" try: if self.using_jobs: with handling_broken_process_pool(): yield else: yield except SystemExit as err: self.register_error(err.code) except BaseException as err: if isinstance(err, CoconutException): logger.display_exc() elif not isinstance(err, KeyboardInterrupt): traceback.print_exc() printerr(report_this_text) self.register_error(errmsg=err.__class__.__name__)
def input(self, more=False): """Prompt for code input.""" sys.stdout.flush() if more: msg = more_prompt else: msg = main_prompt if self.style is not None: internal_assert(prompt_toolkit is not None, "without prompt_toolkit cannot highlight style", self.style) try: return self.prompt(msg) except EOFError: raise # issubclass(EOFError, Exception), so we have to do this except (Exception, AssertionError): logger.display_exc() logger.show_sig("Syntax highlighting failed; switching to --style none.") self.style = None return input(msg)
def handle_input(self, code): """Compile Coconut interpreter input.""" if not self.prompt.multiline: if not should_indent(code): try: return self.comp.parse_block(code) except CoconutException: pass while True: line = self.get_input(more=True) if line is None: return None elif line: code += "\n" + line else: break try: return self.comp.parse_block(code) except CoconutException: logger.display_exc() return None