Changeset 217

Show
Ignore:
Timestamp:
07/17/02 17:12:08 (6 years ago)
Author:
sholloway
Message:

Refactored Foundation.Skinning.StateSkin? to a more consistent state
Added a Foundation.Skinning.StateSkin?.machine module
Refactored Foundation.Skinning.xmlPython Skin into Foundation.Skinning.SkinObject? (pushed the functionality down a level)
Fixed some bugs with StateMachine?
Added State Machine Demo
Removed old State Machine Demo (out-of-date)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/RBJabber/RBJabber/SubjectObserver/StateMachine.py

    r176 r217  
    5151    def __int__(self): 
    5252        for each in self: 
    53             if int(each): return 1 
     53            if each.GetState():  
     54                return 1 
    5455        return 0 
    5556 
    5657    def SetHandled(self): 
    5758        for each in self: 
    58             if int(each): each.SetHandled() 
     59            if each.GetState():  
     60                each.SetHandled() 
    5961     
    6062#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    6365    def __int__(self): 
    6466        for each in self: 
    65             if not int(each):  
     67            if not each.GetState():  
    6668                return 0 
    6769        if self: return 1 
     
    7072    def SetHandled(self): 
    7173        for each in self: 
    72             if int(each): each.SetHandled() 
     74            if each.GetState():  
     75                each.SetHandled() 
    7376     
    7477#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    8083 
    8184    value = 0 
    82     _handled = 0 
     85    incvalue = 1 
     86    precondition = None 
    8387 
    8488    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    9296        self._Influxs = InfluxsClass() 
    9397 
    94     def __int__(self):  
     98    def __int__(self): 
     99        return self.GetState() 
     100 
     101    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     102    #~ Public Methods  
     103    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     104 
     105    def GetState(self): 
    95106        return self.value 
    96107 
    97     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    98     #~ Public Methods  
    99     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    100  
    101     def SetState(self, value =1): 
    102         if value != self.value: 
     108    def SetState(self, value=None): 
     109        if value is None: value = self.incvalue 
     110        if self.value != value: 
    103111            self.value = value  
    104             if self.value and not self._Influxs: 
    105                 self.UpdateObservers(precondition=self.value) 
    106             self.UpdateObservers(state=self) 
    107             if self._handled > 0:  
    108                 self._handled = 0 
    109                 self.value = 0 
     112 
     113            result = self.PushState() 
     114 
     115            return result 
     116 
     117    def PushState(self): 
     118        if self.value and not self._Influxs: 
     119            self.UpdateObservers(precondition=self.value) 
     120 
     121        self.UpdateObservers(state=self) 
     122 
     123        return self.GetState() 
    110124 
    111125    def SetHandled(self, handled=1): 
    112         self._handled += handled  
     126        self.SetState(max(self.value-handled, 0)) 
    113127 
    114128    def CheckInfluxs(self, subject, state): 
    115         if state and not self.value: 
    116             precondition = int(self._Influxs) 
    117             if precondition: 
    118                 lock = self.Lock() 
    119                 self.UpdateObservers(precondition=precondition) 
    120                 self._Influxs.SetHandled() 
    121                 lock.SetLock(None) 
     129        if state and not self.value and not self.precondition: 
     130            self.precondition = int(self._Influxs) 
     131            if self.precondition: 
     132                self.__publishing = 0 
     133                self.preconditionlock = self.Lock() 
     134                self.UpdateObservers(precondition=self.precondition) 
     135 
     136    __publishing = 0 
     137    def PublishSet(self, subject, state): 
     138        if self.precondition and not self.__publishing: 
     139            self.__publishing = 1 
     140            self._Influxs.SetHandled() 
     141            self.preconditionlock.SetLock(None) 
     142            del self.preconditionlock 
     143            self.precondition = None 
    122144         
    123145    def AddInflux(self, Influx): 
    124146        self._Influxs.append(Influx) 
    125         Influx.AddObserver(self.CheckInfluxs, 'state', priority=-1) 
     147        Influx.AddObserver('state', self.CheckInfluxs, priority=-1) 
     148        Influx.AddObserver('state', self.PublishSet, priority=-2) 
    126149 
    127150    def AddOutflux(self, Outflux): 
     
    131154 
    132155class State(StateSubject): 
     156    """NOTE: This class is for backward compatability!!!  Please use StateSubject instead.""" 
     157 
     158    def SetHandled(self, handled=1): 
     159        pass  
     160 
     161    def AddInflux(self, Influx): 
     162        self._Influxs.append(Influx) 
     163        # Reverse the order of callable and category for legacy support 
     164        Influx.AddObserver(self.CheckInfluxs, 'state', priority=-1) 
     165        Influx.AddObserver(self.PublishSet, 'state', priority=-2) 
     166 
    133167    def AddObserver(self, callable, category='', **kw): 
     168        # Reverse the order of callable and category for legacy support 
    134169        category = category or 'precondition' 
    135170        return CategorySubject.AddObserver(self, category, callable, **kw) 
     
    164199 
    165200    a.SetState() 
     201    assert d.GetState() 
    166202 
    167203#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
  • trunk/RBSkinning/RBSkinning/SkinObject.py

    r214 r217  
    6060        } 
    6161 
     62    globalnamespace = {} 
     63 
    6264    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    6365    #~ Special  
     
    173175            return self.parent().Unravel() 
    174176 
     177    #~ Parent Search Patterns ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     178 
     179    def FindParent(self, *Types): 
     180        p = self.parent() 
     181        while p: 
     182            if isinstance(p, *Types): 
     183                return p 
     184            else: p = p.parent() 
     185        return None 
     186 
     187    def FindParentOrObject(self, *Types): 
     188        p = self.parent() 
     189        while p: 
     190            if isinstance(p.object, Types) or isinstance(p, Types): 
     191                return p.object 
     192            else: p = p.parent() 
     193        return None 
     194 
     195    def FindParentObject(self, *Types): 
     196        p = self.parent() 
     197        while p: 
     198            if isinstance(p.object, Types): 
     199                return p.object 
     200            else: p = p.parent() 
     201        return None 
     202 
     203    #~ Eval Helpers ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     204 
     205    def _GetEvalLocals(self, **variables): 
     206        variables['self'] = weakref.proxy(self) 
     207        variables['context'] = weakref.proxy(self.context) 
     208        if self.parent(): 
     209            variables['parentObj'] = self.parent().object 
     210        return variables 
     211 
     212    def EvalLocal(self, code, **variables): 
     213        return eval(code, self.globalnamespace, self._GetEvalLocals(**variables)) 
     214         
    175215    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    176216    #~ Private Methods  
  • trunk/RBSkinning/RBSkinning/wxPythonSkin/wxSkinObject.py

    r211 r217  
    9191 
    9292    def wxGetWindowCollectorParent(self, CollectorTypes=None): 
    93         p = self.parent() 
    94         while p: 
    95             if isinstance(p, CollectorTypes or wxSkinWindowCollectorObject): 
    96                 return p 
    97             else: p = p.parent() 
    98         return None 
     93        if not CollectorTypes: 
     94            return self.FindParent(wxSkinWindowCollectorObject) 
     95        elif isinstance(CollectorTypes, (tuple, list)): 
     96            return self.FindParent(*CollectorTypes) 
     97        else: return self.FindParent(CollectorTypes) 
    9998 
    10099    def wxGetParentObject(self, types): 
    101         p = self.parent() 
    102         while p: 
    103             if isinstance(p.object, types) or isinstance(p, types): 
    104                 return p.object 
    105             else: p = p.parent() 
    106         return None 
     100        if isinstance(types, (tuple, list)): 
     101            return self.FindParentOrObject(*types) 
     102        else: return self.FindParentOrObject(types) 
    107103 
    108104    def wxInitialStandardOptions(self): 
  • trunk/RBSkinning/RBSkinning/xmlPython/PySkinObject.py

    r150 r217  
    4646    default_settings = SkinObject.default_settings.copy() 
    4747    default_settings['phase'] = 'initialize' 
    48     namespace = {} 
    4948     
    5049    def SkinInitialize(self): 
     
    5958        return None 
    6059 
    61     def _getLocals(self, Variables={}): 
    62         result = Variables.copy() 
    63         result['self'] = weakref.proxy(self) 
    64         result['context'] = weakref.proxy(self.context) 
    65         if self.parent(): 
    66             result['parentObj'] = self.parent().object 
    67         return result 
    68  
    6960    def AddNamespace(self, namespace): 
    7061        if isinstance(namespace, ModuleType): 
    71             self.namespace.update(vars(namespace)) 
     62            self.globalnamespace.update(vars(namespace)) 
    7263        else: 
    73             self.namespace.update(namespace) 
     64            self.globalnamespace.update(namespace) 
  • trunk/RBSkinning/RBSkinning/xmlPython/evaluate.py

    r216 r217  
    5252 
    5353    def ExecuteXML(self, **Variables): 
    54         return eval(self.GetCode(), self.namespace, self._getLocals(Variables)) 
     54        return self.EvalLocal(self.GetCode()) 
    5555     
  • trunk/RBSkinning/RBSkinning/xmlPython/for_.py

    r214 r217  
    5555        if self.parent(): 
    5656            self.parent().PushContext() 
    57         self.object = eval(self.settings['collection'], self.namespace, self._getLocals()
     57        self.object = self.EvalLocal(self.settings['collection']
    5858        self.StoreChildren(self.object and 1 or 0) 
    5959 
  • trunk/RBSkinning/RBSkinning/xmlPython/if_.py

    r214 r217  
    5454    def SkinInitialize(self): 
    5555        self.PushContext() 
    56         self.object = eval(self.settings['condition'], self.namespace, self._getLocals()
     56        self.object = self.EvalLocal(self.settings['condition']
    5757        self.StoreChildren(self.object and 1 or 0) 
    5858 
  • trunk/RBSkinning/RBSkinning/xmlPython/inline.py

    r210 r217  
    6767 
    6868        self.object = compile(execCode, 'xmlPython.inline', 'exec') 
    69         exec self.object in self.namespace, self._getLocals(Variables) 
     69        exec self.object in self.globalnamespace, self._GetEvalLocals(**Variables) 
    7070        return self.object 
  • trunk/RBSkinning/RBSkinning/xmlPython/script.py

    r215 r217  
    5757            reload(module) 
    5858        call = getattr(module, self.settings['call']) 
    59         args = eval(self.settings['args'], self.namespace, self._getLocals(Variables)
    60         kw = eval(self.settings['kw'], self.namespace, self._getLocals(Variables)
     59        args = self.EvalLocal(self.settings['args']
     60        kw = self.EvalLocal(self.settings['kw']
    6161        if bAddToSysPath: sys.path.remove(self.context.__root__) 
    6262        return call(*args, **kw)