def gather_candidates(self, context): client = False try: client = self.vim.eval("fireplace#client()") except Exception: pass if client: transport = client.get("connection", {}).get("transport") ns = "" try: ns = self.vim.eval("fireplace#ns()") except Exception: pass host = transport.get("host") port = transport.get("port") # FIXME: Cache connections based on host/port conn = nrepl.connect("nrepl://{}:{}".format(host, port)) # TODO: context for context aware completions conn.write({ "op": "complete", "symbol": context["complete_str"], "extra-metadata": ["arglists", "doc"], "ns": ns }) response = conn.read() return [candidate(x) for x in response.get("completions", [])] return []
# send data def nrepl_eval(iron, data): "requires nrepl-python-client" try: import nrepl except: iron.call_cmd("echomsg 'Unable to eval - missing nREPL client lib'") return vim_pwd = iron.call("getcwd") with open(os.path.join(vim_pwd, ".nrepl-port")) as port: c = nrepl.connect("nrepl://localhost:{}".format(port.read())) c.write({"op": "eval", "code": data}) r = c.read() if 'out' in r: value = r['out'] r = c.read() value = value if r['value'] == u'nil' else r['value'] elif 'ex' in r: iron.call_cmd("echomsg 'An error occurred: {}\n{}'".format( r['ex'], c.read()['err'] )) value = None else: value = r.get('value', None) c.close()
def test_async_watches (self): c = nrepl.connect("nrepl://localhost:" + self.port) wc = nrepl.WatchableConnection(c) outs = {} def add_resp (session, msg): out = msg.get("out", None) if out: outs[session].append(out) def watch_new_sessions (msg, wc, key): session = msg.get("new-session") outs[session] = [] wc.watch("session" + session, {"session": session}, lambda msg, wc, key: add_resp(session, msg)) wc.watch("sessions", {"new-session": None}, watch_new_sessions) wc.send({"op": "clone"}) wc.send({"op": "clone"}) time.sleep(0.5) for i, session in enumerate(outs.keys()): wc.send({"op": "eval", "session": session, "code": """(do (future (Thread/sleep %s00) (println %s) (println (System/currentTimeMillis))))""" % (i, i)}) time.sleep(2) for i, (session, _outs) in enumerate(outs.items()): self.assertEqual(i, int(_outs[0])) # Python3 got dicts that we cant slice, thus we wrap it in a list. outs_values = list(outs.values()) self.assertTrue(int(outs_values[0][1]) < int(outs_values[1][1]))
def get_or_create(self, url): if url not in self.sessions: conn = nrepl.connect(url) wconn = nrepl.WatchableConnection(conn) self.sessions[url] = {'conn': wconn} return self.sessions[url]['conn']
def create_session(scheme, host, port): conn, sess_list = nrepl_connections.get((scheme, host, port), (None, None)) if not conn: conn = nrepl.connect(port, host, scheme) sess_list = set() nrepl_connections[(scheme, host, port)] = (conn, sess_list) session = nrepl.open_session(conn) sess_list.add(session) return conn, session
def get_or_create(self, url): if url not in self.sessions: conn = nrepl.connect(url) wconn = nrepl.WatchableConnection(conn) if self.default_handler is not None: watcher_key = "{}-watcher".format(uuid.uuid4().hex) wconn.watch(watcher_key, {}, self.default_handler) self.default_handler({"wc": watcher_key}, None, None) self.sessions[url] = {'conn': wconn} return self.sessions[url]['conn']
def get_conn(self, conn_string): if conn_string not in self.__conns: conn = nrepl.connect(conn_string) def global_watch(cmsg, cwc, ckey): self.logger.debug("Received message for {}".format(conn_string)) self.logger.debug(cmsg) wc = nrepl.WatchableConnection(conn) self.__conns[conn_string] = wc wc.watch("global_watch", {}, global_watch) return self.__conns.get(conn_string)
def get_conn(self, conn_string): if conn_string not in self.__conns: conn = nrepl.connect(conn_string) def global_watch(cmsg, cwc, ckey): self.logger.debug( "Received message for {}".format(conn_string)) self.logger.debug(cmsg) wc = nrepl.WatchableConnection(conn) self.__conns[conn_string] = wc wc.watch("global_watch", {}, global_watch) return self.__conns.get(conn_string)
def test_simple_connection (self): c = nrepl.connect("nrepl://localhost:" + self.port) c.write({"op": "clone"}) r = c.read() self.assertEqual(["done"], r["status"]) session = r["new-session"] self.assertIsNotNone(session) c.write({"op": "eval", "code": "(+ 1 2)", "session": session}) r = c.read() self.assertEqual(session, r["session"]) self.assertEqual("3", r["value"]) self.assertEqual(["done"], c.read()["status"]) c.write({"op": "eval", "code": "(+ *1 2)", "session": session}) self.assertEqual("5", c.read()["value"]) c.close()
def get_or_create(self, url): if url not in self.sessions: conn = nrepl.connect(url) wconn = nrepl.WatchableConnection(conn) self.sessions[url] = {'conn': wconn} def clone_handler(msg, wc, key): self.sessions[key]['session'] = msg['new-session'] wc.unwatch(key) wconn.watch(url, {'new-session': None}, clone_handler) wconn.send({'op': 'clone'}) return self.sessions[url]['conn']
def test_simple_connection(self): c = nrepl.connect("nrepl://localhost:" + self.port) c.write({"op": "clone"}) r = c.read() self.assertEqual(["done"], r["status"]) session = r["new-session"] self.assertIsNotNone(session) c.write({"op": "eval", "code": "(+ 1 2)", "session": session}) r = c.read() self.assertEqual(session, r["session"]) self.assertEqual("3", r["value"]) self.assertEqual(["done"], c.read()["status"]) c.write({"op": "eval", "code": "(+ *1 2)", "session": session}) self.assertEqual("5", c.read()["value"]) c.close()
def eval(self, cmd): """ Evaluate a command using the attached NREPL. INPUT ----- cmd : str The command to execute. OUTPUT ------ A dictionary with u'session', u'ns', and u'value'. """ host_string = 'nrepl://' + str(self.host) + ':' + str(self.port) c = nrepl.connect(host_string) c.write({"op": "eval", "code": cmd}) print "%s" % c.read()
def eval_code(args): code = get_code(args) try: connection = nrepl.connect("nrepl://" + args.hostname + ":" + str(args.port)) connection.write({"op": "eval", "code": code}) while True: response = connection.read() if not handle_response(response, args): break except socket.error as err: sys.exit("Error connecting to nREPL: " + err.message) finally: connection.close()
def test_async_watches(self): c = nrepl.connect("nrepl://localhost:" + self.port) wc = nrepl.WatchableConnection(c) outs = {} def add_resp(session, msg): out = msg.get("out", None) if out: outs[session].append(out) def watch_new_sessions(msg, wc, key): session = msg.get("new-session") outs[session] = [] wc.watch("session" + session, {"session": session}, lambda msg, wc, key: add_resp(session, msg)) wc.watch("sessions", {"new-session": None}, watch_new_sessions) wc.send({"op": "clone"}) wc.send({"op": "clone"}) time.sleep(0.5) for i, session in enumerate(outs.keys()): wc.send({ "op": "eval", "session": session, "code": """(do (future (Thread/sleep %s00) (println %s) (println (System/currentTimeMillis))))""" % (i, i) }) time.sleep(2) for i, (session, _outs) in enumerate(outs.items()): self.assertEqual(i, int(_outs[0])) # Python3 got dicts that we cant slice, thus we wrap it in a list. outs_values = list(outs.values()) self.assertTrue(int(outs_values[0][1]) < int(outs_values[1][1]))
def tearDown (self): # neither os.kill, self.proc.kill, or self.proc.terminate were shutting # down the leiningen/clojure/nrepl process(es) c = nrepl.connect("nrepl://localhost:" + self.port) c.write({"op": "eval", "code": "(System/exit 0)"}) self.proc.kill()
def __init__(self, host='localhost', port=7002): self.nrepl_client = nrepl.connect("nrepl://%s:%d" % (host, port))
def tearDown(self): # neither os.kill, self.proc.kill, or self.proc.terminate were shutting # down the leiningen/clojure/nrepl process(es) c = nrepl.connect("nrepl://localhost:" + self.port) c.write({"op": "eval", "code": "(System/exit 0)"}) self.proc.kill()