#!/usr/bin/env python
# coding:utf-8

msg0001 = 'no data provided, unable to continue'
msg0002 = 'unable to retrieve ldap data for: \'{0}\''
msg0003 = 'fetching configuration for: \'{0}\''
msg0004 = 'starting: \'{0}\''
msg0005 = 'unusable data provided: \'{0}\', unable to continue'
msg0006 = 'please specify ldap searchbase in config'
msg0007 = 'not retrieving ldap data as we are processing post remove for: \'{0}\''

class ModList(type):

	registered = {}

	# store all the types we know
	def __new__(cls, name, bases, attrs):

		# create the new type
		newtype = super(ModList, cls).__new__(cls, name, bases, attrs)

		# store it
		cls.registered[name] = newtype

		return newtype


	@classmethod
	def getModule(cls, name):

		# get a class from the registered classes
		return cls.registered[name]


	@classmethod
	def getModules(cls):

		# get a list of all registered classes
		return cls.registered.keys()


	@classmethod
	def delModule(cls,name):

		# delete a class from the registered classes
		try:
			del(cls.registered[name])

		except KeyError:
			pass


class PluginAbstract(object):

	def __init__(self, postcmd):

		self.class_name = self.__class__.__name__ 

		# get config-, logger-, ldap- and exit
		self.config = postcmd.getConfig()
		self.logger = postcmd.getLogger()
		self.exit = postcmd.getExit()
		self.gosa_ldap = postcmd.getGosaLdap()

		# split dn, cn and optional data
		try:
			self.dn = postcmd.data[0]
			if not self.dn: raise IndexError

			try:
				self.cn = self.dn.split(',')[0].split('=')[1]
				self.base = ','.join(self.dn.split(',')[1:])
			except IndexError:
				self.exit(253, msg0005.format(self.dn))
		except IndexError:
			self.exit(253, msg0001)

		try:
			self.opt = postcmd.data[1:]
		except IndexError:
			 self.opt = []

		self.searchbase = postcmd.gosa_ldap_cfg['searchbase']
		if not self.searchbase:
			self.exit(254, msg0006)

		if postcmd.action != 'remove':
			self.ldap_data = self.getLdapData()
		else:
			self.logger.debug(msg0007.format(self.dn))
			self.ldap_data = None


	def getConfig(self, sect, cfg_def, cfg_ver):

		self.logger.debug(msg0003.format(self.class_name+sect))
		self.config.updateDefaults(self.class_name+sect, cfg_def)
		self.config.updateVerify(cfg_ver)
		self.tasks_cfg = self.config.getSection(self.class_name+sect)


	def getLdapData(self):

		ldap_data = self.gosa_ldap.getAttributeList(self.dn)
		if ldap_data == None:
			self.exit(1, msg0002.format(self.dn))

		return ldap_data


	def precreate(self):

		mod_name = self.class_name+'.precreate'
		self.logger.debug(msg0004.format(mod_name))


	def premodify(self):

		mod_name = self.class_name+'.premodify'
		self.logger.debug(msg0004.format(mod_name))


	def preremove(self):

		mod_name = self.class_name+'.preremove'
		self.logger.debug(msg0004.format(mod_name))


	def create(self):

		mod_name = self.class_name+'.create'
		self.logger.debug(msg0004.format(mod_name))


	def modify(self):

		mod_name = self.class_name+'.modify'
		self.logger.debug(msg0004.format(mod_name))


	def remove(self):

		mod_name = self.class_name+'.remove'
		self.logger.debug(msg0004.format(mod_name))


if __name__ == '__main__':

	pass

