[Dev OpenGP] [51] Just forgot something O:-) |
[ Thread Index |
Date Index
| More opengp.tuxfamily.org/development Archives
]
Revision: 51
Author: alband85
Date: 2009-03-18 13:29:34 +0100 (Wed, 18 Mar 2009)
Log Message:
-----------
Just forgot something O:-)
Added Paths:
-----------
trunk/src/lib/ogp/plugins/
trunk/src/lib/ogp/plugins/__init__.py
trunk/src/lib/ogp/plugins/abstractmethod.py
trunk/src/lib/ogp/plugins/metaclass.py
trunk/src/lib/ogp/plugins/plugin.py
Added: trunk/src/lib/ogp/plugins/__init__.py
===================================================================
--- trunk/src/lib/ogp/plugins/__init__.py (rev 0)
+++ trunk/src/lib/ogp/plugins/__init__.py 2009-03-18 12:29:34 UTC (rev 51)
@@ -0,0 +1,2 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*
Added: trunk/src/lib/ogp/plugins/abstractmethod.py
===================================================================
--- trunk/src/lib/ogp/plugins/abstractmethod.py (rev 0)
+++ trunk/src/lib/ogp/plugins/abstractmethod.py 2009-03-18 12:29:34 UTC (rev 51)
@@ -0,0 +1,52 @@
+# See http://code.activestate.com/recipes/266468/ for further details
+#
+# Note for further versions: http://docs.python.org/library/abc.html
+# Abstract base classes will be supported in Python 2.6
+
+
+class AbstractMethod (object):
+ """Defines a class to create abstract methods
+
+ @example:
+ class Foo:
+ foo = AbstractMethod('foo')
+ """
+
+ def __init__(self, func):
+ """Constructor
+
+ @params func: name of the function (used when raising an
+ exception).
+ @type func: str
+ """
+ self._function = func
+
+ def __get__(self, obj, type):
+ """Get callable object
+
+ @returns An instance of AbstractMethodHelper.
+
+ This trickery is needed to get the name of the class for which
+ an abstract method was requested, otherwise it would be
+ sufficient to include a __call__ method in the AbstractMethod
+ class itself.
+ """
+ return self.AbstractMethodHelper(self._function, type)
+
+class AbstractMethodHelper (object):
+ """Abstract method helper class
+
+ An AbstractMethodHelper instance is a callable object that
+ represents an abstract method.
+ """
+ def __init__(self, func, cls):
+ self._function = func
+ self._class = cls
+
+ def __call__(self, *args, **kwargs):
+ """Call abstract method
+
+ Raises a TypeError, because abstract methods can not be
+ called.
+ """
+ raise TypeError('Abstract method `' + self._class.__name__ + '.' + self._function + '\' called')
Added: trunk/src/lib/ogp/plugins/metaclass.py
===================================================================
--- trunk/src/lib/ogp/plugins/metaclass.py (rev 0)
+++ trunk/src/lib/ogp/plugins/metaclass.py 2009-03-18 12:29:34 UTC (rev 51)
@@ -0,0 +1,51 @@
+# For more informations, see http://code.activestate.com/recipes/266468/
+
+from abstractmethod import AbstractMethod
+
+class Metaclass (type):
+
+ def __init__(cls, name, bases, *args, **kwargs):
+ """Configure a new class
+
+ @param cls: Class object
+ @param name: Name of the class
+ @param bases: All base classes for cls
+ """
+ super(Metaclass, cls).__init__(cls, name, bases, *args, **kwargs)
+
+ # Detach cls.new() from class Metaclass, and make it a method
+ # of cls.
+ cls.__new__ = staticmethod(cls.new)
+
+ # Find all abstract methods, and assign the resulting list to
+ # cls.__abstractmethods__, so we can read that variable when a
+ # request for allocation (__new__) is done.
+ abstractmethods = []
+ ancestors = list(cls.__mro__)
+ ancestors.reverse() # Start with __builtin__.object
+ for ancestor in ancestors:
+ for clsname, clst in ancestor.__dict__.items():
+ if isinstance(clst, AbstractMethod):
+ abstractmethods.append(clsname)
+ else:
+ if clsname in abstractmethods:
+ abstractmethods.remove(clsname)
+
+ abstractmethods.sort()
+ setattr(cls, '__abstractmethods__', abstractmethods)
+
+ def new(self, cls, *args, **kwargs):
+ """Allocator for class cls
+
+ @param self: Class object for which an instance should be
+ created.
+
+ @param cls: Same as self.
+ """
+ if len(cls.__abstractmethods__):
+ raise NotImplementedError('Can\'t instantiate class `' + \
+ cls.__name__ + '\';\n' + \
+ 'Abstract methods: ' + \
+ ", ".join(cls.__abstractmethods__))
+
+ return object.__new__(self)
Added: trunk/src/lib/ogp/plugins/plugin.py
===================================================================
--- trunk/src/lib/ogp/plugins/plugin.py (rev 0)
+++ trunk/src/lib/ogp/plugins/plugin.py 2009-03-18 12:29:34 UTC (rev 51)
@@ -0,0 +1,49 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*
+
+from metaclass import Metaclass
+from abstractmethod import AbstractMethod
+
+class Plugin (object):
+ __metaclass__ = Metaclass
+
+ def __init__(self, dn):
+ self.__dn = dn
+
+ def getPluginFromName(plugin):
+ return
+ getPluginFromName = staticmethod('getPluginFromName')
+
+ # Abstract methods
+ #mergeDescription = AbstractMethod('mergeDescription')
+
+ def getName():
+ return ""
+
+ def installConf(self):
+ pass
+
+ def help(self, cmd):
+ pass
+
+ def runCommand(self, argv):
+ pass
+
+ def update(self):
+ """
+ Commit changes
+ """
+ pass
+
+ def cancel(self):
+ """
+ Do not commit and delete changes.
+ """
+ pass
+
+ def pullFile(self, file, fullTree=False):
+ pass
+
+ def pushFile(self, file, content):
+ pass
+