import logging
from PublicInterface import PublicInterface

class Component:
	"""The base class for all metadata service components.  Implementations of Component may present a public interface (PublicInterface) to the Component constructor, via Component.__init__(public_interface=MyInterfaceImplementation).
	
	Component implementations have a private log instance, accessible at self.log, which is a Python Logger object."""

	log = None
	public_interface = None
	__component_name = None

	def __init__(self,public_interface=None):
		"""All metadata services have a name"""
		# Contract:
		assert isinstance(public_interface,PublicInterface) or public_interface is None

		# Implementation:
		self.log = logging.getLogger(self.get_component_name())
		self.public_interface = public_interface

	def get_component_name(self):
		if self.__component_name: return self.__component_name  # in the future, if a name is set, there has to be a way to change the logger's name as well...
		a = repr(self.__class__)
		a = a.split(" ")[1]
		a = a.replace("\'","").replace(">","")
		return a

	def set_component_name(self,name):
		assert type(name) is str and len(name) > 0
		self.__component_name = name
		self.log = logging.getLogger(self.get_component_name())