Changeset 431

Show
Ignore:
Timestamp:
01/27/03 23:40:16 (6 years ago)
Author:
sholloway
Message:

*** empty log message ***

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/RBFoundation/RBFoundation/Aspects/__init__.py

    r281 r431  
    2020##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    2121 
     22#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     23#~ Definitions  
     24#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    2225 
     26class Aspect(object): 
     27    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     28    #~ Special  
     29    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     30 
     31    def __new__(klass, *args, **kw): 
     32        result = super(Aspect, klass).__new__(klass, *args, **kw) 
     33        klass.OnAspectInsert(result) 
     34        return result 
     35 
     36    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     37    #~ Public Methods  
     38    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     39 
     40    def OnAspectInsert(self): pass 
     41    def OnAspectRemove(self): pass 
     42 
     43    def MetaAspect(klass, name, bases, data): 
     44        baseresult = type(name, bases, data) 
     45        name = '%s & %s' % (name, klass.__name__) 
     46        indict = {'_otherclass' : baseresult} 
     47        return type(name, (klass, baseresult), indict) 
     48    MetaAspect = classmethod(MetaAspect) 
     49 
     50    def InsertAspect(klass, obj): 
     51        indict = {'_otherclass' : obj.__class__, '_aspectclass':klass} 
     52        name = '%s & %s' % (obj.__class__.__name__, klass.__name__) 
     53        obj.__class__ = type(name, (klass, obj.__class__), indict) 
     54        klass.OnAspectInsert(obj) 
     55        return obj 
     56    InsertAspect = classmethod(InsertAspect) 
     57 
     58    def RemoveAspect(self): 
     59        assert isinstance(self, Aspect) 
     60        self.OnAspectRemove() 
     61        result = self._aspectclass 
     62        self.__class__ = self._otherclass 
     63        return result 
     64 
     65    def RemoveAllAspects(self): 
     66        result = [] 
     67        while isinstance(self, Aspect): 
     68            result.append(self.RemoveAspect()) 
     69        return result 
     70 
     71    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     72    #~ Protected Methods  
     73    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     74 
     75    def _AspectedOperation(self, method, *args, **kw): 
     76        """Wrapper so 'method' can be called in the origional class configuration""" 
     77        try:  
     78            thisclass = self.__class__ 
     79            self.__class__ = self._otherclass 
     80            result = method(*args, **kw) 
     81        finally: 
     82            self.__class__ = thisclass 
     83        return result 
     84