コード例 #1
0
ファイル: FunctionInjection.py プロジェクト: rexrainbow/Cube
def InstMemberDictInject(objInstances):
    class _instance_values: 
        pass
        
    iter = AllInstanceKlassIter(objInstances)
    evtMgr = EventMgrGet()
    objInstances._vals_=dict()
    for klass in iter:
        # override __getattr__
        klass._old_getattr_fn = klass.__getattr__
        def my_getattr(self, name):
            objkey = self.__dict__['__instance__']
            if objInstances._vals_.has_key(objkey):
                if hasattr(objInstances._vals_[objkey], name):
                    return getattr(objInstances._vals_[objkey], name)
            # None if not in default name space
            return self._old_getattr_fn(name)
        klass.__getattr__ = my_getattr
   
        # override __setattr__
        klass._old_setattr_fn = klass.__setattr__
        def my_setattr(self, name, value):
            if ( (name.lower() in self.__class__._otherAttrib) or 
                 (hasattr(self.__class__, name)) ):
                self._old_setattr_fn(name, value)
            else:
                objkey = self.__dict__['__instance__']
                if not objInstances._vals_.has_key(objkey):
                    objInstances._vals_[objkey]=_instance_values()
                setattr(objInstances._vals_[objkey], name, value)
        klass.__setattr__ = my_setattr
    
    # clean objInstances._vals_[objkey] when instance destroyed
    def _claen_vals(evt):
        objkey = evt.inst.__dict__['__instance__']
        if objInstances._vals_.has_key(objkey):
            del objInstances._vals_[objkey]
    evtMgr.Attach(_EVT_INST_DESTROY_FINAL(), _claen_vals)
コード例 #2
0
ファイル: FunctionInjection.py プロジェクト: rexrainbow/Cube
 def my_destroy(self):
     self._old_destroy_fn()
     param = EvtParamInstDestroy(self)
     evtMgr.Notify(EVT_INST_DESTROY(), [param])
     evtMgr.Notify(_EVT_INST_DESTROY_FINAL(), [param])