rendered paste body#!/usr/bin/env python# encoding: utf-8"""interface.pyCreated by Cody Brocious on 2006-11-11.Copyleft (c) 2006 Cody Brocious All rights reversed."""import sysclass interface(object): def __new__(cls, *args, **kwargs): obj = object.__new__(cls, *args, **kwargs) try: obj.__public__ except: obj.__public__ = {} try: obj.__public_attributes__ except: obj.__public_attributes__ = () obj.collectInterfaces() return obj @classmethod def addInterface(cls, func, interface): try: __interface__ = func.__interface__ except: __interface__ = func.__interface__ = [] __interface__.append(interface) @classmethod def public(cls, func): interface.addInterface(func, interface.public) return func @classmethod def protected(cls, func): interface.addInterface(func, interface.protected) return func @classmethod def secure(cls, func): interface.addInterface(func, interface.secure) return func @classmethod def getCallingFunction(cls, depth=2): frame = sys._getframe(depth) func_name = frame.f_code.co_name if 'self' in frame.f_locals: self = frame.f_locals['self'] elif 'cls' in frame.f_locals: self = frame.f_locals['cls'] else: self = None if self: try: func = object.__getattribute__(self, func_name) if self.__class__ != type: self = self.__class__ return self, func except: pass if func_name in frame.f_globals: return None, frame.f_globals[func_name] return None, None def collectInterfaces(self): for name in dir(self): obj = object.__getattribute__(self, name) try: if interface.public in obj.__interface__: self.__public__[name] = obj elif interface.protected in obj.__interface__: self.__protected__[name] = obj except (KeyError, AttributeError): pass def __getitem__(self, name): return self.__public__[name] def __getattribute__(self, name): # print 'getattr: %08x' % id(self), name if name in ('__bases__', '__methods__', '__members__', '__class__'): return object.__getattribute__(self, name) __public__ = object.__getattribute__(self, '__public__') if name in __public__: return __public__[name] __public_attributes__ = object.__getattribute__(self, '__public_attributes__') if name in __public_attributes__: return object.__getattribute__(self, name) func_cls, func = interface.getCallingFunction() __class__ = object.__getattribute__(self, '__class__') if not func_cls or not func or (not issubclass(__class__, func_cls) and not issubclass(func_cls, __class__)): raise AttributeError, '%s object has no attribute %s or it is protected' % (str(__class__.__name__), repr(name)) try: return object.__getattribute__(self, name) except: raise AttributeError, '%s object has no attribute %s' % (str(__class__.__name__), repr(name)) return None