def test_readln_empty(self): read_pipe, lua_write_pipe = [None] * 2 try: read_pipe = Pipe("env1", "read", 'r', "mame/pipes") lua_write_pipe = setup_pipe(read_pipe) with self.assertRaises(IOError) as context: read_pipe.readln(timeout=0.1) assert_that( str(context.exception), contains_string("Failed to read value from 'readPipe'")) finally: close_pipes(read_pipe, lua_write_pipe)
class DataPipe(object): def __init__(self, env_id, screen_dims, addresses, pipes_path): self.pipe = Pipe(env_id, "data", 'r', pipes_path) self.screenDims = screen_dims self.addresses = addresses def open(self, console): self.pipe.open(console) def close(self): self.pipe.close() # Generates the equivalent Lua code specifying what data the Lua engine should return every time step def get_lua_string(self): return self.pipe.get_lua_string(args=[address.get_lua_string() for address in self.addresses.values()]+['s:bitmap_binary()']) def read_data(self, timeout=10): data = {} line = self.pipe.readln(timeout=timeout) cursor = 0 for k in self.addresses.keys(): cursor_end = cursor while line[cursor_end] != 43: cursor_end += 1 part = line[cursor:cursor_end] data[k] = int(part.decode("utf-8")) cursor = cursor_end+1 data["frame"] = np.frombuffer(line[cursor:], dtype='uint8').reshape(self.screenDims["height"], self.screenDims["width"], 3) return data
def test_read_from_write_pipe(self): write_pipe, lua_read_pipe = [None] * 2 try: write_pipe = Pipe("env1", "write", 'w', "mame/pipes") lua_read_pipe = setup_pipe(write_pipe) with self.assertRaises(IOError) as context: write_pipe.readln() assert_that( str(context.exception), contains_string( "Attempted to read from '/home/michael/dev/MAMEToolkit/test/emulator/mame/pipes/write-env1.pipe' in 'w' mode" )) finally: close_pipes(write_pipe, lua_read_pipe)
def run_read(output_queue): read_pipe, lua_write_pipe = [None] * 2 try: read_pipe = Pipe("env1", "read", 'r', "mame/pipes") lua_write_pipe = setup_pipe(read_pipe) lua_write_pipe.write("test\n") lua_write_pipe.flush() output_queue.put(read_pipe.readln(timeout=1)) finally: close_pipes(read_pipe, lua_write_pipe)
def test_read(self): read_pipe, lua_write_pipe = [None] * 2 try: read_pipe = Pipe("env1", "read", 'r', "mame/pipes") lua_write_pipe = setup_pipe(read_pipe) lua_write_pipe.write("test\n") lua_write_pipe.flush() assert_that(read_pipe.readln(timeout=0.1), equal_to(b"test")) finally: close_pipes(read_pipe, lua_write_pipe)