def queue_poll(self, sleep_t=0.5): """Put new messages on the queue as they arrive. Blocking in a thread. Value of sleep is low to improve responsiveness. """ connection_alive = True while self.running: if self.ws: def logger_and_close(m): self.log("Websocket exception: {}".format(m)) if not self.running: # Tear down has been invoked # Prepare to exit the program connection_alive = False else: if not self.number_try_connection: # Stop everything and disable plugin self.teardown() self.disable_plugin() # WebSocket exception may happen with catch(Exception, logger_and_close): result = self.ws.recv() self.queue.put(result) if connection_alive: time.sleep(sleep_t)
def reorder_classpath(self, classpath_file): """Reorder classpath and put monkeys-jar in the first place.""" success = False with catch((IOError, OSError)): with open(classpath_file, "r") as f: classpath = f.readline() # Proceed if classpath is non-empty if classpath: units = classpath.split(":") reordered_units = [] for unit in units: if "monkeys" in unit: reordered_units.insert(0, unit) else: reordered_units.append(unit) reordered_classpath = ":".join(reordered_units) with open(classpath_file, "w") as f: f.write(reordered_classpath) success = True return success
def handle_symbol_info(self, call_id, payload): """Handler for response `SymbolInfo`.""" warn = lambda e: self.message("unknown_symbol") with catch(KeyError, warn): decl_pos = payload["declPos"] f = decl_pos.get("file") self.log(str(self.call_options[call_id])) display = self.call_options[call_id].get("display") if display and f: self.vim.command(commands["display_message"].format(f)) open_definition = self.call_options[call_id].get("open_definition") if open_definition and f: self.clean_errors() self.vim_command("doautocmd_bufleave") split = self.call_options[call_id].get("split") vert = self.call_options[call_id].get("vert") key = "" if split: key = "vert_split_window" if vert else "split_window" else: key = "edit_file" self.vim.command(commands[key].format(f)) self.vim_command("doautocmd_bufreadenter") self.set_position(decl_pos) del self.call_options[call_id]
def threadsafe_vim(self, command): """Threadsafe call if neovim, normal if vim.""" def normal_vim(e): self.vim.command(command) with catch(Exception, normal_vim): self.vim.session.threadsafe_call(command)
def send(self, msg): """Send something to the ensime server.""" def reconnect(e): self.log("send error: {}, reconnecting...".format(e)) self.connect_ensime_server() self.ws.send(msg + "\n") self.log("send: in") if self.ws: with catch(Exception, reconnect): self.log("send: {}".format(msg)) self.ws.send(msg + "\n")
def send(self, msg): """Send something to the ensime server.""" def reconnect(e): self.log("send error: {}, reconnecting...".format(e)) self.connect_ensime_server() if self.ws: self.ws.send(msg + "\n") self.log("send: in") if self.running and self.ws: with catch(Exception, reconnect): self.log("send: {}".format(msg)) self.ws.send(msg + "\n")
def queue_poll(self, sleep_t=0.5): """Put new messages on the queue as they arrive. Blocking in a thread. Value of sleep is low to improve responsiveness. """ while self.running: if self.ws: def logger_and_close(m): self.log("Websocket exception: {}".format(m)) self.teardown() # WebSocket exception may happen with catch(Exception, logger_and_close): result = self.ws.recv() self.queue.put(result) time.sleep(sleep_t)
def handle_symbol_info(self, call_id, payload): """Handler for response `SymbolInfo`.""" warn = lambda e: self.message("unknown_symbol") with catch(KeyError, warn): decl_pos = payload["declPos"] open_definition = self.call_options[call_id].get("open_definition") if open_definition: self.clean_errors() self.vim_command("doautocmd_bufleave") split = self.call_options[call_id].get("split") key = "split_window" if split else "edit_file" self.vim.command(commands[key].format(decl_pos["file"])) self.vim_command("doautocmd_bufreadenter") self.set_position(decl_pos) del self.call_options[call_id]
def connect_ensime_server(self): """Start initial connection with the server.""" self.log("connect_ensime_server: in") def disable_completely(e): if e: self.log("connection error: {}".format(e)) self.shutdown_server() self.disable_plugin() if self.running and self.number_try_connection: self.number_try_connection -= 1 if not self.ensime_server: port = self.ensime.http_port() self.ensime_server = gconfig["ensime_server"].format(port) with catch(Exception, disable_completely): from websocket import create_connection self.ws = create_connection(self.ensime_server) if self.ws: self.send_request({"typehint": "ConnectionInfoReq"}) else: # If it hits this, number_try_connection is 0 disable_completely(None)
def connect_ensime_server(self): """Start initial connection with the server.""" self.log("connect_ensime_server: in") def disable_completely(e): if e: self.log("connection error: {}".format(e)) self.shutdown_server() self.disable_plugin() if self.running and self.number_try_connection: self.number_try_connection -= 1 if not self.ensime_server: port = self.ensime.http_port() self.ensime_server = gconfig["ensime_server"].format(port) with catch(Exception, disable_completely): from websocket import create_connection # Use the default timeout (no timeout). self.ws = create_connection(self.ensime_server) if self.ws: self.send_request({"typehint": "ConnectionInfoReq"}) else: # If it hits this, number_try_connection is 0 disable_completely(None)
def on_stop(): log.close() null.close() with catch(Exception, lambda e: None): os.remove(pid_path)
def on_stop(): log.close() null.close() with catch(Exception): os.remove(pid_path)
def on_stop(): log.close() with catch(Exception, lambda e: None): os.remove(pid_path)