[Dev OpenGP] [61] Added plugin mechanism

[ Thread Index | Date Index | More opengp.tuxfamily.org/development Archives ]


Revision: 61
Author:   nicolaf
Date:     2009-03-18 21:16:28 +0100 (Wed, 18 Mar 2009)

Log Message:
-----------
Added plugin mechanism

Modified Paths:
--------------
    trunk/src/lib/ogp/plugins/__init__.py
    trunk/src/lib/ogp/plugins/plugin.py

Added Paths:
-----------
    trunk/src/lib/ogp/plugins/test/
    trunk/src/lib/ogp/plugins/test/__init__.py
    trunk/src/lib/ogp/plugins/test/test.py


Modified: trunk/src/lib/ogp/plugins/__init__.py
===================================================================
--- trunk/src/lib/ogp/plugins/__init__.py	2009-03-18 16:00:40 UTC (rev 60)
+++ trunk/src/lib/ogp/plugins/__init__.py	2009-03-18 20:16:28 UTC (rev 61)
@@ -1,2 +1,15 @@
 #!/usr/bin/python
 # -*- coding: utf-8 -*
+
+from plugin import *
+from pkg_resources import resource_filename
+from os.path import dirname,isdir,join
+from os import listdir
+from imp import *
+path = dirname(resource_filename(__name__, '__init.py__'))
+for d in listdir(path):
+	if isdir(join(path,d)):
+		try:
+			load_package('ogp.plugins.' + d,join(path,d))
+		except:
+			pass

Modified: trunk/src/lib/ogp/plugins/plugin.py
===================================================================
--- trunk/src/lib/ogp/plugins/plugin.py	2009-03-18 16:00:40 UTC (rev 60)
+++ trunk/src/lib/ogp/plugins/plugin.py	2009-03-18 20:16:28 UTC (rev 61)
@@ -4,21 +4,34 @@
 from metaclass import Metaclass
 from abstractmethod import AbstractMethod
 
-class Plugin (object):
-	__metaclass__ = Metaclass
+class Plugin(object):
+	#__metaclass__ = Metaclass
 
 	def __init__(self, dn):
 		self.__dn = dn
+	
+	__registeredPlugins = dict()
+	def __getPluginFromName(name):
+		return Plugin.__registeredPlugins[name]
+	getPluginFromName = staticmethod(__getPluginFromName)
 
-	def getPluginFromName(plugin):
-		return
-	getPluginFromName = staticmethod('getPluginFromName')
+	def __registerPlugin(pluginClass):
+		try:
+			Plugin.__registeredPlugins[pluginClass.name]
+			raise OgpPluginError("registerPlugin: duplicated plugin name '" + pluginClass.name + "'")
+		except:
+			pass
+		Plugin.__registeredPlugins[pluginClass.name] = pluginClass
+	registerPlugin = staticmethod(__registerPlugin)
+
+	def __getRegisteredPlugins():
+		return Plugin.__registeredPlugins.copy()
+	getRegisteredPlugins = staticmethod(__getRegisteredPlugins)
 	
 	# Abstract methods
 	#mergeDescription = AbstractMethod('mergeDescription')
 
-	def getName():
-		return ""
+	name = None
 
 	def installConf(self):
 		pass
@@ -47,3 +60,11 @@
 	def pushFile(self, file, content):
 		pass
 
+class OgpPluginError(Exception):
+	def __init__(self, value):
+		assert isinstance(value, str)
+		self.value = value
+	
+	def __str__(self):
+		return repr("OgpXmlError: " + self.value)
+

Added: trunk/src/lib/ogp/plugins/test/__init__.py
===================================================================
--- trunk/src/lib/ogp/plugins/test/__init__.py	                        (rev 0)
+++ trunk/src/lib/ogp/plugins/test/__init__.py	2009-03-18 20:16:28 UTC (rev 61)
@@ -0,0 +1,7 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*
+
+from ogp.plugins.plugin import *
+from test import *
+
+Plugin.registerPlugin(Test)


Property changes on: trunk/src/lib/ogp/plugins/test/__init__.py
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/src/lib/ogp/plugins/test/test.py
===================================================================
--- trunk/src/lib/ogp/plugins/test/test.py	                        (rev 0)
+++ trunk/src/lib/ogp/plugins/test/test.py	2009-03-18 20:16:28 UTC (rev 61)
@@ -0,0 +1,7 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*
+
+from ogp.plugins.plugin import *
+
+class Test(Plugin):
+	name = "test"


Property changes on: trunk/src/lib/ogp/plugins/test/test.py
___________________________________________________________________
Name: svn:executable
   + *


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/