| 1 |
#!/usr/bin/env python |
|---|
| 2 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 3 |
##~ License |
|---|
| 4 |
##~ |
|---|
| 5 |
##- The RuneBlade Foundation library is intended to ease some |
|---|
| 6 |
##- aspects of writing intricate Jabber, XML, and User Interface (wxPython, etc.) |
|---|
| 7 |
##- applications, while providing the flexibility to modularly change the |
|---|
| 8 |
##- architecture. Enjoy. |
|---|
| 9 |
##~ |
|---|
| 10 |
##~ Copyright (C) 2002 TechGame Networks, LLC. |
|---|
| 11 |
##~ |
|---|
| 12 |
##~ This library is free software; you can redistribute it and/or |
|---|
| 13 |
##~ modify it under the terms of the BSD style License as found in the |
|---|
| 14 |
##~ LICENSE file included with this distribution. |
|---|
| 15 |
##~ |
|---|
| 16 |
##~ TechGame Networks, LLC can be reached at: |
|---|
| 17 |
##~ 3578 E. Hartsel Drive #211 |
|---|
| 18 |
##~ Colorado Springs, Colorado, USA, 80920 |
|---|
| 19 |
##~ |
|---|
| 20 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 21 |
|
|---|
| 22 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 23 |
#~ Imports |
|---|
| 24 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 25 |
|
|---|
| 26 |
import types |
|---|
| 27 |
import time |
|---|
| 28 |
import bisect |
|---|
| 29 |
import weakref |
|---|
| 30 |
from RBFoundation.SubObs.Basic import SubjectList |
|---|
| 31 |
|
|---|
| 32 |
import AttributeMgr |
|---|
| 33 |
import BufferMgr |
|---|
| 34 |
import StateMgr |
|---|
| 35 |
|
|---|
| 36 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 37 |
#~ Definitions |
|---|
| 38 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 39 |
|
|---|
| 40 |
class _MatchAll(object): |
|---|
| 41 |
def __cmp__(self, other): return 0 # -1: less, 0: equal, 1: greater |
|---|
| 42 |
_MatchAll = _MatchAll() |
|---|
| 43 |
|
|---|
| 44 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 45 |
|
|---|
| 46 |
class SequenceBase(object): |
|---|
| 47 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 48 |
#~ Constants / Variables / Etc. |
|---|
| 49 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 50 |
|
|---|
| 51 |
SetupPhase = -2 |
|---|
| 52 |
ManagerPhase = -1 |
|---|
| 53 |
GeneralPhase = 0 |
|---|
| 54 |
PostPhase = 1 |
|---|
| 55 |
|
|---|
| 56 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 57 |
#~ Public Methods |
|---|
| 58 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 59 |
|
|---|
| 60 |
def __init__(self): |
|---|
| 61 |
self.Elements = [] |
|---|
| 62 |
self.OnAddElement = SubjectList() |
|---|
| 63 |
self.OnRemoveElement = SubjectList() |
|---|
| 64 |
self.OnBeginExecute = SubjectList() |
|---|
| 65 |
self.OnEndExecute = SubjectList() |
|---|
| 66 |
|
|---|
| 67 |
def __len__(self): |
|---|
| 68 |
return len(self.Elements) |
|---|
| 69 |
|
|---|
| 70 |
def AddSetupElement(self, Element, PriorityDelta=0): |
|---|
| 71 |
self._AddElement(Element, self.SetupPhase + PriorityDelta) |
|---|
| 72 |
|
|---|
| 73 |
def AddManagerElement(self, Element, PriorityDelta=0): |
|---|
| 74 |
self._AddElement(Element, self.ManagerPhase + PriorityDelta) |
|---|
| 75 |
|
|---|
| 76 |
def AddPostElement(self, Element, PriorityDelta=0): |
|---|
| 77 |
self._AddElement(Element, self.PostPhase + PriorityDelta) |
|---|
| 78 |
|
|---|
| 79 |
def AddElement(self, Element, priority=None): |
|---|
| 80 |
if priority is None: |
|---|
| 81 |
priority = self.GeneralPhase |
|---|
| 82 |
self._AddElement(Element, priority) |
|---|
| 83 |
|
|---|
| 84 |
def AddElements(self, elements): |
|---|
| 85 |
for each in elements: |
|---|
| 86 |
if isinstance(each, (tuple, list)): |
|---|
| 87 |
self.AddElement(*each) |
|---|
| 88 |
else: self.AddElement(each) |
|---|
| 89 |
|
|---|
| 90 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 91 |
|
|---|
| 92 |
def RemoveElement(self, Element): |
|---|
| 93 |
self.Elements[:] = [x for x in self.Elements if x[-1] != Element] |
|---|
| 94 |
self.OnRemoveElement.Update(Element) |
|---|
| 95 |
|
|---|
| 96 |
def GLExecute(self, context): |
|---|
| 97 |
self.OnBeginExecute.Update(context) |
|---|
| 98 |
for priority, elementfn, element in self.Elements: |
|---|
| 99 |
if getattr(element, 'Active', 1): |
|---|
| 100 |
elementfn(context) |
|---|
| 101 |
self.OnEndExecute.Update(context) |
|---|
| 102 |
|
|---|
| 103 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 104 |
#~ Protected Methods |
|---|
| 105 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 106 |
|
|---|
| 107 |
def _AddElement(self, Element, priority=0): |
|---|
| 108 |
if priority <= 0: |
|---|
| 109 |
idx = bisect.bisect_right(self.Elements, (priority, _MatchAll, _MatchAll)) |
|---|
| 110 |
else: |
|---|
| 111 |
idx = bisect.bisect_left(self.Elements, (priority, _MatchAll, _MatchAll)) |
|---|
| 112 |
|
|---|
| 113 |
if isinstance(Element, types.MethodType): |
|---|
| 114 |
ElementFn, Element = Element, Element.im_self |
|---|
| 115 |
elif callable(getattr(Element, 'GLExecute', None)): |
|---|
| 116 |
ElementFn = Element.GLExecute |
|---|
| 117 |
elif callable(getattr(Element, 'GLSelect', None)): |
|---|
| 118 |
ElementFn = Element.GLSelect |
|---|
| 119 |
else: |
|---|
| 120 |
raise ValueError, "Unsuppored element type %r: %r" % (type(Element), Element) |
|---|
| 121 |
|
|---|
| 122 |
self.Elements.insert(idx, (priority, ElementFn, Element)) |
|---|
| 123 |
|
|---|
| 124 |
try: ElementSequenceAdd = Element.SequenceAdd |
|---|
| 125 |
except AttributeError: pass |
|---|
| 126 |
else: ElementSequenceAdd(weakref.proxy(self)) |
|---|
| 127 |
|
|---|
| 128 |
self.OnAddElement.Update(Element) |
|---|
| 129 |
|
|---|
| 130 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 131 |
|
|---|
| 132 |
class Sequence(SequenceBase): |
|---|
| 133 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 134 |
#~ Public Methods |
|---|
| 135 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 136 |
|
|---|
| 137 |
def __init__(self, ManagerAttribute=0, ManageBuffer=0): |
|---|
| 138 |
SequenceBase.__init__(self) |
|---|
| 139 |
|
|---|
| 140 |
if ManagerAttribute: |
|---|
| 141 |
self.AttributeMgr = AttributeMgr.AttributeEffector() |
|---|
| 142 |
else: self.AttributeMgr = AttributeMgr.AttributeTracker() |
|---|
| 143 |
self.AddManagerElement(self.AttributeMgr) |
|---|
| 144 |
|
|---|
| 145 |
if ManageBuffer: |
|---|
| 146 |
self.BufferMgr = BufferMgr.BufferEffector() |
|---|
| 147 |
else: self.BufferMgr = BufferMgr.BufferTracker() |
|---|
| 148 |
self.AddManagerElement(self.BufferMgr) |
|---|
| 149 |
|
|---|
| 150 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 151 |
|
|---|
| 152 |
class RootSequence(Sequence): |
|---|
| 153 |
def __init__(self): |
|---|
| 154 |
Sequence.__init__(self, 1, 1) |
|---|
| 155 |
self.StateMgr = StateMgr.StateManager() |
|---|
| 156 |
self.ClientStateMgr = StateMgr.ClientStateManager() |
|---|
| 157 |
|
|---|
| 158 |
def GLExecute(self, context=None): |
|---|
| 159 |
self.Statistics = {} |
|---|
| 160 |
start = time.clock() |
|---|
| 161 |
self.Statistics['start'] = start |
|---|
| 162 |
self.StateMgr.Reset() |
|---|
| 163 |
self.ClientStateMgr.Reset() |
|---|
| 164 |
Sequence.GLExecute(self, self) |
|---|
| 165 |
stop = time.clock() |
|---|
| 166 |
self.Statistics['stop'] = stop |
|---|
| 167 |
self.Statistics['duration'] = stop - start |
|---|
| 168 |
self.Statistics['persecond'] = 1./max(1e-9, stop-start) |
|---|
| 169 |
|
|---|