Changeset 284

Show
Ignore:
Timestamp:
09/18/02 20:33:49 (6 years ago)
Author:
sholloway
Message:

Changed the interface of SkinContext? in order to be more flexable
Fixed some naming and comparison problems in WeakBind?
Fixed some itereation issues in DOT Skinning
Added an invisible DOT skin element
Added a LazyProperty? that calls ClassFactory? to instantiate a variable on the first reference.
Added a hash method to WeakBind?

Files:

Legend:

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

    r280 r284  
    7272    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    7373 
    74     def __init__(self, callback): 
    75         if callback is None:  
     74    def __init__(self, Callable, *weakrefArgs, **weakrefKw): 
     75        if Callable is None:  
    7676            # A "None" method... it should expire immediately 
    7777            pass 
    78         elif isinstance(callback, BoundCallableBase): 
     78        elif isinstance(Callable, BoundCallableBase): 
    7979            # One of those weird instances where we are proxying for an object very similar to ourself...  
    8080            # keep a hard reference to the BoundCallableBase 
    81             self.im_func = callback 
    82         elif isinstance(callback, typesMethods): 
     81            self.im_func = Callable 
     82        elif isinstance(Callable, typesMethods): 
    8383            # Wrap up the method and potential instance 
    84             if getattr(callback, 'im_self', None) is not None:  
    85                 self.im_self = weakref.ref(callback.im_self
    86             self.im_func = getattr(callback, 'im_func', callback
    87         elif isinstance(callback, typesInstances): 
     84            if getattr(Callable, 'im_self', None) is not None:  
     85                self.im_self = weakref.ref(Callable.im_self, *weakrefArgs, **weakrefKw
     86            self.im_func = getattr(Callable, 'im_func', Callable
     87        elif isinstance(Callable, typesInstances): 
    8888            # This is a "callable object", make a weakref to it 
    89             self.im_self = weakref.ref(callback
     89            self.im_self = weakref.ref(Callable, *weakrefArgs, **weakrefKw
    9090            self.im_func = None 
    91         elif callable(callback): 
     91        elif callable(Callable): 
    9292            # What the heck is it?  well... its supposed to be callable... 
    93             self.im_func = callback 
     93            self.im_func = Callable 
    9494 
    9595    def __nonzero__(self): 
     
    9999            return self.im_func and 1 or 0 
    100100 
    101     #def __call__(self, *args, **kw): 
    102     #    return self._DoCall(args, kw) 
     101    def __hash__(self): 
     102        if self.im_self is not None: 
     103            return hash(self.im_self) 
     104        else:  
     105            return hash(self.im_func) 
    103106 
    104107    def __eq__(self, other): 
    105108        if isinstance(other, WeakBoundCallable): 
    106109            return (self.im_self == other.im_self) and (self.im_func == other.im_func)  
     110        elif isinstance(other, weakref.ReferenceType):  
     111            return self.im_func == other or self.im_self == other 
    107112        elif callable(other):  
    108113            return self == WeakBoundCallable(other) 
     
    121126            else: raise weakref.ReferenceError, "weakly-referenced object no longer exists" 
    122127        else: return self.im_func(*args, **kw) 
     128 
     129    #def __call__(self, *args, **kw): 
     130    #    return self._DoCall(args, kw) 
    123131    __call__ = _DoCall 
    124132 
     
    136144    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    137145 
    138     callback = None 
     146    Callable = None 
    139147 
    140148    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    142150    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    143151 
    144     def __init__(self, callback): 
    145         if callback is None:  
     152    def __init__(self, Callable): 
     153        if Callable is None:  
    146154            pass # A "None" method... it should expire immediately 
    147         elif callable(callback): 
    148             self.callback = callback 
     155        elif callable(Callable): 
     156            self.Callable = Callable 
    149157 
    150158    def __nonzero__(self): 
    151         return self.callback is not None and 1 or 0 
     159        return self.Callable is not None and 1 or 0 
    152160 
    153161    #def __call__(self, *args, **kw): 
     
    156164    def __eq__(self, other): 
    157165        if isinstance(other, StrongBoundCallable): 
    158             return self.callback == other.callback 
     166            return self.Callable == other.Callable 
    159167        elif callable(other):  
    160168            return self == StrongBoundCallable(other) 
     
    165173 
    166174    def _DoCall(self, *args, **kw): 
    167         return self.callback(*args, **kw) 
     175        return self.Callable(*args, **kw) 
     176 
     177    #def __call__(self, *args, **kw): 
     178    #    return self._DoCall(args, kw) 
    168179    __call__ = _DoCall 
    169180 
    170181#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    171182 
    172 def BindCallable(callback, BindClass=BoundCallable): 
     183def BindCallable(Callable, BindClass=BoundCallable, *args, **kw): 
    173184    """Binds a callable object using BindClass only if needed."""  
    174     if isinstance(callback, BoundCallableBase): 
     185    if isinstance(Callable, BoundCallableBase): 
    175186        # It's already bound in some form or another,  
    176187        # but we have to guard it from being wrapped 
    177188        # again, because it is itself an instance. 
    178         return callback 
    179     elif isinstance(callback, typesRequireBinding): 
    180         # Well if it requires binding, then we should 
    181         # do so! 
    182         return BindClass(callback) 
     189        return Callable 
     190    elif isinstance(Callable, typesRequireBinding): 
     191        # Well if it requires binding, then we should do so! 
     192        return BindClass(Callable, *args, **kw) 
    183193    else: 
    184194        # not quite sure what it is, but it does not require binding 
    185         return callback 
    186  
    187 def WeakBindCallable(callback): 
     195        return Callable 
     196 
     197def WeakBindCallable(Callable, *args, **kw): 
    188198    """Weakly binds a callable object only if needed."""  
    189     return BindCallable(callback, WeakBoundCallable
    190  
    191 def StrongBindCallable(callback): 
     199    return BindCallable(Callable, WeakBoundCallable, *args, **kw
     200 
     201def StrongBindCallable(Callable, *args, **kw): 
    192202    """Strongly binds a callable object only if needed."""  
    193     return BindCallable(callback, StrongBoundCallable
     203    return BindCallable(Callable, StrongBoundCallable, *args, **kw
    194204 
    195205#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    207217            return self 
    208218     
     219    def onrelease(wr): 
     220        print "Released wr:", wr 
     221 
    209222    a = _Test() 
    210223    assert(sys.getrefcount(a) == 2) 
    211224 
    212     b1 = BindCallable(a.me) 
     225    b1 = WeakBindCallable(a.me, onrelease) 
    213226    assert(b1) 
    214227    assert(sys.getrefcount(a) == 2) 
    215228 
    216     b2 = BindCallable(a.me) 
     229    b2 = WeakBindCallable(a.me, onrelease) 
    217230    assert(b2) 
    218231    assert(sys.getrefcount(a) == 2) 
     
    226239    assert(b2 == b1) 
    227240 
    228     b3 = BindCallable(a.you
     241    b3 = WeakBindCallable(a.you, onrelease
    229242    assert(not b1 == b3) 
    230243    assert(b1 != b3) 
  • trunk/RBFoundation/RBFoundation/XMLClassBuilder.py

    r269 r284  
    205205                return self.NextFactorySet._GetElementFactory(*args, **kw) 
    206206 
    207         raise KeyError, "Could not find a class to build for node %r" % node 
     207        raise KeyError, "Could not find a class to build for node %r" % (node,) 
    208208 
    209209#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
  • trunk/RBSkinning/RBSkinning/SkinContext.py

    r280 r284  
    2626from __future__ import generators 
    2727 
    28 myStat = {} 
    2928#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    3029#~ Class 
     
    3635    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    3736 
    38     def __init__(self, NextContext): 
     37    def __init__(self, NextContext, Data={}): 
    3938        self._NextContext = NextContext 
     39        self.__dict__.update(Data) 
    4040 
    4141    def __getattribute__(self, name): 
     
    6262    #~ Protected Methods  
    6363    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     64 
     65    def _update(self, Data): 
     66        self.__dict__.update(Data) 
    6467 
    6568    def _OwnerContext(self, name, returnLast=0): 
  • trunk/RBSkinning/RBSkinning/XMLSkinner.py

    r253 r284  
    5353        self._elements, self._LastCompleteElement  = [], None 
    5454        parser = self._CreateParser() 
    55         self.context = SkinContext.SkinContext(contextIn
     55        self.context = SkinContext.SkinContext(contextIn, kwAddedContext
    5656        self.context.__skinner__ = weakref.ref(self) 
    5757        self.context.__root__ = rootPath 
    58         self.context.__dict__.update(kwAddedContext) 
    5958         
    6059        parser.Parse(xml) 
     
    7776        self._elements, self._LastCompleteElement  = [], None 
    7877        parser = self._CreateParser() 
    79         self.context = SkinContext.SkinContext(contextIn
     78        self.context = SkinContext.SkinContext(contextIn, kwAddedContext
    8079        self.context.__skinner__ = weakref.ref(self) 
    8180        self.context.__root__ = rootPath 
    82         self.context.__dict__.update(kwAddedContext) 
    8381 
    8482        parser.ParseFile(file) 
     
    9997 
    10098        parentContext = GraftElements[-1].context 
    101         self.context = SkinContext.SkinContext(parentContext
     99        self.context = SkinContext.SkinContext(parentContext, kwAddedContext
    102100        self.context.__skinner__ = weakref.ref(self) 
    103101        self.context.__root__ = rootPath 
    104         self.context.__dict__.update(kwAddedContext) 
    105102        self._LastCompleteElement.context = self.context 
    106103 
     
    128125 
    129126        parentContext = GraftElements[-1].context 
    130         self.context = SkinContext.SkinContext(parentContext
     127        self.context = SkinContext.SkinContext(parentContext, kwAddedContext
    131128        self.context.__skinner__ = weakref.ref(self) 
    132129        self.context.__root__ = rootPath 
    133         self.context.__dict__.update(kwAddedContext) 
    134130        self._LastCompleteElement.context = self.context 
    135131 
     
    151147        parser = self._CreateParser() 
    152148        element.context.__skinner__ = weakref.ref(self) 
    153         element.context.__dict__.update(kwAddedContext) 
     149        element.context._update(kwAddedContext) 
    154150 
    155151        parser.Parse(xml) 
     
    171167        element.context.__skinner__ = weakref.ref(self) 
    172168        if rootPath: element.context.__root__ = rootPath 
    173         element.context.__dict__.update(kwAddedContext) 
     169        element.context._update(kwAddedContext) 
    174170 
    175171        parser.ParseFile(file) 
  • trunk/RBSkinning/RBSkinning/dotSkin/DOTSkinObject.py

    r253 r284  
    5656        result = [] 
    5757        for each in self.Elements(): 
    58             result.append(each._toDOT(joinstr)) 
     58            try: toDOT = each._toDOT 
     59            except AttributeError: pass 
     60            else: result.append(toDOT(joinstr)) 
    5961        if joinstr is not None: 
    6062            return joinstr.join(result) 
  • trunk/RBSkinning/RBSkinning/dotSkin/edge.py

    r253 r284  
    4242                if data: result.append(data) 
    4343            else: 
    44                 result.append(each._toDOT(joinstr='', close=0)) 
     44                try: toDOT = each._toDOT 
     45                except AttributeError: pass 
     46                else: result.append(toDOT(joinstr='', close=0)) 
    4547        result = self.context.DOTEdgeJoin.join(result) 
    4648 
  • trunk/RBSkinning/RBSkinning/dotSkin/graph.py

    r263 r284  
    5656 
    5757        for each in self.Elements(): 
    58             result.append(each._toDOT(joinstr)) 
     58            try: toDOT = each._toDOT 
     59            except AttributeError: pass 
     60            else: result.append(toDOT(joinstr)) 
    5961 
    6062        if close: result.append('}') 
  • trunk/RBSkinning/RBSkinning/dotSkin/subgraph.py

    r263 r284  
    3737    _DOTStartFormat = 'subgraph %s {' 
    3838 
     39    def SkinInitialize(self): 
     40        pass 
     41 
    3942    def _toDOT(self, joinstr='\n', close=1): 
    4043        if close: 
  • trunk/RBSkinning/RBSkinning/wxTools/wxActiveXWrapper.py

    r282 r284  
    7878            return pywin.mfc.activex.Control.__getattr__(self, attr) 
    7979        except AttributeError: 
    80             return getattr(eo._eventObj, attr) 
     80            if self._eventObj: 
     81                return getattr(self._eventObj, attr) 
     82            else: raise 
    8183 
    8284    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~