import time
from common import DateTime,platform_to_utf8
from xmlrpclib import DateTime as xmlDateTime
import ZODB
from persistent import Persistent
from pgtools.Catalog import Catalogable

### make it compliant with the dublin core FIXME, or maybe dublin core compliance must be in File

class InformationObject(Persistent,Catalogable):
	"""This is the base class for all information objects stored in the index.  Indexer implementors need to subclass from this, and implement all methods, in order to be able to store their information objects safely into the index.  Do not forget to call InformationObject.__init__(...) in your inherited class."""
	
	indexing_date = None
	
	InformationObject_catalogable = "indexing_date"
	
	def __init__(self):
		self.indexing_date = DateTime.gmtime(time.time())
		
# 	def is_valid(self):  # fixme think this through
		"""You should use this method to let the index determine whether the file is valid.  Index will automatically purge all objects which are not valid."""
		
	# maybe FIXME the responsibility for this conversion relies in the XML-RPC interface
	def as_marshallable_representation(self):
		""" Returns an XML-serializable representation of the object in a dictionary.  Strings are converted to UTF-8 FIXME prove that's needed, and dates/times are in the DateTime wrapper object used by xmlrpclib. """
		element = {}
		for key,value in self.__dict__.iteritems():
 			if type(value) is str: value = platform_to_utf8(value) # utf8 needed?
			elif type(value) is DateTime.DateTimeType:
				# this should return an xmlDateTime object representing the time in UTC (override a stupid mistake in DateTime from the xmlrpclib
				value = xmlDateTime(value.localtime()) # FIXME verify this is working, on-the-wire!
			elif type(value) in (float,int,long): value = value
			else: value = str(value)
			element[key] = value # FIXME need to process other types
		return element
		
	def __str__(self):
		return "information object"
