#!/usr/bin/env python

# FIXME standardize on a common response/faultCode format and common codes

class PublicInterface:
	"""The public interface for your component. # fixme clarify this docs
	
	To publish a set of functions for your component, you implement the PublicInterface class and in your component's constructor, call Component.__init__(MyInterface(component=self)).  In any of your implementation's methods, you will have the self.__component attribute available for you to operate on your component.

	In your implementation of the interface, each public (non _) method and object is available for the whole world to use through the interface servers.  When the user requests a method through an interface server, the interface server automatically locates the appropriate method to run, passing the user ID (which can be None) and the user-supplied parameters to your method."""

	_component = None
	_name = None
	
	def __init__(self,name,component):
		"""The default implementation automatically assigns the passed component to self._component"""
		#contract
		from Component import Component
		assert isinstance(component,Component)
		assert type(name) is str and len(name) > 0
		#implementation
		self._component = component
		self._name = name
		
	def get_name(self):
		return self._name

	def get_component(self): # proteger esta funcion de outside accessors
		return self._component

#FIXME validate each request per client UID like if uid != 0 (it could be none coming from the web interface, e.g.) then raise something which notifies the client that you're fucked because you're not root
# class AdministrativeInterface:
# 	""" An abstraction encapsulating admin-only requests to the metadata service """
# 	__metadata_service = None
#
# 	def __init__(self,metadata_service):
# 		self.__metadata_service = metadata_service
# 		self.__log = logging.getLogger("interfaces.administrative")
#
# 	def exit(self):
# 		""" Terminate the metadata service cleanly """
# 		self.__log.debug("Exit request on public interface")
# 		self.__metadata_service.request_exit()
# 		return 0
#
# 	def reloadConfig(self):
# 		""" Reload configuration file and plugins  """
# 		self.__log.debug("Reload configuration request on public interface")
# 		self.__metadata_service.request_reload_config()
# 		return 0
#
# # 	def getCacheSize(self):
# # 		""" Get the cache size"""
# # 		self.__log.debug("Cache size request on public interface")
# # 		return self.__metadata_service.index_database.getCacheSize()
# #
# # 	def getCacheDetailSize(self):
# # 		""" Get the cache size, detailed by connection """
# # 		self.__log.debug("Cache detail size request on public interface")
# # 		return self.__metadata_service.index_database.cacheDetailSize()
# #
# # 	def getObjectCount(self):
# # 		""" Get the number of stored objects """
# # 		self.__log.debug("Object count request on public interface")
# # 		return self.__metadata_service.index_database.objectCount()
# #
# # 	def minimizeCache(self,size):
# # 		""" Get the number of stored objects """
# # 		self.__log.debug("Cache minimize request on public interface")
# # 		self.__metadata_service.index_database.cacheMinimize(size)
# # 		return 0
#
# #FIXME not here! create a subclass of DatabaseOperation named DatabaseModificationOperation which, when passed to the execute method, shall create a new transaction and run the operation in that context.  Semantics for databaseoperation should also be changed.  Document the semantics and document the fact that if the user wants a transaction to be aborted, all he has to do is simply raise an exception (check that's the case!)
# #FIXME not here, but rather in the HTML plugin , the KDE metainfo is returning fucked up strings for html document titles, better to pull the title out with a regular expression!
# #FIXME to return faults we can use a Fault instance defined somewhere in SimpleXMLRPCServer!
# #FIXME document the fact that all methods need to return at least one non-None value, such as "OK" or 0
#
# class MetadataServicePublicInterface(PublishableInterface):
# 	""" This is the official public interface, which aggregates all other interfaces. """
# 	index = None
# 	administration = None
#
# 	def __init__(self,metadata_service):
# 		self.index = IndexInterface(metadata_service)
# 		self.administration = AdministrativeInterface(metadata_service)
#
