from MetadataService.InformationObject import InformationObject
from MetadataService.common import DateTime
from platform import strip_mountpoint
import os
from InfospaceObjects import Volume

class File(InformationObject):
	volume = None
	path = None
	absolute_path = None
	size = None
	modification_date = None
	mimetype = None
	
	File_catalogable = "absolute_path,size,modification_date,mimetype".split(",")

	def __init__(self,filename,mimetype,volume):
		assert isinstance(volume,Volume)
		assert type(filename) is str and len(filename) > 0
		assert type(mimetype) is str and len(mimetype) > 0
		InformationObject.__init__(self)
		self.volume = volume
		self.mimetype = mimetype
		mp = volume.get_mountpoint()
		if mp is None: raise IOError, "The file's volume vanished before its time!"
		self.path = strip_mountpoint(filename,mp)
		self.absolute_path = os.path.normpath("/%s/%s"%(volume.guid,self.path))
		statdata = os.stat(filename)
		self.size = statdata.st_size
		self.modification_date = DateTime.gmtime(statdata.st_mtime) # document this.  This construct means: construct a DateTime representation based on a UTC ticks value (same as time.time())  We should move to using Zope's DateTime instead maybe, and using DateIndexes instead of fieldindexes for that.

# commented out for the time being.  this sucks!
# 	def as_marshallable_representation(self):
# 		m = InformationObject.as_marshallable_representation(self)
# 		vol = self.volume
# 		del m["volume"]
# 		m["volume_guid"] = vol.guid
# 		m["volume_mountpoint"] = vol.get_mountpoint()
# 		fn = self.get_filename()
# 		if fn is not None: m["filename"] = fn
# 		return m
# 	
	def get_filename(self):
		mp = self.volume.get_mountpoint()
		if mp: return os.path.join(mp,self.path)

	def __str__(self):
		return "file %s (of type %s)"%(self.get_filename(),self.mimetype)

	# for the index to call before returning results, and decide what to purge itself  fixme think this through this should be in the base class
# 	def is_valid(self):
# 		if os.path.exists(self.filename): return True
# 		return False
		
	# busquemosle un uso a esto
# 	def is_fresh(self):
# 		bla bla si la vejez del archivo no corresponde a lo que esta indexado, simplemente solici
