All pastes #246929 Raw Edit

Cody Brocious

public python v1 · immutable
#246929 ·published 2006-11-12 21:22 UTC
rendered paste body
#!/usr/bin/env python# encoding: utf-8"""interface.pyCreated by Cody Brocious on 2006-11-11.Copyright (c) 2006 MP3tunes, LLC. All rights reserved."""import sysclass interface(object):    __public__    = {}    __protected__ = {}        def __new__(cls, *args, **kwargs):        obj = object.__new__(cls, *args, **kwargs)                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']                        try:                func = getattr(self, func_name)                return func            except:                pass                if func_name in frame.f_globals:            return frame.f_globals[func_name]                return 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):        __public__ = object.__getattribute__(self, '__public__')        if name in __public__:            return __public__[name]                __protected__ = object.__getattribute__(self, '__protected__')        if name in __protected__:            func = interface.getCallingFunction()                        if issubclass(self.__class__, func.im_class) or issubclass(func.im_class, self.__class__):                return __protected__[name]                try:            if object.__getattribute__(interface, name) or name in ('__public__', '__protected__'):                return object.__getattribute__(self, name)        except:            raise AttributeError, '%s object has no attribute %s' % (str(self.__class__.__name__), repr(name))                return None