def test_multi_param_params(self): s = stream() s.state = "escape-lb" input = "5;25" s.process(input) self.assertEqual(s.params, [5]) self.assertEqual(s.current_param, "25")
def __init__(self, **kwargs): self._stream = vt102.stream() self._screen = vt102.screen((ROWS, COLS)) self._screen.attach(self._stream) self._need_inventory = True self._has_more = False self._command = None self.messages = collections.deque(maxlen=1000) self.stats = {} self.inventory = {} self.spells = {} opts = dict(character=random.choice('bar pri ran val wiz'.split()), gender=random.choice('mal fem'.split()), race=random.choice('elf hum'.split()), align=random.choice('cha neu'.split())) opts.update(kwargs) handle = tempfile.NamedTemporaryFile() handle.write(self.OPTIONS.format(**opts).encode('utf-8')) handle.flush() os.environ['NETHACKOPTIONS'] = '@' + handle.name
def test_bell_emits_event(self): s = stream() c = self.counter() s.add_event_listener("bell", c) s.consume(chr(ctrl.BEL)) self.assertEqual(c.count, 1)
def __init__(self, **kwargs): ###### self.action_count = 0 self.only_maps = True self.RADIUS = 4 self.queued_action = None self.process = Popen(['python', '-u', 'agent423.py', str(self.RADIUS)], stdout=PIPE, stdin=PIPE, bufsize=1) self.hp = 15 self.level = 1 ###### self._stream = vt102.stream() self._screen = vt102.screen((ROWS, COLS)) self._screen.attach(self._stream) self._need_inventory = True self._has_more = False self._command = None self.messages = collections.deque(maxlen=1000) self.stats = {} self.inventory = {} self.spells = {} opts = dict(character=random.choice('bar pri ran val wiz'.split()), gender=random.choice('mal fem'.split()), race=random.choice('elf hum'.split()), align=random.choice('cha neu'.split())) opts.update(kwargs)
def test_carriage_return(self): s = stream() c = self.counter() s.add_event_listener("carriage-return", c) s.consume(chr(ctrl.CR)) self.assertEqual(c.count, 1 ) self.assertEqual(s.state, "stream")
def test_linefeed(self): s = stream() c = self.counter() s.add_event_listener("linefeed", c) s.process(chr(ctrl.LF) + chr(ctrl.VT) + chr(ctrl.FF)) self.assertEqual(c.count, 3) self.assertEqual(s.state, "stream")
def test_tab(self): s = stream() c = self.counter() s.add_event_listener("tab", c) s.consume(chr(ctrl.HT)) self.assertEqual(c.count, 1) self.assertEqual(s.state, "stream")
def test_backspace(self): s = stream() c = self.counter() s.add_event_listener("backspace", c) s.consume(chr(ctrl.BS)) self.assertEqual(c.count, 1) self.assertEqual(s.state, "stream")
def test_carriage_return(self): s = stream() c = self.counter() s.add_event_listener("carriage-return", c) s.consume(chr(ctrl.CR)) self.assertEqual(c.count, 1) self.assertEqual(s.state, "stream")
def test_basic_escapes(self): s = stream() for cmd, event in stream.escape.items(): c = self.counter() s.add_event_listener(event, c) s.consume(chr(ctrl.ESC)) self.assertEqual(s.state, "escape") s.consume(chr(cmd)) self.assertEqual(c.count, 1) self.assertEqual(s.state, "stream")
def __init__(self, toggle_help="\t", toggle_map="`"): self.options = parse_options() if self.options.save: self.save_file = os.path.join(self.noobhack_dir, "save-%s" % self.options.save) else: self.save_file = os.path.join(self.noobhack_dir, "save") self.toggle_help = toggle_help self.toggle_map = toggle_map self.mode = "game" self.playing = False self.reloading = False self.last_input = time() self.pending_input = [] if not os.path.exists(self.noobhack_dir): os.makedirs(self.noobhack_dir, 0755) self.nethack = self.connect_to_game() self.output_proxy = proxy.Output(self.nethack) self.input_proxy = proxy.Input(self.nethack) # Create an in-memory terminal screen and register it's stream # processor with the output proxy. self.stream = vt102.stream() # For some reason that I can't assertain: curses freaks out and crashes # when you use exactly the number of rows that are available on the # terminal. It seems easiest just to subtract one from the rows and # deal with it rather than hunt forever trying to figure out what I'm # doing wrong with curses. rows, cols = size() self.term = vt102.screen((rows, cols), self.options.encoding) self.term.attach(self.stream) self.output_proxy.register(self.stream.process) self.game = Game(self.term) self.output_proxy.register(self._restore_game_checker) self.output_proxy.register(self._game_started_checker) self.output_proxy.register(self._quit_or_died_checker) # Register the `toggle` key to open up the interactive nooback # assistant. self.input_proxy.register(self._toggle)
def test_cursor_up(self): class argcheck: def __init__(self): self.count = 0 def __call__(self, distance): self.count += 1 assert distance == 5 s = stream() input = "\000" + chr(ctrl.ESC) + "[5" + chr(esc.CUU) e = argcheck() s.add_event_listener("cursor-up", e) s.process(input) self.assertEqual(e.count, 1) self.assertEqual(s.state, "stream")
def _run(self, win): curses.nonl() curses.curs_set(1) self._win = win self._win.nodelay(1) self._win.scrollok(0) self._win.idlok(1) self._vt_stream = vt102.stream() self._vt_screen = vt102.screen(self._win.getmaxyx()) self._vt_screen.attach(self._vt_stream) self._buffered_output = u'' self._vt_stream.process(ConsoleServer.banner) server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_socket.bind(('127.0.0.1', 6809)) server_socket.listen() server_socket.setblocking(False) self._selector.register(server_socket, selectors.EVENT_READ, self._accept) while True: events = self._selector.select(timeout=0.1) if self._want_exit: return for key, mask in events: callback = key.data callback(key.fileobj, mask) self._update() if len(self._buffered_input) > 0: if self._connection is not None: self._connection.send( self._buffered_input.encode('latin-1')) self._buffered_input = u''
def termkey(child, stream, screen, key, timeout=0): logging.debug("Sending '%s' to child"%key) child.send(key) s = termcheck(child) logging.debug("Sending child.before text to vt102 stream") stream.process(child.before) logging.debug("vt102 screen dump") logging.debug(screen) # START LOGGING logging.basicConfig(filename='menu_demo.log',level=logging.DEBUG) # SETUP VT102 EMULATOR #rows, columns = os.popen('stty size', 'r').read().split() rows, columns = (50,120) stream=vt102.stream() screen=vt102.screen((int(rows), int(columns))) screen.attach(stream) logging.debug("Setup vt102 with %d %d"%(int(rows),int(columns))) logging.debug("Starting demo2.py child process...") child = pexpect.spawn('./demo2.py', maxread=65536, dimensions=(int(rows),int(columns))) s = termcheck(child) logging.debug("Sending child.before (len=%d) text to vt102 stream"%len(child.before)) stream.process(child.before) logging.debug("vt102 screen dump") logging.debug(screen) termkey(child, stream, screen, "a")