Author: dmeyer
Date: Fri Feb 29 11:24:00 2008
New Revision: 3133
Log:
Add some code similar to interfaces
Added:
trunk/base/test/interface.py
Modified:
trunk/base/src/__init__.py
trunk/base/src/utils.py
Modified: trunk/base/src/__init__.py
==============================================================================
--- trunk/base/src/__init__.py (original)
+++ trunk/base/src/__init__.py Fri Feb 29 11:24:00 2008
@@ -35,6 +35,9 @@
# Import the two important strutils functions
from strutils import str_to_unicode, unicode_to_str
+# add interface and implements
+from utils import add_interface, implements
+
# Add tempfile support.
from tmpfile import tempfile
Modified: trunk/base/src/utils.py
==============================================================================
--- trunk/base/src/utils.py (original)
+++ trunk/base/src/utils.py Fri Feb 29 11:24:00 2008
@@ -179,7 +179,7 @@
def set_running(name, modify = True):
"""
- Set this program as running with the given name. If modify is True,
+ Set this program as running with the given name. If modify is True,
the process name is updated as described in set_process_name().
"""
cmdline = open('/proc/%s/cmdline' % os.getpid()).readline()
@@ -195,7 +195,7 @@
"""
On Linux systems later than 2.6.9, this function sets the process name as
it
appears in ps, and so that it can be found with killall.
-
+
Note: name will be truncated to the cumulative length of the original
process name and all its arguments; once updated, passed arguments will no
longer be visible.
@@ -237,7 +237,7 @@
def __call__(self, *args, **kwargs):
return getattr(self._singleton(), self._name)(*args, **kwargs)
-
+
def __init__(self, classref):
self._singleton = None
@@ -256,7 +256,7 @@
class property(property):
"""
- Replaces built-in property function to extend it as per
+ Replaces built-in property function to extend it as per
http://bugs.python.org/issue1416
"""
def __init__(self, fget = None, fset = None, fdel = None, doc = None):
@@ -281,3 +281,45 @@
def getter(self, fget):
return self._add_doc(property(fget, self.fset, self.fdel),
fget.__doc__ or self.fget.__doc__)
+
+
+# list of interface definitions
+_interfaces = {}
+
+class implements(object):
+ """
+ Metaclass class generator that will inherit the object from all interfaces
+ defined on __init__. This can be used to inherit from a class which is not
+ visible when the base class is defined.
+ """
+ def __init__(self, *cls):
+ self._cls = cls
+
+ def __call__(self, name, bases, dict):
+ """
+ The metadata class
+ """
+ def create(*args, **kwargs):
+ inherit = list(bases)
+ for interface in self._cls:
+ if not interface in _interfaces:
+ raise AttributeError('%s is no valid interface' %
interface)
+ inherit.append(_interfaces[interface])
+ from new import classobj
+ if object in inherit:
+ inherit.remove(object)
+ obj = classobj(name, tuple(inherit), dict)(*args, **kwargs)
+ for interface in self._cls:
+ # call hidden __interface__ functions for __init__
+ func = getattr(_interfaces[interface], '__interface__', None)
+ if func is not None:
+ func(obj)
+ return obj
+
+ return create
+
+def add_interface(cls, name):
+ """
+ Add a class definition as interface with the given name.
+ """
+ _interfaces[name] = cls
Added: trunk/base/test/interface.py
==============================================================================
--- (empty file)
+++ trunk/base/test/interface.py Fri Feb 29 11:24:00 2008
@@ -0,0 +1,56 @@
+import kaa
+
+#
+# This is one file not directly imported by the application
+#
+
+class FooInterface(object):
+
+ def __interface__(self):
+ # __init__ function for interfaces, no args allowed
+ # will be called after __init__ of the real class
+ print 'Init FooInterface:', self.get_value()
+
+ def get_value(self):
+ # this is needed for __interface__, the real object has to
+ # define it somehow
+ raise NotImplementedError
+
+ def func1(self, string):
+ raise NotImplementedError
+
+ def func2(self):
+ print 'already defined'
+
+ def func3(self):
+ raise NotImplementedError
+
+kaa.add_interface(FooInterface, 'test.Foo')
+
+class BarInterface(object):
+
+ def is_also_foo(self):
+ print isinstance(self, FooInterface)
+
+kaa.add_interface(BarInterface, 'bar')
+
+
+#
+# This is the application
+#
+
+class MyObject(object):
+ __metaclass__ = kaa.implements('test.Foo', 'bar')
+
+ def get_value(self):
+ # required by test.Foo
+ return 1
+
+ def func1(self, string):
+ print string
+
+p = MyObject() # print Init FooInterface: 1
+p.func1('x') # --> print x
+p.is_also_foo() # --> print True
+p.func2() # --> print already defined
+p.func3() # --> raise NotImplementedError
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog