def _seek_index(self, seek_count): """Seeks the index file to immediately *after* seek_count. May be the end of the file.""" last = self._read_last_count() if last <= seek_count: return last_pos = self.index_fp.tell() / 12 # Let's guess where we should go... ## FIXME: might be better to count back absolutely, instead of ## using the range guess = last_pos * seek_count / last self.index_fp.seek(guess * 12 + 8) least = 0 greatest = last_pos while 1: count, = int_encoding.unpack(self.index_fp.read(4)) if count == seek_count: return if seek_count > count: least = guess else: greatest = guess if greatest - least < 1: # either we've bounded the target, or we've bounded an # empty spot (meaning the target doesn't exist, but # we're okay) if greatest != guess: # We're not at the right place yet os.seek(greatest * 12) return diff = seek_count - count self.index_fp.seek(diff * 12 - 4, os.SEEK_CUR)
def marshal(self, obj): # return str(obj) dict = obj os = cStringIO.StringIO() os.write("{\n") for key in dict.keys(): os.write("%s: %s,\n" % (` key `, ` dict[key] `)) os.write("}\n") os.seek(0) return os.read()
def __seek_to_last_line(self, f): f.seek(0, 2) if f.tell() == 0: return # An empty file # Using lowlevel IO to make sure we play with single bytes fd = f.fileno() # os.lseek(fd, -1, 2) # Go to the last byte char = None # Here goes the delimiter portability, well, most stuff # won't work on non-unix (or even non-Linux), so doing # this the right way doesn't really pay off. while f.tell() > 0: char = os.read(fd, 1) if char == '\n': return # We found our place os.seek(-2, 1) # Seek to one before
def update_debugbtn(self): # BokehKzinRing::update_button_in() """update_debugbtn Button via an event lambda""" os = io.StringIO() self.debug(f"{self.name} Debug", os=os) os.seek(0) self.display.display(BokehKzinRing.brre.sub("<br/>", os.read()))
sessiondb_path = base64.b64decode(sessiondb_path) persist_file = None try: while True: # establish <I/O channel> socket connection. io_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) io_socket.connect(("127.0.0.1", io_port)) driver = TeletypeDriver(pid, ttyname, master, io_socket, control_connection) if persist_file: size = os.stat(persist_file).st_size pos = size - BUFFER_SIZE * 2 persist = open(persist_file) if pos > 0: os.seek(pos) try: while True: buf = os.read(persist.fileno(), BUFFER_SIZE) if not buf: break io_socket.send(buf) finally: persist.close() os.remove(persist_file) persist_file = None driver.drive_tty() if not driver.isalive(): trace("closed.")
try: while True: # establish <I/O channel> socket connection. io_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) io_socket.connect(("127.0.0.1", io_port)) driver = TeletypeDriver(pid, ttyname, master, io_socket, control_connection) if persist_file: size = os.stat(persist_file).st_size pos = size - BUFFER_SIZE * 2 persist = open(persist_file) if pos > 0: os.seek(pos) try: while True: buf = os.read(persist.fileno(), BUFFER_SIZE) if not buf: break io_socket.send(buf) finally: persist.close() os.remove(persist_file) persist_file = None driver.drive_tty() if not driver.isalive(): trace("closed.")