def get_input_with_readline(filename): """ sets up readline support for entering sitenames, completed from the existing list and accepts a line of input. """ if not os.path.exists(filename): file(filename, "w").close() all_lines = map(lambda x: x.strip(), file(filename).readlines()) def completer(text, state): """ custom readline completer """ candidates = filter(lambda x: x.startswith(text), all_lines) if state <= len(candidates): return candidates[state-1] else: return None try: import readline readline.set_completer(completer) readline.read_history_file(filename) readline.parse_and_bind('tab: complete') if hasattr(readline, 'readline'): print "sitename: ", readline._issued = True return readline.readline(history=all_lines, histfile=None) else: return raw_input("sitename: ") except: # no readline? return raw_input("sitename: ")
def iterate_readline(self, iterator, recv_calls, recv_len=1024): socket = MagicMock() socket.recv.side_effect = iterator + ("", ) it = readline(socket, recv_len) self.assertEquals(next(it), "foo") self.assertEquals(next(it), "bar") self.assertEquals(next(it), "baz") with self.assertRaises(StopIteration): next(it) self.assertEquals(len(socket.recv.mock_calls), recv_calls)
def read_command(self): self.reading_command = True self.draw_command_bar(status=WAITING_FOR_CMD) if self.logger != None: self.logger.add_tag("read_command") self.draw_command_bar(status=WAITING_FOR_CMD) self.input_bar_cursor = csr.right_of(self.input_bar_cursor, 5) inp, exit_dict = readline(self.input_bar_cursor, max_input=self.term.width - 5) if exit_dict["enter"]: self.command = [substr for substr in inp.split(" ") if substr not in ["", None]] self.set_status(EDITING) self.input_bar_text = u"" self.input_bar_cursor = csr.home(self.input_bar_cursor) echo_at_cell(self.input_bar_cursor, self.term.clear_eol) self.reading_command = False if self.logger != None: self.logger.remove_tag()
def test_pass_something_that_doesnt_have_a_recv_method(self): with self.assertRaises(Exception): next(readline(None))