Files @ b0515ceb502d
Branch filter:

Location: Morevna/src/datafile.py - annotation

Laman
actually flushing the files
from hashtree import HashTree


BLOCK_SIZE = HashTree.BLOCK_SIZE


class DataFile:
	def __init__(self, file_handle):
		self._last_index = 0
		self._f = file_handle

	@staticmethod
	def open(filename, mode="rb"):
		return DataFile(open(filename, mode=mode))

	def write_at(self, i, block_data):
		if i!=self._last_index+1:
			self._f.seek(i*BLOCK_SIZE)
		self._f.write(block_data)
		self._last_index = i

	def read_from(self, i, byte_count=BLOCK_SIZE):
		if i!=self._last_index+1:
			self._f.seek(i*BLOCK_SIZE)
		self._last_index = i
		return self._f.read(byte_count)

	def flush(self):
		self._f.flush()

	def close(self):
		self._f.close()