def __init__(self, video_type, filename, file_id, total_blocks, completed_blocks = [], extension='avi', save_dir=False):
		self.__abort = False
		self.__block_counter = 0
		self.__video_type = video_type
		self.__filename = filename
		self.__file_id = file_id
		self.__total_blocks = total_blocks
		self.__completed_blocks = completed_blocks
		self.__queue = MyPriorityQueue()
		self.__stream_disk_caching = ADDON.get_setting('stream_caching') == 'Disk'
		
		self.cache_file = vfs.join(WORK_DIRECTORY, self.__file_id + '.temp')
		self.state_file = vfs.join(WORK_DIRECTORY, self.__file_id + '.state')
		if video_type=='tvshow':
			save_dir = save_dir if save_dir else TVSHOW_DIRECTORY
			self.output_file = vfs.join(save_dir, vfs.clean_file_name(self.__filename) + '.' + extension)
		elif video_type=='movie':
			save_dir = save_dir if save_dir else MOVIE_DIRECTORY
			self.output_file = vfs.join(save_dir, vfs.clean_file_name(self.__filename) + '.' + extension)
		self.open()
class OutputHandler():
	def __init__(self, video_type, filename, file_id, total_blocks, completed_blocks = [], extension='avi', save_dir=False):
		self.__abort = False
		self.__block_counter = 0
		self.__video_type = video_type
		self.__filename = filename
		self.__file_id = file_id
		self.__total_blocks = total_blocks
		self.__completed_blocks = completed_blocks
		self.__queue = MyPriorityQueue()
		self.__stream_disk_caching = ADDON.get_setting('stream_caching') == 'Disk'
		
		self.cache_file = vfs.join(WORK_DIRECTORY, self.__file_id + '.temp')
		self.state_file = vfs.join(WORK_DIRECTORY, self.__file_id + '.state')
		if video_type=='tvshow':
			save_dir = save_dir if save_dir else TVSHOW_DIRECTORY
			self.output_file = vfs.join(save_dir, vfs.clean_file_name(self.__filename) + '.' + extension)
		elif video_type=='movie':
			save_dir = save_dir if save_dir else MOVIE_DIRECTORY
			self.output_file = vfs.join(save_dir, vfs.clean_file_name(self.__filename) + '.' + extension)
		self.open()

	def abort_all(self):
		self.__abort = True

	def open(self):
		try:
			self.__handle = open(self.cache_file, "r+b")
			#self.__handle = vfs.open(self.cache_file, "r+b")
		except IOError:
			self.__handle = open(self.cache_file, "wb")
			#self.__handle = vfs.open(self.cache_file, "wb")
		
	def flush(self):
		self.__handle.flush()
		
	def close(self):
		self.__handle.close()

	def write_block(self, block, offset, block_number):
		ADDON.log("write block %s %s of %s" % (block_number, self.__block_counter+1, self.__total_blocks), LOG_LEVEL.STANDARD)
		self.__handle.seek(offset, 0)
		self.__handle.write(block)
		self.__completed_blocks.append(block_number)
		ADDON.save_data(self.state_file, {"total_blocks": self.__total_blocks, "completed_blocks": self.__completed_blocks})
		self.flush()

	def queue_block(self, block, offset, block_number):
		self.__queue.put((block, offset, block_number), offset)

	def process_queue(self):
		while self.__block_counter < self.__total_blocks:
			if self.__abort: break;
			if self.__queue.empty() is False:
				block,offset,block_number = self.__queue.get()
				self.write_block(block, offset, block_number)
				self.increment_counter()
			else:
				time.sleep(.1)
		self.clean_up()
		
	def increment_counter(self):
		self.__block_counter += 1	
	
	def clean_up(self):
		self.close()
		if self.__video_type != 'stream':
			if ADDON.get_setting('enable_custom_output') == 'true':
				ADDON.log("Moving %s to %s" % (self.cache_file, self.output_file), LOG_LEVEL.VERBOSE)
				vfs.mv(self.cache_file, self.output_file)
			else:
				vfs.rename(self.cache_file, self.output_file, quiet=True)
		vfs.rm(self.state_file, quiet=True)