PythonUtil.py 139 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391
  1. """Undocumented Module"""
  2. __all__ = ['unique', 'indent', 'nonRepeatingRandomList',
  3. 'writeFsmTree', 'StackTrace', 'traceFunctionCall', 'traceParentCall',
  4. 'printThisCall', 'tron', 'trace', 'troff', 'getClassLineage', 'pdir',
  5. '_pdir', '_is_variadic', '_has_keywordargs', '_varnames', '_getcode',
  6. 'Signature', 'doc', 'adjust', 'difference', 'intersection', 'union',
  7. 'sameElements', 'makeList', 'makeTuple', 'list2dict', 'invertDict',
  8. 'invertDictLossless', 'uniqueElements', 'disjoint', 'contains',
  9. 'replace', 'reduceAngle', 'fitSrcAngle2Dest', 'fitDestAngle2Src',
  10. 'closestDestAngle2', 'closestDestAngle', 'binaryRepr', 'profileFunc',
  11. 'profiled', 'startProfile', 'printProfile', 'getSetterName',
  12. 'getSetter', 'Functor', 'Stack', 'Queue', 'ParamObj',
  13. 'POD', 'bound', 'clamp', 'lerp', 'average', 'addListsByValue',
  14. 'boolEqual', 'lineupPos', 'formatElapsedSeconds', 'solveQuadratic',
  15. 'stackEntryInfo', 'lineInfo', 'callerInfo', 'lineTag',
  16. 'findPythonModule', 'describeException', 'mostDerivedLast',
  17. 'clampScalar', 'weightedChoice', 'randFloat', 'normalDistrib',
  18. 'weightedRand', 'randUint31', 'randInt32', 'randUint32',
  19. 'SerialNumGen', 'serialNum', 'uniqueName', 'Enum', 'Singleton',
  20. 'SingletonError', 'printListEnum', 'safeRepr',
  21. 'fastRepr', 'tagRepr', 'tagWithCaller', 'isDefaultValue', 'set_trace', 'pm',
  22. 'ScratchPad', 'Sync', 'RefCounter', 'itype', 'getNumberedTypedString',
  23. 'getNumberedTypedSortedString', 'getNumberedTypedSortedStringWithReferrers',
  24. 'getNumberedTypedSortedStringWithReferrersGen',
  25. 'printNumberedTyped', 'DelayedCall', 'DelayedFunctor',
  26. 'FrameDelayedCall', 'SubframeCall', 'ArgumentEater', 'ClassTree', 'getBase',
  27. 'HotkeyBreaker','logMethodCalls','GoldenRatio',
  28. 'GoldenRectangle', 'pivotScalar', 'rad90', 'rad180', 'rad270', 'rad360',
  29. 'nullGen', 'loopGen', 'makeFlywheelGen', 'flywheel', 'choice',
  30. 'printStack', 'printReverseStack', 'listToIndex2item', 'listToItem2index',
  31. 'pandaBreak','pandaTrace','formatTimeCompact','DestructiveScratchPad',
  32. 'deeptype','getProfileResultString','StdoutCapture','StdoutPassthrough',
  33. 'Averager', 'getRepository', 'formatTimeExact', 'startSuperLog', 'endSuperLog',
  34. 'typeName', 'safeTypeName', 'histogramDict', 'unescapeHtmlString', 'bpdb', ]
  35. import types
  36. import string
  37. import re
  38. import math
  39. import operator
  40. import inspect
  41. import os
  42. import sys
  43. import random
  44. import time
  45. import gc
  46. #if __debug__:
  47. import traceback
  48. import __builtin__
  49. from StringIO import StringIO
  50. import marshal
  51. import BpDb
  52. import unicodedata
  53. import bisect
  54. __report_indent = 3
  55. from direct.directutil import Verify
  56. from panda3d.core import ConfigVariableBool
  57. ScalarTypes = (types.FloatType, types.IntType, types.LongType)
  58. """
  59. # with one integer positional arg, this uses about 4/5 of the memory of the Functor class below
  60. def Functor(function, *args, **kArgs):
  61. argsCopy = args[:]
  62. def functor(*cArgs, **ckArgs):
  63. kArgs.update(ckArgs)
  64. return function(*(argsCopy + cArgs), **kArgs)
  65. return functor
  66. """
  67. class Functor:
  68. def __init__(self, function, *args, **kargs):
  69. assert callable(function), "function should be a callable obj"
  70. self._function = function
  71. self._args = args
  72. self._kargs = kargs
  73. if hasattr(self._function, '__name__'):
  74. self.__name__ = self._function.__name__
  75. else:
  76. self.__name__ = str(itype(self._function))
  77. if hasattr(self._function, '__doc__'):
  78. self.__doc__ = self._function.__doc__
  79. else:
  80. self.__doc__ = self.__name__
  81. def destroy(self):
  82. del self._function
  83. del self._args
  84. del self._kargs
  85. del self.__name__
  86. del self.__doc__
  87. def _do__call__(self, *args, **kargs):
  88. _kargs = self._kargs.copy()
  89. _kargs.update(kargs)
  90. return self._function(*(self._args + args), **_kargs)
  91. # this method is used in place of __call__ if we are recording creation stacks
  92. def _exceptionLoggedCreationStack__call__(self, *args, **kargs):
  93. try:
  94. return self._do__call__(*args, **kargs)
  95. except Exception, e:
  96. print '-->Functor creation stack (%s): %s' % (
  97. self.__name__, self.getCreationStackTraceCompactStr())
  98. raise
  99. __call__ = _do__call__
  100. def __repr__(self):
  101. s = 'Functor(%s' % self._function.__name__
  102. for arg in self._args:
  103. try:
  104. argStr = repr(arg)
  105. except:
  106. argStr = 'bad repr: %s' % arg.__class__
  107. s += ', %s' % argStr
  108. for karg, value in self._kargs.items():
  109. s += ', %s=%s' % (karg, repr(value))
  110. s += ')'
  111. return s
  112. class Stack:
  113. def __init__(self):
  114. self.__list = []
  115. def push(self, item):
  116. self.__list.append(item)
  117. def top(self):
  118. # return the item on the top of the stack without popping it off
  119. return self.__list[-1]
  120. def pop(self):
  121. return self.__list.pop()
  122. def clear(self):
  123. self.__list = []
  124. def isEmpty(self):
  125. return len(self.__list) == 0
  126. def __len__(self):
  127. return len(self.__list)
  128. class Queue:
  129. # FIFO queue
  130. # interface is intentionally identical to Stack (LIFO)
  131. def __init__(self):
  132. self.__list = []
  133. def push(self, item):
  134. self.__list.append(item)
  135. def top(self):
  136. # return the next item at the front of the queue without popping it off
  137. return self.__list[0]
  138. def front(self):
  139. return self.__list[0]
  140. def back(self):
  141. return self.__list[-1]
  142. def pop(self):
  143. return self.__list.pop(0)
  144. def clear(self):
  145. self.__list = []
  146. def isEmpty(self):
  147. return len(self.__list) == 0
  148. def __len__(self):
  149. return len(self.__list)
  150. if __debug__ and __name__ == '__main__':
  151. q = Queue()
  152. assert q.isEmpty()
  153. q.clear()
  154. assert q.isEmpty()
  155. q.push(10)
  156. assert not q.isEmpty()
  157. q.push(20)
  158. assert not q.isEmpty()
  159. assert len(q) == 2
  160. assert q.front() == 10
  161. assert q.back() == 20
  162. assert q.top() == 10
  163. assert q.top() == 10
  164. assert q.pop() == 10
  165. assert len(q) == 1
  166. assert not q.isEmpty()
  167. assert q.pop() == 20
  168. assert len(q) == 0
  169. assert q.isEmpty()
  170. def unique(L1, L2):
  171. """Return a list containing all items in 'L1' that are not in 'L2'"""
  172. L2 = dict([(k, None) for k in L2])
  173. return [item for item in L1 if item not in L2]
  174. def indent(stream, numIndents, str):
  175. """
  176. Write str to stream with numIndents in front of it
  177. """
  178. # To match emacs, instead of a tab character we will use 4 spaces
  179. stream.write(' ' * numIndents + str)
  180. def nonRepeatingRandomList(vals, max):
  181. random.seed(time.time())
  182. #first generate a set of random values
  183. valueList=range(max)
  184. finalVals=[]
  185. for i in range(vals):
  186. index=int(random.random()*len(valueList))
  187. finalVals.append(valueList[index])
  188. valueList.remove(valueList[index])
  189. return finalVals
  190. def writeFsmTree(instance, indent = 0):
  191. if hasattr(instance, 'parentFSM'):
  192. writeFsmTree(instance.parentFSM, indent-2)
  193. elif hasattr(instance, 'fsm'):
  194. name = ''
  195. if hasattr(instance.fsm, 'state'):
  196. name = instance.fsm.state.name
  197. print "%s: %s"%(instance.fsm.name, name)
  198. #if __debug__: #RAU accdg to Darren its's ok that StackTrace is not protected by __debug__
  199. # DCR: if somebody ends up using StackTrace in production, either
  200. # A) it will be OK because it hardly ever gets called, or
  201. # B) it will be easy to track it down (grep for StackTrace)
  202. class StackTrace:
  203. def __init__(self, label="", start=0, limit=None):
  204. """
  205. label is a string (or anything that be be a string)
  206. that is printed as part of the trace back.
  207. This is just to make it easier to tell what the
  208. stack trace is referring to.
  209. start is an integer number of stack frames back
  210. from the most recent. (This is automatically
  211. bumped up by one to skip the __init__ call
  212. to the StackTrace).
  213. limit is an integer number of stack frames
  214. to record (or None for unlimited).
  215. """
  216. self.label = label
  217. if limit is not None:
  218. self.trace = traceback.extract_stack(sys._getframe(1+start),
  219. limit=limit)
  220. else:
  221. self.trace = traceback.extract_stack(sys._getframe(1+start))
  222. def compact(self):
  223. r = ''
  224. comma = ','
  225. for filename, lineNum, funcName, text in self.trace:
  226. r += '%s.%s:%s%s' % (filename[:filename.rfind('.py')][filename.rfind('\\')+1:], funcName, lineNum, comma)
  227. if len(r):
  228. r = r[:-len(comma)]
  229. return r
  230. def reverseCompact(self):
  231. r = ''
  232. comma = ','
  233. for filename, lineNum, funcName, text in self.trace:
  234. r = '%s.%s:%s%s%s' % (filename[:filename.rfind('.py')][filename.rfind('\\')+1:], funcName, lineNum, comma, r)
  235. if len(r):
  236. r = r[:-len(comma)]
  237. return r
  238. def __str__(self):
  239. r = "Debug stack trace of %s (back %s frames):\n"%(
  240. self.label, len(self.trace),)
  241. for i in traceback.format_list(self.trace):
  242. r+=i
  243. r+="***** NOTE: This is not a crash. This is a debug stack trace. *****"
  244. return r
  245. def printStack():
  246. print StackTrace(start=1).compact()
  247. return True
  248. def printReverseStack():
  249. print StackTrace(start=1).reverseCompact()
  250. return True
  251. def printVerboseStack():
  252. print StackTrace(start=1)
  253. return True
  254. #-----------------------------------------------------------------------------
  255. def traceFunctionCall(frame):
  256. """
  257. return a string that shows the call frame with calling arguments.
  258. e.g.
  259. foo(x=234, y=135)
  260. """
  261. f = frame
  262. co = f.f_code
  263. dict = f.f_locals
  264. n = co.co_argcount
  265. if co.co_flags & 4: n = n+1
  266. if co.co_flags & 8: n = n+1
  267. r=''
  268. if 'self' in dict:
  269. r = '%s.'%(dict['self'].__class__.__name__,)
  270. r+="%s("%(f.f_code.co_name,)
  271. comma=0 # formatting, whether we should type a comma.
  272. for i in range(n):
  273. name = co.co_varnames[i]
  274. if name=='self':
  275. continue
  276. if comma:
  277. r+=', '
  278. else:
  279. # ok, we skipped the first one, the rest get commas:
  280. comma=1
  281. r+=name
  282. r+='='
  283. if name in dict:
  284. v=safeRepr(dict[name])
  285. if len(v)>2000:
  286. # r+="<too big for debug>"
  287. r += (v[:2000] + "...")
  288. else:
  289. r+=v
  290. else: r+="*** undefined ***"
  291. return r+')'
  292. def traceParentCall():
  293. return traceFunctionCall(sys._getframe(2))
  294. def printThisCall():
  295. print traceFunctionCall(sys._getframe(1))
  296. return 1 # to allow "assert printThisCall()"
  297. if __debug__:
  298. def lineage(obj, verbose=0, indent=0):
  299. """
  300. return instance or class name in as a multiline string.
  301. Usage: print lineage(foo)
  302. (Based on getClassLineage())
  303. """
  304. r=""
  305. if type(obj) == types.ListType:
  306. r+=(" "*indent)+"python list\n"
  307. elif type(obj) == types.DictionaryType:
  308. r+=(" "*indent)+"python dictionary\n"
  309. elif type(obj) == types.ModuleType:
  310. r+=(" "*indent)+str(obj)+"\n"
  311. elif type(obj) == types.InstanceType:
  312. r+=lineage(obj.__class__, verbose, indent)
  313. elif type(obj) == types.ClassType:
  314. r+=(" "*indent)
  315. if verbose:
  316. r+=obj.__module__+"."
  317. r+=obj.__name__+"\n"
  318. for c in obj.__bases__:
  319. r+=lineage(c, verbose, indent+2)
  320. return r
  321. def tron():
  322. sys.settrace(trace)
  323. def trace(frame, event, arg):
  324. if event == 'line':
  325. pass
  326. elif event == 'call':
  327. print traceFunctionCall(sys._getframe(1))
  328. elif event == 'return':
  329. print "returning"
  330. elif event == 'exception':
  331. print "exception"
  332. return trace
  333. def troff():
  334. sys.settrace(None)
  335. #-----------------------------------------------------------------------------
  336. def getClassLineage(obj):
  337. """
  338. print object inheritance list
  339. """
  340. if type(obj) == types.DictionaryType:
  341. # Just a dictionary, return dictionary
  342. return [obj]
  343. elif (type(obj) == types.InstanceType):
  344. # Instance, make a list with the instance and its class interitance
  345. return [obj] + getClassLineage(obj.__class__)
  346. elif ((type(obj) == types.ClassType) or
  347. (type(obj) == types.TypeType)):
  348. # Class or type, see what it derives from
  349. lineage = [obj]
  350. for c in obj.__bases__:
  351. lineage = lineage + getClassLineage(c)
  352. return lineage
  353. # New FFI objects are types that are not defined.
  354. # but they still act like classes
  355. elif hasattr(obj, '__class__'):
  356. # Instance, make a list with the instance and its class interitance
  357. return [obj] + getClassLineage(obj.__class__)
  358. else:
  359. # Not what I'm looking for
  360. return []
  361. def pdir(obj, str = None, width = None,
  362. fTruncate = 1, lineWidth = 75, wantPrivate = 0):
  363. # Remove redundant class entries
  364. uniqueLineage = []
  365. for l in getClassLineage(obj):
  366. if type(l) == types.ClassType:
  367. if l in uniqueLineage:
  368. break
  369. uniqueLineage.append(l)
  370. # Pretty print out directory info
  371. uniqueLineage.reverse()
  372. for obj in uniqueLineage:
  373. _pdir(obj, str, width, fTruncate, lineWidth, wantPrivate)
  374. print
  375. def _pdir(obj, str = None, width = None,
  376. fTruncate = 1, lineWidth = 75, wantPrivate = 0):
  377. """
  378. Print out a formatted list of members and methods of an instance or class
  379. """
  380. def printHeader(name):
  381. name = ' ' + name + ' '
  382. length = len(name)
  383. if length < 70:
  384. padBefore = int((70 - length)/2.0)
  385. padAfter = max(0, 70 - length - padBefore)
  386. header = '*' * padBefore + name + '*' * padAfter
  387. print header
  388. print
  389. def printInstanceHeader(i, printHeader = printHeader):
  390. printHeader(i.__class__.__name__ + ' INSTANCE INFO')
  391. def printClassHeader(c, printHeader = printHeader):
  392. printHeader(c.__name__ + ' CLASS INFO')
  393. def printDictionaryHeader(d, printHeader = printHeader):
  394. printHeader('DICTIONARY INFO')
  395. # Print Header
  396. if type(obj) == types.InstanceType:
  397. printInstanceHeader(obj)
  398. elif type(obj) == types.ClassType:
  399. printClassHeader(obj)
  400. elif type (obj) == types.DictionaryType:
  401. printDictionaryHeader(obj)
  402. # Get dict
  403. if type(obj) == types.DictionaryType:
  404. dict = obj
  405. # FFI objects are builtin types, they have no __dict__
  406. elif not hasattr(obj, '__dict__'):
  407. dict = {}
  408. else:
  409. dict = obj.__dict__
  410. # Adjust width
  411. if width:
  412. maxWidth = width
  413. else:
  414. maxWidth = 10
  415. keyWidth = 0
  416. aproposKeys = []
  417. privateKeys = []
  418. remainingKeys = []
  419. for key in dict.keys():
  420. if not width:
  421. keyWidth = len(key)
  422. if str:
  423. if re.search(str, key, re.I):
  424. aproposKeys.append(key)
  425. if (not width) and (keyWidth > maxWidth):
  426. maxWidth = keyWidth
  427. else:
  428. if key[:1] == '_':
  429. if wantPrivate:
  430. privateKeys.append(key)
  431. if (not width) and (keyWidth > maxWidth):
  432. maxWidth = keyWidth
  433. else:
  434. remainingKeys.append(key)
  435. if (not width) and (keyWidth > maxWidth):
  436. maxWidth = keyWidth
  437. # Sort appropriate keys
  438. if str:
  439. aproposKeys.sort()
  440. else:
  441. privateKeys.sort()
  442. remainingKeys.sort()
  443. # Print out results
  444. if wantPrivate:
  445. keys = aproposKeys + privateKeys + remainingKeys
  446. else:
  447. keys = aproposKeys + remainingKeys
  448. format = '%-' + repr(maxWidth) + 's'
  449. for key in keys:
  450. value = dict[key]
  451. if callable(value):
  452. strvalue = repr(Signature(value))
  453. else:
  454. strvalue = repr(value)
  455. if fTruncate:
  456. # Cut off line (keeping at least 1 char)
  457. strvalue = strvalue[:max(1, lineWidth - maxWidth)]
  458. print (format % key)[:maxWidth] + '\t' + strvalue
  459. # Magic numbers: These are the bit masks in func_code.co_flags that
  460. # reveal whether or not the function has a *arg or **kw argument.
  461. _POS_LIST = 4
  462. _KEY_DICT = 8
  463. def _is_variadic(function):
  464. return function.func_code.co_flags & _POS_LIST
  465. def _has_keywordargs(function):
  466. return function.func_code.co_flags & _KEY_DICT
  467. def _varnames(function):
  468. return function.func_code.co_varnames
  469. def _getcode(f):
  470. """
  471. _getcode(f)
  472. This function returns the name and function object of a callable
  473. object.
  474. """
  475. def method_get(f):
  476. return f.__name__, f.im_func
  477. def function_get(f):
  478. return f.__name__, f
  479. def instance_get(f):
  480. if hasattr(f, '__call__'):
  481. method = f.__call__
  482. if (type(method) == types.MethodType):
  483. func = method.im_func
  484. else:
  485. func = method
  486. return ("%s%s" % (f.__class__.__name__, '__call__'), func)
  487. else:
  488. s = ("Instance %s of class %s does not have a __call__ method" %
  489. (f, f.__class__.__name__))
  490. raise TypeError, s
  491. def class_get(f):
  492. if hasattr(f, '__init__'):
  493. return f.__name__, f.__init__.im_func
  494. else:
  495. return f.__name__, lambda: None
  496. codedict = { types.UnboundMethodType: method_get,
  497. types.MethodType: method_get,
  498. types.FunctionType: function_get,
  499. types.InstanceType: instance_get,
  500. types.ClassType: class_get,
  501. }
  502. try:
  503. return codedict[type(f)](f)
  504. except KeyError:
  505. if hasattr(f, '__call__'): # eg, built-in functions and methods
  506. # raise ValueError, "type %s not supported yet." % type(f)
  507. return f.__name__, None
  508. else:
  509. raise TypeError, ("object %s of type %s is not callable." %
  510. (f, type(f)))
  511. class Signature:
  512. def __init__(self, func):
  513. self.type = type(func)
  514. self.name, self.func = _getcode(func)
  515. def ordinary_args(self):
  516. n = self.func.func_code.co_argcount
  517. return _varnames(self.func)[0:n]
  518. def special_args(self):
  519. n = self.func.func_code.co_argcount
  520. x = {}
  521. #
  522. if _is_variadic(self.func):
  523. x['positional'] = _varnames(self.func)[n]
  524. if _has_keywordargs(self.func):
  525. x['keyword'] = _varnames(self.func)[n+1]
  526. elif _has_keywordargs(self.func):
  527. x['keyword'] = _varnames(self.func)[n]
  528. else:
  529. pass
  530. return x
  531. def full_arglist(self):
  532. base = list(self.ordinary_args())
  533. x = self.special_args()
  534. if 'positional' in x:
  535. base.append(x['positional'])
  536. if 'keyword' in x:
  537. base.append(x['keyword'])
  538. return base
  539. def defaults(self):
  540. defargs = self.func.func_defaults
  541. args = self.ordinary_args()
  542. mapping = {}
  543. if defargs is not None:
  544. for i in range(-1, -(len(defargs)+1), -1):
  545. mapping[args[i]] = defargs[i]
  546. else:
  547. pass
  548. return mapping
  549. def __repr__(self):
  550. if self.func:
  551. defaults = self.defaults()
  552. specials = self.special_args()
  553. l = []
  554. for arg in self.ordinary_args():
  555. if arg in defaults:
  556. l.append(arg + '=' + str(defaults[arg]))
  557. else:
  558. l.append(arg)
  559. if 'positional' in specials:
  560. l.append('*' + specials['positional'])
  561. if 'keyword' in specials:
  562. l.append('**' + specials['keyword'])
  563. return "%s(%s)" % (self.name, ', '.join(l))
  564. else:
  565. return "%s(?)" % self.name
  566. def doc(obj):
  567. if (isinstance(obj, types.MethodType)) or \
  568. (isinstance(obj, types.FunctionType)):
  569. print obj.__doc__
  570. def adjust(command = None, dim = 1, parent = None, **kw):
  571. """
  572. adjust(command = None, parent = None, **kw)
  573. Popup and entry scale to adjust a parameter
  574. Accepts any Slider keyword argument. Typical arguments include:
  575. command: The one argument command to execute
  576. min: The min value of the slider
  577. max: The max value of the slider
  578. resolution: The resolution of the slider
  579. text: The label on the slider
  580. These values can be accessed and/or changed after the fact
  581. >>> vg = adjust()
  582. >>> vg['min']
  583. 0.0
  584. >>> vg['min'] = 10.0
  585. >>> vg['min']
  586. 10.0
  587. """
  588. # Make sure we enable Tk
  589. from direct.tkwidgets import Valuator
  590. # Set command if specified
  591. if command:
  592. kw['command'] = lambda x: apply(command, x)
  593. if parent is None:
  594. kw['title'] = command.__name__
  595. kw['dim'] = dim
  596. # Create toplevel if needed
  597. if not parent:
  598. vg = apply(Valuator.ValuatorGroupPanel, (parent,), kw)
  599. else:
  600. vg = apply(Valuator.ValuatorGroup, (parent,), kw)
  601. vg.pack(expand = 1, fill = 'x')
  602. return vg
  603. def difference(a, b):
  604. """
  605. difference(list, list):
  606. """
  607. if not a: return b
  608. if not b: return a
  609. d = []
  610. for i in a:
  611. if (i not in b) and (i not in d):
  612. d.append(i)
  613. for i in b:
  614. if (i not in a) and (i not in d):
  615. d.append(i)
  616. return d
  617. def intersection(a, b):
  618. """
  619. intersection(list, list):
  620. """
  621. if not a: return []
  622. if not b: return []
  623. d = []
  624. for i in a:
  625. if (i in b) and (i not in d):
  626. d.append(i)
  627. for i in b:
  628. if (i in a) and (i not in d):
  629. d.append(i)
  630. return d
  631. def union(a, b):
  632. """
  633. union(list, list):
  634. """
  635. # Copy a
  636. c = a[:]
  637. for i in b:
  638. if (i not in c):
  639. c.append(i)
  640. return c
  641. def sameElements(a, b):
  642. if len(a) != len(b):
  643. return 0
  644. for elem in a:
  645. if elem not in b:
  646. return 0
  647. for elem in b:
  648. if elem not in a:
  649. return 0
  650. return 1
  651. def makeList(x):
  652. """returns x, converted to a list"""
  653. if type(x) is types.ListType:
  654. return x
  655. elif type(x) is types.TupleType:
  656. return list(x)
  657. else:
  658. return [x,]
  659. def makeTuple(x):
  660. """returns x, converted to a tuple"""
  661. if type(x) is types.ListType:
  662. return tuple(x)
  663. elif type(x) is types.TupleType:
  664. return x
  665. else:
  666. return (x,)
  667. def list2dict(L, value=None):
  668. """creates dict using elements of list, all assigned to same value"""
  669. return dict([(k, value) for k in L])
  670. def listToIndex2item(L):
  671. """converts list to dict of list index->list item"""
  672. d = {}
  673. for i, item in enumerate(L):
  674. d[i] = item
  675. return d
  676. assert listToIndex2item(['a','b']) == {0: 'a', 1: 'b',}
  677. def listToItem2index(L):
  678. """converts list to dict of list item->list index
  679. This is lossy if there are duplicate list items"""
  680. d = {}
  681. for i, item in enumerate(L):
  682. d[item] = i
  683. return d
  684. assert listToItem2index(['a','b']) == {'a': 0, 'b': 1,}
  685. def invertDict(D, lossy=False):
  686. """creates a dictionary by 'inverting' D; keys are placed in the new
  687. dictionary under their corresponding value in the old dictionary.
  688. It is an error if D contains any duplicate values.
  689. >>> old = {'key1':1, 'key2':2}
  690. >>> invertDict(old)
  691. {1: 'key1', 2: 'key2'}
  692. """
  693. n = {}
  694. for key, value in D.items():
  695. if not lossy and value in n:
  696. raise 'duplicate key in invertDict: %s' % value
  697. n[value] = key
  698. return n
  699. def invertDictLossless(D):
  700. """similar to invertDict, but values of new dict are lists of keys from
  701. old dict. No information is lost.
  702. >>> old = {'key1':1, 'key2':2, 'keyA':2}
  703. >>> invertDictLossless(old)
  704. {1: ['key1'], 2: ['key2', 'keyA']}
  705. """
  706. n = {}
  707. for key, value in D.items():
  708. n.setdefault(value, [])
  709. n[value].append(key)
  710. return n
  711. def uniqueElements(L):
  712. """are all elements of list unique?"""
  713. return len(L) == len(list2dict(L))
  714. def disjoint(L1, L2):
  715. """returns non-zero if L1 and L2 have no common elements"""
  716. used = dict([(k, None) for k in L1])
  717. for k in L2:
  718. if k in used:
  719. return 0
  720. return 1
  721. def contains(whole, sub):
  722. """
  723. Return 1 if whole contains sub, 0 otherwise
  724. """
  725. if (whole == sub):
  726. return 1
  727. for elem in sub:
  728. # The first item you find not in whole, return 0
  729. if elem not in whole:
  730. return 0
  731. # If you got here, whole must contain sub
  732. return 1
  733. def replace(list, old, new, all=0):
  734. """
  735. replace 'old' with 'new' in 'list'
  736. if all == 0, replace first occurrence
  737. otherwise replace all occurrences
  738. returns the number of items replaced
  739. """
  740. if old not in list:
  741. return 0
  742. if not all:
  743. i = list.index(old)
  744. list[i] = new
  745. return 1
  746. else:
  747. numReplaced = 0
  748. for i in xrange(len(list)):
  749. if list[i] == old:
  750. numReplaced += 1
  751. list[i] = new
  752. return numReplaced
  753. rad90 = math.pi / 2.
  754. rad180 = math.pi
  755. rad270 = 1.5 * math.pi
  756. rad360 = 2. * math.pi
  757. def reduceAngle(deg):
  758. """
  759. Reduces an angle (in degrees) to a value in [-180..180)
  760. """
  761. return (((deg + 180.) % 360.) - 180.)
  762. def fitSrcAngle2Dest(src, dest):
  763. """
  764. given a src and destination angle, returns an equivalent src angle
  765. that is within [-180..180) of dest
  766. examples:
  767. fitSrcAngle2Dest(30, 60) == 30
  768. fitSrcAngle2Dest(60, 30) == 60
  769. fitSrcAngle2Dest(0, 180) == 0
  770. fitSrcAngle2Dest(-1, 180) == 359
  771. fitSrcAngle2Dest(-180, 180) == 180
  772. """
  773. return dest + reduceAngle(src - dest)
  774. def fitDestAngle2Src(src, dest):
  775. """
  776. given a src and destination angle, returns an equivalent dest angle
  777. that is within [-180..180) of src
  778. examples:
  779. fitDestAngle2Src(30, 60) == 60
  780. fitDestAngle2Src(60, 30) == 30
  781. fitDestAngle2Src(0, 180) == -180
  782. fitDestAngle2Src(1, 180) == 180
  783. """
  784. return src + (reduceAngle(dest - src))
  785. def closestDestAngle2(src, dest):
  786. # The function above didn't seem to do what I wanted. So I hacked
  787. # this one together. I can't really say I understand it. It's more
  788. # from impirical observation... GRW
  789. diff = src - dest
  790. if diff > 180:
  791. # if the difference is greater that 180 it's shorter to go the other way
  792. return dest - 360
  793. elif diff < -180:
  794. # or perhaps the OTHER other way...
  795. return dest + 360
  796. else:
  797. # otherwise just go to the original destination
  798. return dest
  799. def closestDestAngle(src, dest):
  800. # The function above didn't seem to do what I wanted. So I hacked
  801. # this one together. I can't really say I understand it. It's more
  802. # from impirical observation... GRW
  803. diff = src - dest
  804. if diff > 180:
  805. # if the difference is greater that 180 it's shorter to go the other way
  806. return src - (diff - 360)
  807. elif diff < -180:
  808. # or perhaps the OTHER other way...
  809. return src - (360 + diff)
  810. else:
  811. # otherwise just go to the original destination
  812. return dest
  813. def binaryRepr(number, max_length = 32):
  814. # This will only work reliably for relatively small numbers.
  815. # Increase the value of max_length if you think you're going
  816. # to use long integers
  817. assert number < 2L << max_length
  818. shifts = map (operator.rshift, max_length * [number], \
  819. range (max_length - 1, -1, -1))
  820. digits = map (operator.mod, shifts, max_length * [2])
  821. if not digits.count (1): return 0
  822. digits = digits [digits.index (1):]
  823. return ''.join([repr(digit) for digit in digits])
  824. class StdoutCapture:
  825. # redirects stdout to a string
  826. def __init__(self):
  827. self._oldStdout = sys.stdout
  828. sys.stdout = self
  829. self._string = ''
  830. def destroy(self):
  831. sys.stdout = self._oldStdout
  832. del self._oldStdout
  833. def getString(self):
  834. return self._string
  835. # internal
  836. def write(self, string):
  837. self._string = ''.join([self._string, string])
  838. class StdoutPassthrough(StdoutCapture):
  839. # like StdoutCapture but also allows output to go through to the OS as normal
  840. # internal
  841. def write(self, string):
  842. self._string = ''.join([self._string, string])
  843. self._oldStdout.write(string)
  844. # constant profile defaults
  845. PyUtilProfileDefaultFilename = 'profiledata'
  846. PyUtilProfileDefaultLines = 80
  847. PyUtilProfileDefaultSorts = ['cumulative', 'time', 'calls']
  848. _ProfileResultStr = ''
  849. def getProfileResultString():
  850. # if you called profile with 'log' not set to True,
  851. # you can call this function to get the results as
  852. # a string
  853. global _ProfileResultStr
  854. return _ProfileResultStr
  855. def profileFunc(callback, name, terse, log=True):
  856. global _ProfileResultStr
  857. if 'globalProfileFunc' in __builtin__.__dict__:
  858. # rats. Python profiler is not re-entrant...
  859. base.notify.warning(
  860. 'PythonUtil.profileStart(%s): aborted, already profiling %s'
  861. #'\nStack Trace:\n%s'
  862. % (name, __builtin__.globalProfileFunc,
  863. #StackTrace()
  864. ))
  865. return
  866. __builtin__.globalProfileFunc = callback
  867. __builtin__.globalProfileResult = [None]
  868. prefix = '***** START PROFILE: %s *****' % name
  869. if log:
  870. print prefix
  871. startProfile(cmd='globalProfileResult[0]=globalProfileFunc()', callInfo=(not terse), silent=not log)
  872. suffix = '***** END PROFILE: %s *****' % name
  873. if log:
  874. print suffix
  875. else:
  876. _ProfileResultStr = '%s\n%s\n%s' % (prefix, _ProfileResultStr, suffix)
  877. result = globalProfileResult[0]
  878. del __builtin__.__dict__['globalProfileFunc']
  879. del __builtin__.__dict__['globalProfileResult']
  880. return result
  881. def profiled(category=None, terse=False):
  882. """ decorator for profiling functions
  883. turn categories on and off via "want-profile-categoryName 1"
  884. e.g.
  885. @profiled('particles')
  886. def loadParticles():
  887. ...
  888. want-profile-particles 1
  889. """
  890. assert type(category) in (types.StringType, types.NoneType), "must provide a category name for @profiled"
  891. # allow profiling in published versions
  892. """
  893. try:
  894. null = not __dev__
  895. except:
  896. null = not __debug__
  897. if null:
  898. # if we're not in __dev__, just return the function itself. This
  899. # results in zero runtime overhead, since decorators are evaluated
  900. # at module-load.
  901. def nullDecorator(f):
  902. return f
  903. return nullDecorator
  904. """
  905. def profileDecorator(f):
  906. def _profiled(*args, **kArgs):
  907. name = '(%s) %s from %s' % (category, f.func_name, f.__module__)
  908. # showbase might not be loaded yet, so don't use
  909. # base.config. Instead, query the ConfigVariableBool.
  910. if (category is None) or ConfigVariableBool('want-profile-%s' % category, 0).getValue():
  911. return profileFunc(Functor(f, *args, **kArgs), name, terse)
  912. else:
  913. return f(*args, **kArgs)
  914. _profiled.__doc__ = f.__doc__
  915. return _profiled
  916. return profileDecorator
  917. # intercept profile-related file operations to avoid disk access
  918. movedOpenFuncs = []
  919. movedDumpFuncs = []
  920. movedLoadFuncs = []
  921. profileFilenames = set()
  922. profileFilenameList = Stack()
  923. profileFilename2file = {}
  924. profileFilename2marshalData = {}
  925. def _profileOpen(filename, *args, **kArgs):
  926. # this is a replacement for the file open() builtin function
  927. # for use during profiling, to intercept the file open
  928. # operation used by the Python profiler and profile stats
  929. # systems
  930. if filename in profileFilenames:
  931. # if this is a file related to profiling, create an
  932. # in-RAM file object
  933. if filename not in profileFilename2file:
  934. file = StringIO()
  935. file._profFilename = filename
  936. profileFilename2file[filename] = file
  937. else:
  938. file = profileFilename2file[filename]
  939. else:
  940. file = movedOpenFuncs[-1](filename, *args, **kArgs)
  941. return file
  942. def _profileMarshalDump(data, file):
  943. # marshal.dump doesn't work with StringIO objects
  944. # simulate it
  945. if isinstance(file, StringIO) and hasattr(file, '_profFilename'):
  946. if file._profFilename in profileFilenames:
  947. profileFilename2marshalData[file._profFilename] = data
  948. return None
  949. return movedDumpFuncs[-1](data, file)
  950. def _profileMarshalLoad(file):
  951. # marshal.load doesn't work with StringIO objects
  952. # simulate it
  953. if isinstance(file, StringIO) and hasattr(file, '_profFilename'):
  954. if file._profFilename in profileFilenames:
  955. return profileFilename2marshalData[file._profFilename]
  956. return movedLoadFuncs[-1](file)
  957. def _installProfileCustomFuncs(filename):
  958. assert filename not in profileFilenames
  959. profileFilenames.add(filename)
  960. profileFilenameList.push(filename)
  961. movedOpenFuncs.append(__builtin__.open)
  962. __builtin__.open = _profileOpen
  963. movedDumpFuncs.append(marshal.dump)
  964. marshal.dump = _profileMarshalDump
  965. movedLoadFuncs.append(marshal.load)
  966. marshal.load = _profileMarshalLoad
  967. def _getProfileResultFileInfo(filename):
  968. return (profileFilename2file.get(filename, None),
  969. profileFilename2marshalData.get(filename, None))
  970. def _setProfileResultsFileInfo(filename, info):
  971. f, m = info
  972. if f:
  973. profileFilename2file[filename] = f
  974. if m:
  975. profileFilename2marshalData[filename] = m
  976. def _clearProfileResultFileInfo(filename):
  977. profileFilename2file.pop(filename, None)
  978. profileFilename2marshalData.pop(filename, None)
  979. def _removeProfileCustomFuncs(filename):
  980. assert profileFilenameList.top() == filename
  981. marshal.load = movedLoadFuncs.pop()
  982. marshal.dump = movedDumpFuncs.pop()
  983. __builtin__.open = movedOpenFuncs.pop()
  984. profileFilenames.remove(filename)
  985. profileFilenameList.pop()
  986. profileFilename2file.pop(filename, None)
  987. # don't let marshalled data pile up
  988. profileFilename2marshalData.pop(filename, None)
  989. # call this from the prompt, and break back out to the prompt
  990. # to stop profiling
  991. #
  992. # OR to do inline profiling, you must make a globally-visible
  993. # function to be profiled, i.e. to profile 'self.load()', do
  994. # something like this:
  995. #
  996. # def func(self=self):
  997. # self.load()
  998. # import __builtin__
  999. # __builtin__.func = func
  1000. # PythonUtil.startProfile(cmd='func()', filename='profileData')
  1001. # del __builtin__.func
  1002. #
  1003. def _profileWithoutGarbageLeak(cmd, filename):
  1004. # The profile module isn't necessarily installed on every Python
  1005. # installation, so we import it here, instead of in the module
  1006. # scope.
  1007. import profile
  1008. # this is necessary because the profile module creates a memory leak
  1009. Profile = profile.Profile
  1010. statement = cmd
  1011. sort = -1
  1012. retVal = None
  1013. #### COPIED FROM profile.run ####
  1014. prof = Profile()
  1015. try:
  1016. prof = prof.run(statement)
  1017. except SystemExit:
  1018. pass
  1019. if filename is not None:
  1020. prof.dump_stats(filename)
  1021. else:
  1022. #return prof.print_stats(sort) #DCR
  1023. retVal = prof.print_stats(sort) #DCR
  1024. #################################
  1025. # eliminate the garbage leak
  1026. del prof.dispatcher
  1027. return retVal
  1028. def startProfile(filename=PyUtilProfileDefaultFilename,
  1029. lines=PyUtilProfileDefaultLines,
  1030. sorts=PyUtilProfileDefaultSorts,
  1031. silent=0,
  1032. callInfo=1,
  1033. useDisk=False,
  1034. cmd='run()'):
  1035. # uniquify the filename to allow multiple processes to profile simultaneously
  1036. filename = '%s.%s%s' % (filename, randUint31(), randUint31())
  1037. if not useDisk:
  1038. # use a RAM file
  1039. _installProfileCustomFuncs(filename)
  1040. _profileWithoutGarbageLeak(cmd, filename)
  1041. if silent:
  1042. extractProfile(filename, lines, sorts, callInfo)
  1043. else:
  1044. printProfile(filename, lines, sorts, callInfo)
  1045. if not useDisk:
  1046. # discard the RAM file
  1047. _removeProfileCustomFuncs(filename)
  1048. else:
  1049. os.remove(filename)
  1050. # call these to see the results again, as a string or in the log
  1051. def printProfile(filename=PyUtilProfileDefaultFilename,
  1052. lines=PyUtilProfileDefaultLines,
  1053. sorts=PyUtilProfileDefaultSorts,
  1054. callInfo=1):
  1055. import pstats
  1056. s = pstats.Stats(filename)
  1057. s.strip_dirs()
  1058. for sort in sorts:
  1059. s.sort_stats(sort)
  1060. s.print_stats(lines)
  1061. if callInfo:
  1062. s.print_callees(lines)
  1063. s.print_callers(lines)
  1064. # same args as printProfile
  1065. def extractProfile(*args, **kArgs):
  1066. global _ProfileResultStr
  1067. # capture print output
  1068. sc = StdoutCapture()
  1069. # print the profile output, redirected to the result string
  1070. printProfile(*args, **kArgs)
  1071. # make a copy of the print output
  1072. _ProfileResultStr = sc.getString()
  1073. # restore stdout to what it was before
  1074. sc.destroy()
  1075. def getSetterName(valueName, prefix='set'):
  1076. # getSetterName('color') -> 'setColor'
  1077. # getSetterName('color', 'get') -> 'getColor'
  1078. return '%s%s%s' % (prefix, valueName[0].upper(), valueName[1:])
  1079. def getSetter(targetObj, valueName, prefix='set'):
  1080. # getSetter(smiley, 'pos') -> smiley.setPos
  1081. return getattr(targetObj, getSetterName(valueName, prefix))
  1082. def mostDerivedLast(classList):
  1083. """pass in list of classes. sorts list in-place, with derived classes
  1084. appearing after their bases"""
  1085. class ClassSortKey(object):
  1086. __slots__ = 'classobj',
  1087. def __init__(self, classobj):
  1088. self.classobj = classobj
  1089. def __lt__(self, other):
  1090. return issubclass(other.classobj, self.classobj)
  1091. classList.sort(key=ClassSortKey)
  1092. """
  1093. ParamObj/ParamSet
  1094. =================
  1095. These two classes support you in the definition of a formal set of
  1096. parameters for an object type. The parameters may be safely queried/set on
  1097. an object instance at any time, and the object will react to newly-set
  1098. values immediately.
  1099. ParamSet & ParamObj also provide a mechanism for atomically setting
  1100. multiple parameter values before allowing the object to react to any of the
  1101. new values--useful when two or more parameters are interdependent and there
  1102. is risk of setting an illegal combination in the process of applying a new
  1103. set of values.
  1104. To make use of these classes, derive your object from ParamObj. Then define
  1105. a 'ParamSet' subclass that derives from the parent class' 'ParamSet' class,
  1106. and define the object's parameters within its ParamSet class. (see examples
  1107. below)
  1108. The ParamObj base class provides 'get' and 'set' functions for each
  1109. parameter if they are not defined. These default implementations
  1110. respectively set the parameter value directly on the object, and expect the
  1111. value to be available in that location for retrieval.
  1112. Classes that derive from ParamObj can optionally declare a 'get' and 'set'
  1113. function for each parameter. The setter should simply store the value in a
  1114. location where the getter can find it; it should not do any further
  1115. processing based on the new parameter value. Further processing should be
  1116. implemented in an 'apply' function. The applier function is optional, and
  1117. there is no default implementation.
  1118. NOTE: the previous value of a parameter is available inside an apply
  1119. function as 'self.getPriorValue()'
  1120. The ParamSet class declaration lists the parameters and defines a default
  1121. value for each. ParamSet instances represent a complete set of parameter
  1122. values. A ParamSet instance created with no constructor arguments will
  1123. contain the default values for each parameter. The defaults may be
  1124. overriden by passing keyword arguments to the ParamSet's constructor. If a
  1125. ParamObj instance is passed to the constructor, the ParamSet will extract
  1126. the object's current parameter values.
  1127. ParamSet.applyTo(obj) sets all of its parameter values on 'obj'.
  1128. SETTERS AND APPLIERS
  1129. ====================
  1130. Under normal conditions, a call to a setter function, i.e.
  1131. cam.setFov(90)
  1132. will actually result in the following calls being made:
  1133. cam.setFov(90)
  1134. cam.applyFov()
  1135. Calls to several setter functions, i.e.
  1136. cam.setFov(90)
  1137. cam.setViewType('cutscene')
  1138. will result in this call sequence:
  1139. cam.setFov(90)
  1140. cam.applyFov()
  1141. cam.setViewType('cutscene')
  1142. cam.applyViewType()
  1143. Suppose that you desire the view type to already be set to 'cutscene' at
  1144. the time when applyFov() is called. You could reverse the order of the set
  1145. calls, but suppose that you also want the fov to be set properly at the
  1146. time when applyViewType() is called.
  1147. In this case, you can 'lock' the params, i.e.
  1148. cam.lockParams()
  1149. cam.setFov(90)
  1150. cam.setViewType('cutscene')
  1151. cam.unlockParams()
  1152. This will result in the following call sequence:
  1153. cam.setFov(90)
  1154. cam.setViewType('cutscene')
  1155. cam.applyFov()
  1156. cam.applyViewType()
  1157. NOTE: Currently the order of the apply calls following an unlock is not
  1158. guaranteed.
  1159. EXAMPLE CLASSES
  1160. ===============
  1161. Here is an example of a class that uses ParamSet/ParamObj to manage its
  1162. parameters:
  1163. class Camera(ParamObj):
  1164. class ParamSet(ParamObj.ParamSet):
  1165. Params = {
  1166. 'viewType': 'normal',
  1167. 'fov': 60,
  1168. }
  1169. ...
  1170. def getViewType(self):
  1171. return self.viewType
  1172. def setViewType(self, viewType):
  1173. self.viewType = viewType
  1174. def applyViewType(self):
  1175. if self.viewType == 'normal':
  1176. ...
  1177. def getFov(self):
  1178. return self.fov
  1179. def setFov(self, fov):
  1180. self.fov = fov
  1181. def applyFov(self):
  1182. base.camera.setFov(self.fov)
  1183. ...
  1184. EXAMPLE USAGE
  1185. =============
  1186. cam = Camera()
  1187. ...
  1188. # set up for the cutscene
  1189. savedSettings = cam.ParamSet(cam)
  1190. cam.setViewType('closeup')
  1191. cam.setFov(90)
  1192. ...
  1193. # cutscene is over, set the camera back
  1194. savedSettings.applyTo(cam)
  1195. del savedSettings
  1196. """
  1197. class ParamObj:
  1198. # abstract base for classes that want to support a formal parameter
  1199. # set whose values may be queried, changed, 'bulk' changed (defer reaction
  1200. # to changes until multiple changes have been performed), and
  1201. # extracted/stored/applied all at once (see documentation above)
  1202. # ParamSet subclass: container of parameter values. Derived class must
  1203. # derive a new ParamSet class if they wish to define new params. See
  1204. # documentation above.
  1205. class ParamSet:
  1206. Params = {
  1207. # base class does not define any parameters, but they would
  1208. # appear here as 'name': defaultValue,
  1209. #
  1210. # WARNING: default values of mutable types that do not copy by
  1211. # value (dicts, lists etc.) will be shared by all class instances
  1212. # if default value is callable, it will be called to get actual
  1213. # default value
  1214. #
  1215. # for example:
  1216. #
  1217. # class MapArea(ParamObj):
  1218. # class ParamSet(ParamObj.ParamSet):
  1219. # Params = {
  1220. # 'spawnIndices': Functor(list, [1,5,22]),
  1221. # }
  1222. #
  1223. }
  1224. def __init__(self, *args, **kwArgs):
  1225. self.__class__._compileDefaultParams()
  1226. if len(args) == 1 and len(kwArgs) == 0:
  1227. # extract our params from an existing ParamObj instance
  1228. obj = args[0]
  1229. self.paramVals = {}
  1230. for param in self.getParams():
  1231. self.paramVals[param] = getSetter(obj, param, 'get')()
  1232. else:
  1233. assert len(args) == 0
  1234. if __debug__:
  1235. for arg in kwArgs.keys():
  1236. assert arg in self.getParams()
  1237. self.paramVals = dict(kwArgs)
  1238. def getValue(self, param):
  1239. if param in self.paramVals:
  1240. return self.paramVals[param]
  1241. return self._Params[param]
  1242. def applyTo(self, obj):
  1243. # Apply our entire set of params to a ParamObj
  1244. obj.lockParams()
  1245. for param in self.getParams():
  1246. getSetter(obj, param)(self.getValue(param))
  1247. obj.unlockParams()
  1248. def extractFrom(self, obj):
  1249. # Extract our entire set of params from a ParamObj
  1250. obj.lockParams()
  1251. for param in self.getParams():
  1252. self.paramVals[param] = getSetter(obj, param, 'get')()
  1253. obj.unlockParams()
  1254. @classmethod
  1255. def getParams(cls):
  1256. # returns safely-mutable list of param names
  1257. cls._compileDefaultParams()
  1258. return cls._Params.keys()
  1259. @classmethod
  1260. def getDefaultValue(cls, param):
  1261. cls._compileDefaultParams()
  1262. dv = cls._Params[param]
  1263. if hasattr(dv, '__call__'):
  1264. dv = dv()
  1265. return dv
  1266. @classmethod
  1267. def _compileDefaultParams(cls):
  1268. if '_Params' in cls.__dict__:
  1269. # we've already compiled the defaults for this class
  1270. return
  1271. bases = list(cls.__bases__)
  1272. if object in bases:
  1273. bases.remove(object)
  1274. # bring less-derived classes to the front
  1275. mostDerivedLast(bases)
  1276. cls._Params = {}
  1277. for c in (bases + [cls]):
  1278. # make sure this base has its dict of param defaults
  1279. c._compileDefaultParams()
  1280. if 'Params' in c.__dict__:
  1281. # apply this class' default param values to our dict
  1282. cls._Params.update(c.Params)
  1283. def __repr__(self):
  1284. argStr = ''
  1285. for param in self.getParams():
  1286. argStr += '%s=%s,' % (param,
  1287. repr(self.getValue(param)))
  1288. return '%s.%s(%s)' % (
  1289. self.__class__.__module__, self.__class__.__name__, argStr)
  1290. # END PARAMSET SUBCLASS
  1291. def __init__(self, *args, **kwArgs):
  1292. assert issubclass(self.ParamSet, ParamObj.ParamSet)
  1293. # If you pass in a ParamSet obj, its values will be applied to this
  1294. # object in the constructor.
  1295. params = None
  1296. if len(args) == 1 and len(kwArgs) == 0:
  1297. # if there's one argument, assume that it's a ParamSet
  1298. params = args[0]
  1299. elif len(kwArgs) > 0:
  1300. assert len(args) == 0
  1301. # if we've got keyword arguments, make a ParamSet out of them
  1302. params = self.ParamSet(**kwArgs)
  1303. self._paramLockRefCount = 0
  1304. # these hold the current value of parameters while they are being set to
  1305. # a new value, to support getPriorValue()
  1306. self._curParamStack = []
  1307. self._priorValuesStack = []
  1308. # insert stub funcs for param setters, to handle locked params
  1309. for param in self.ParamSet.getParams():
  1310. # set the default value on the object
  1311. setattr(self, param, self.ParamSet.getDefaultValue(param))
  1312. setterName = getSetterName(param)
  1313. getterName = getSetterName(param, 'get')
  1314. # is there a setter defined?
  1315. if not hasattr(self, setterName):
  1316. # no; provide the default
  1317. def defaultSetter(self, value, param=param):
  1318. #print '%s=%s for %s' % (param, value, id(self))
  1319. setattr(self, param, value)
  1320. self.__class__.__dict__[setterName] = defaultSetter
  1321. # is there a getter defined?
  1322. if not hasattr(self, getterName):
  1323. # no; provide the default. If there is no value set, return
  1324. # the default
  1325. def defaultGetter(self, param=param,
  1326. default=self.ParamSet.getDefaultValue(param)):
  1327. return getattr(self, param, default)
  1328. self.__class__.__dict__[getterName] = defaultGetter
  1329. # have we already installed a setter stub?
  1330. origSetterName = '%s_ORIG' % (setterName,)
  1331. if not hasattr(self, origSetterName):
  1332. # move the original setter aside
  1333. origSetterFunc = getattr(self.__class__, setterName)
  1334. setattr(self.__class__, origSetterName, origSetterFunc)
  1335. """
  1336. # if the setter is a direct member of this instance, move the setter
  1337. # aside
  1338. if setterName in self.__dict__:
  1339. self.__dict__[setterName + '_MOVED'] = self.__dict__[setterName]
  1340. setterFunc = self.__dict__[setterName]
  1341. """
  1342. # install a setter stub that will a) call the real setter and
  1343. # then the applier, or b) call the setter and queue the
  1344. # applier, depending on whether our params are locked
  1345. """
  1346. setattr(self, setterName, types.MethodType(
  1347. Functor(setterStub, param, setterFunc), self, self.__class__))
  1348. """
  1349. def setterStub(self, value, param=param, origSetterName=origSetterName):
  1350. # should we apply the value now or should we wait?
  1351. # if this obj's params are locked, we track which values have
  1352. # been set, and on unlock, we'll call the applyers for those
  1353. # values
  1354. if self._paramLockRefCount > 0:
  1355. priorValues = self._priorValuesStack[-1]
  1356. if param not in priorValues:
  1357. try:
  1358. priorValue = getSetter(self, param, 'get')()
  1359. except:
  1360. priorValue = None
  1361. priorValues[param] = priorValue
  1362. self._paramsSet[param] = None
  1363. getattr(self, origSetterName)(value)
  1364. else:
  1365. # prepare for call to getPriorValue
  1366. try:
  1367. priorValue = getSetter(self, param, 'get')()
  1368. except:
  1369. priorValue = None
  1370. self._priorValuesStack.append({
  1371. param: priorValue,
  1372. })
  1373. getattr(self, origSetterName)(value)
  1374. # call the applier, if there is one
  1375. applier = getattr(self, getSetterName(param, 'apply'), None)
  1376. if applier is not None:
  1377. self._curParamStack.append(param)
  1378. applier()
  1379. self._curParamStack.pop()
  1380. self._priorValuesStack.pop()
  1381. if hasattr(self, 'handleParamChange'):
  1382. self.handleParamChange((param,))
  1383. setattr(self.__class__, setterName, setterStub)
  1384. if params is not None:
  1385. params.applyTo(self)
  1386. def destroy(self):
  1387. """
  1388. for param in self.ParamSet.getParams():
  1389. setterName = getSetterName(param)
  1390. self.__dict__[setterName].destroy()
  1391. del self.__dict__[setterName]
  1392. """
  1393. pass
  1394. def setDefaultParams(self):
  1395. # set all the default parameters on ourself
  1396. self.ParamSet().applyTo(self)
  1397. def getCurrentParams(self):
  1398. params = self.ParamSet()
  1399. params.extractFrom(self)
  1400. return params
  1401. def lockParams(self):
  1402. self._paramLockRefCount += 1
  1403. if self._paramLockRefCount == 1:
  1404. self._handleLockParams()
  1405. def unlockParams(self):
  1406. if self._paramLockRefCount > 0:
  1407. self._paramLockRefCount -= 1
  1408. if self._paramLockRefCount == 0:
  1409. self._handleUnlockParams()
  1410. def _handleLockParams(self):
  1411. # this will store the names of the parameters that are modified
  1412. self._paramsSet = {}
  1413. # this will store the values of modified params (from prior to
  1414. # the lock).
  1415. self._priorValuesStack.append({})
  1416. def _handleUnlockParams(self):
  1417. for param in self._paramsSet:
  1418. # call the applier, if there is one
  1419. applier = getattr(self, getSetterName(param, 'apply'), None)
  1420. if applier is not None:
  1421. self._curParamStack.append(param)
  1422. applier()
  1423. self._curParamStack.pop()
  1424. self._priorValuesStack.pop()
  1425. if hasattr(self, 'handleParamChange'):
  1426. self.handleParamChange(tuple(self._paramsSet.keys()))
  1427. del self._paramsSet
  1428. def paramsLocked(self):
  1429. return self._paramLockRefCount > 0
  1430. def getPriorValue(self):
  1431. # call this within an apply function to find out what the prior value
  1432. # of the param was
  1433. return self._priorValuesStack[-1][self._curParamStack[-1]]
  1434. def __repr__(self):
  1435. argStr = ''
  1436. for param in self.ParamSet.getParams():
  1437. try:
  1438. value = getSetter(self, param, 'get')()
  1439. except:
  1440. value = '<unknown>'
  1441. argStr += '%s=%s,' % (param, repr(value))
  1442. return '%s(%s)' % (self.__class__.__name__, argStr)
  1443. if __debug__ and __name__ == '__main__':
  1444. class ParamObjTest(ParamObj):
  1445. class ParamSet(ParamObj.ParamSet):
  1446. Params = {
  1447. 'num': 0,
  1448. }
  1449. def applyNum(self):
  1450. self.priorValue = self.getPriorValue()
  1451. pto = ParamObjTest()
  1452. assert pto.getNum() == 0
  1453. pto.setNum(1)
  1454. assert pto.priorValue == 0
  1455. assert pto.getNum() == 1
  1456. pto.lockParams()
  1457. pto.setNum(2)
  1458. # make sure applyNum is not called until we call unlockParams
  1459. assert pto.priorValue == 0
  1460. assert pto.getNum() == 2
  1461. pto.unlockParams()
  1462. assert pto.priorValue == 1
  1463. assert pto.getNum() == 2
  1464. """
  1465. POD (Plain Ol' Data)
  1466. Like ParamObj/ParamSet, but without lock/unlock/getPriorValue and without
  1467. appliers. Similar to a C++ struct, but with auto-generated setters and
  1468. getters.
  1469. Use POD when you want the generated getters and setters of ParamObj, but
  1470. efficiency is a concern and you don't need the bells and whistles provided
  1471. by ParamObj.
  1472. POD.__init__ *MUST* be called. You should NOT define your own data getters
  1473. and setters. Data values may be read, set, and modified directly. You will
  1474. see no errors if you define your own getters/setters, but there is no
  1475. guarantee that they will be called--and they will certainly be bypassed by
  1476. POD internally.
  1477. EXAMPLE CLASSES
  1478. ===============
  1479. Here is an example of a class heirarchy that uses POD to manage its data:
  1480. class Enemy(POD):
  1481. DataSet = {
  1482. 'faction': 'navy',
  1483. }
  1484. class Sailor(Enemy):
  1485. DataSet = {
  1486. 'build': HUSKY,
  1487. 'weapon': Cutlass(scale=.9),
  1488. }
  1489. EXAMPLE USAGE
  1490. =============
  1491. s = Sailor(faction='undead', build=SKINNY)
  1492. # make two copies of s
  1493. s2 = s.makeCopy()
  1494. s3 = Sailor(s)
  1495. # example sets
  1496. s2.setWeapon(Musket())
  1497. s3.build = TALL
  1498. # example gets
  1499. faction2 = s2.getFaction()
  1500. faction3 = s3.faction
  1501. """
  1502. class POD:
  1503. DataSet = {
  1504. # base class does not define any data items, but they would
  1505. # appear here as 'name': defaultValue,
  1506. #
  1507. # WARNING: default values of mutable types that do not copy by
  1508. # value (dicts, lists etc.) will be shared by all class instances.
  1509. # if default value is callable, it will be called to get actual
  1510. # default value
  1511. #
  1512. # for example:
  1513. #
  1514. # class MapData(POD):
  1515. # DataSet = {
  1516. # 'spawnIndices': Functor(list, [1,5,22]),
  1517. # }
  1518. }
  1519. def __init__(self, **kwArgs):
  1520. self.__class__._compileDefaultDataSet()
  1521. if __debug__:
  1522. # make sure all of the keyword arguments passed in
  1523. # are present in our data set
  1524. for arg in kwArgs.keys():
  1525. assert arg in self.getDataNames(), (
  1526. "unknown argument for %s: '%s'" % (
  1527. self.__class__, arg))
  1528. # assign each of our data items directly to self
  1529. for name in self.getDataNames():
  1530. # if a value has been passed in for a data item, use
  1531. # that value, otherwise use the default value
  1532. if name in kwArgs:
  1533. getSetter(self, name)(kwArgs[name])
  1534. else:
  1535. getSetter(self, name)(self.getDefaultValue(name))
  1536. def setDefaultValues(self):
  1537. # set all the default data values on ourself
  1538. for name in self.getDataNames():
  1539. getSetter(self, name)(self.getDefaultValue(name))
  1540. # this functionality used to be in the constructor, triggered by a single
  1541. # positional argument; that was conflicting with POD subclasses that wanted
  1542. # to define different behavior for themselves when given a positional
  1543. # constructor argument
  1544. def copyFrom(self, other, strict=False):
  1545. # if 'strict' is true, other must have a value for all of our data items
  1546. # otherwise we'll use the defaults
  1547. for name in self.getDataNames():
  1548. if hasattr(other, getSetterName(name, 'get')):
  1549. setattr(self, name, getSetter(other, name, 'get')())
  1550. else:
  1551. if strict:
  1552. raise "object '%s' doesn't have value '%s'" % (other, name)
  1553. else:
  1554. setattr(self, name, self.getDefaultValue(name))
  1555. # support 'p = POD.POD().copyFrom(other)' syntax
  1556. return self
  1557. def makeCopy(self):
  1558. # returns a duplicate of this object
  1559. return self.__class__().copyFrom(self)
  1560. def applyTo(self, obj):
  1561. # Apply our entire set of data to another POD
  1562. for name in self.getDataNames():
  1563. getSetter(obj, name)(getSetter(self, name, 'get')())
  1564. def getValue(self, name):
  1565. return getSetter(self, name, 'get')()
  1566. @classmethod
  1567. def getDataNames(cls):
  1568. # returns safely-mutable list of datum names
  1569. cls._compileDefaultDataSet()
  1570. return cls._DataSet.keys()
  1571. @classmethod
  1572. def getDefaultValue(cls, name):
  1573. cls._compileDefaultDataSet()
  1574. dv = cls._DataSet[name]
  1575. # this allows us to create a new mutable object every time we ask
  1576. # for its default value, i.e. if the default value is dict, this
  1577. # method will return a new empty dictionary object every time. This
  1578. # will cause problems if the intent is to store a callable object
  1579. # as the default value itself; we need a way to specify that the
  1580. # callable *is* the default value and not a default-value creation
  1581. # function
  1582. if hasattr(dv, '__call__'):
  1583. dv = dv()
  1584. return dv
  1585. @classmethod
  1586. def _compileDefaultDataSet(cls):
  1587. if '_DataSet' in cls.__dict__:
  1588. # we've already compiled the defaults for this class
  1589. return
  1590. # create setters & getters for this class
  1591. if 'DataSet' in cls.__dict__:
  1592. for name in cls.DataSet:
  1593. setterName = getSetterName(name)
  1594. if not hasattr(cls, setterName):
  1595. def defaultSetter(self, value, name=name):
  1596. setattr(self, name, value)
  1597. cls.__dict__[setterName] = defaultSetter
  1598. getterName = getSetterName(name, 'get')
  1599. if not hasattr(cls, getterName):
  1600. def defaultGetter(self, name=name):
  1601. return getattr(self, name)
  1602. cls.__dict__[getterName] = defaultGetter
  1603. # this dict will hold all of the aggregated default data values for
  1604. # this particular class, including values from its base classes
  1605. cls._DataSet = {}
  1606. bases = list(cls.__bases__)
  1607. # process in reverse of inheritance order, so that base classes listed first
  1608. # will take precedence over later base classes
  1609. bases.reverse()
  1610. for curBase in bases:
  1611. # skip multiple-inheritance base classes that do not derive from POD
  1612. if issubclass(curBase, POD):
  1613. # make sure this base has its dict of data defaults
  1614. curBase._compileDefaultDataSet()
  1615. # grab all inherited data default values
  1616. cls._DataSet.update(curBase._DataSet)
  1617. # pull in our own class' default values if any are specified
  1618. if 'DataSet' in cls.__dict__:
  1619. cls._DataSet.update(cls.DataSet)
  1620. def __repr__(self):
  1621. argStr = ''
  1622. for name in self.getDataNames():
  1623. argStr += '%s=%s,' % (name, repr(getSetter(self, name, 'get')()))
  1624. return '%s(%s)' % (self.__class__.__name__, argStr)
  1625. if __debug__ and __name__ == '__main__':
  1626. class PODtest(POD):
  1627. DataSet = {
  1628. 'foo': dict,
  1629. }
  1630. p1 = PODtest()
  1631. p2 = PODtest()
  1632. assert hasattr(p1, 'foo')
  1633. # make sure the getter is working
  1634. assert p1.getFoo() is p1.foo
  1635. p1.getFoo()[1] = 2
  1636. assert p1.foo[1] == 2
  1637. # make sure that each instance gets its own copy of a mutable
  1638. # data item
  1639. assert p1.foo is not p2.foo
  1640. assert len(p1.foo) == 1
  1641. assert len(p2.foo) == 0
  1642. # make sure the setter is working
  1643. p2.setFoo({10:20})
  1644. assert p2.foo[10] == 20
  1645. # make sure modifications to mutable data items don't affect other
  1646. # instances
  1647. assert p1.foo[1] == 2
  1648. class DerivedPOD(PODtest):
  1649. DataSet = {
  1650. 'bar': list,
  1651. }
  1652. d1 = DerivedPOD()
  1653. # make sure that derived instances get their own copy of mutable
  1654. # data items
  1655. assert hasattr(d1, 'foo')
  1656. assert len(d1.foo) == 0
  1657. # make sure derived instances get their own items
  1658. assert hasattr(d1, 'bar')
  1659. assert len(d1.bar) == 0
  1660. def bound(value, bound1, bound2):
  1661. """
  1662. returns value if value is between bound1 and bound2
  1663. otherwise returns bound that is closer to value
  1664. """
  1665. if bound1 > bound2:
  1666. return min(max(value, bound2), bound1)
  1667. else:
  1668. return min(max(value, bound1), bound2)
  1669. clamp = bound
  1670. def lerp(v0, v1, t):
  1671. """
  1672. returns a value lerped between v0 and v1, according to t
  1673. t == 0 maps to v0, t == 1 maps to v1
  1674. """
  1675. return v0 + ((v1 - v0) * t)
  1676. def getShortestRotation(start, end):
  1677. """
  1678. Given two heading values, return a tuple describing
  1679. the shortest interval from 'start' to 'end'. This tuple
  1680. can be used to lerp a camera between two rotations
  1681. while avoiding the 'spin' problem.
  1682. """
  1683. start, end = start % 360, end % 360
  1684. if abs(end - start) > 180:
  1685. if end < start:
  1686. end += 360
  1687. else:
  1688. start += 360
  1689. return (start, end)
  1690. def average(*args):
  1691. """ returns simple average of list of values """
  1692. val = 0.
  1693. for arg in args:
  1694. val += arg
  1695. return val / len(args)
  1696. class Averager:
  1697. def __init__(self, name):
  1698. self._name = name
  1699. self.reset()
  1700. def reset(self):
  1701. self._total = 0.
  1702. self._count = 0
  1703. def addValue(self, value):
  1704. self._total += value
  1705. self._count += 1
  1706. def getAverage(self):
  1707. return self._total / self._count
  1708. def getCount(self):
  1709. return self._count
  1710. def addListsByValue(a, b):
  1711. """
  1712. returns a new array containing the sums of the two array arguments
  1713. (c[0] = a[0 + b[0], etc.)
  1714. """
  1715. c = []
  1716. for x, y in zip(a, b):
  1717. c.append(x + y)
  1718. return c
  1719. def boolEqual(a, b):
  1720. """
  1721. returns true if a and b are both true or both false.
  1722. returns false otherwise
  1723. (a.k.a. xnor -- eXclusive Not OR).
  1724. """
  1725. return (a and b) or not (a or b)
  1726. def lineupPos(i, num, spacing):
  1727. """
  1728. use to line up a series of 'num' objects, in one dimension,
  1729. centered around zero
  1730. 'i' is the index of the object in the lineup
  1731. 'spacing' is the amount of space between objects in the lineup
  1732. """
  1733. assert num >= 1
  1734. assert i >= 0 and i < num
  1735. pos = float(i) * spacing
  1736. return pos - ((float(spacing) * (num-1))/2.)
  1737. def formatElapsedSeconds(seconds):
  1738. """
  1739. Returns a string of the form "mm:ss" or "hh:mm:ss" or "n days",
  1740. representing the indicated elapsed time in seconds.
  1741. """
  1742. sign = ''
  1743. if seconds < 0:
  1744. seconds = -seconds
  1745. sign = '-'
  1746. # We use math.floor() instead of casting to an int, so we avoid
  1747. # problems with numbers that are too large to represent as
  1748. # type int.
  1749. seconds = math.floor(seconds)
  1750. hours = math.floor(seconds / (60 * 60))
  1751. if hours > 36:
  1752. days = math.floor((hours + 12) / 24)
  1753. return "%s%d days" % (sign, days)
  1754. seconds -= hours * (60 * 60)
  1755. minutes = (int)(seconds / 60)
  1756. seconds -= minutes * 60
  1757. if hours != 0:
  1758. return "%s%d:%02d:%02d" % (sign, hours, minutes, seconds)
  1759. else:
  1760. return "%s%d:%02d" % (sign, minutes, seconds)
  1761. def solveQuadratic(a, b, c):
  1762. # quadratic equation: ax^2 + bx + c = 0
  1763. # quadratic formula: x = [-b +/- sqrt(b^2 - 4ac)] / 2a
  1764. # returns None, root, or [root1, root2]
  1765. # a cannot be zero.
  1766. if a == 0.:
  1767. return None
  1768. # calculate the determinant (b^2 - 4ac)
  1769. D = (b * b) - (4. * a * c)
  1770. if D < 0:
  1771. # there are no solutions (sqrt(negative number) is undefined)
  1772. return None
  1773. elif D == 0:
  1774. # only one root
  1775. return (-b) / (2. * a)
  1776. else:
  1777. # OK, there are two roots
  1778. sqrtD = math.sqrt(D)
  1779. twoA = 2. * a
  1780. root1 = ((-b) - sqrtD) / twoA
  1781. root2 = ((-b) + sqrtD) / twoA
  1782. return [root1, root2]
  1783. def stackEntryInfo(depth=0, baseFileName=1):
  1784. """
  1785. returns the sourcefilename, line number, and function name of
  1786. an entry in the stack.
  1787. 'depth' is how far back to go in the stack; 0 is the caller of this
  1788. function, 1 is the function that called the caller of this function, etc.
  1789. by default, strips off the path of the filename; override with baseFileName
  1790. returns (fileName, lineNum, funcName) --> (string, int, string)
  1791. returns (None, None, None) on error
  1792. """
  1793. try:
  1794. stack = None
  1795. frame = None
  1796. try:
  1797. stack = inspect.stack()
  1798. # add one to skip the frame associated with this function
  1799. frame = stack[depth+1]
  1800. filename = frame[1]
  1801. if baseFileName:
  1802. filename = os.path.basename(filename)
  1803. lineNum = frame[2]
  1804. funcName = frame[3]
  1805. result = (filename, lineNum, funcName)
  1806. finally:
  1807. del stack
  1808. del frame
  1809. except:
  1810. result = (None, None, None)
  1811. return result
  1812. def lineInfo(baseFileName=1):
  1813. """
  1814. returns the sourcefilename, line number, and function name of the
  1815. code that called this function
  1816. (answers the question: 'hey lineInfo, where am I in the codebase?')
  1817. see stackEntryInfo, above, for info on 'baseFileName' and return types
  1818. """
  1819. return stackEntryInfo(1, baseFileName)
  1820. def callerInfo(baseFileName=1, howFarBack=0):
  1821. """
  1822. returns the sourcefilename, line number, and function name of the
  1823. caller of the function that called this function
  1824. (answers the question: 'hey callerInfo, who called me?')
  1825. see stackEntryInfo, above, for info on 'baseFileName' and return types
  1826. """
  1827. return stackEntryInfo(2+howFarBack, baseFileName)
  1828. def lineTag(baseFileName=1, verbose=0, separator=':'):
  1829. """
  1830. returns a string containing the sourcefilename and line number
  1831. of the code that called this function
  1832. (equivalent to lineInfo, above, with different return type)
  1833. see stackEntryInfo, above, for info on 'baseFileName'
  1834. if 'verbose' is false, returns a compact string of the form
  1835. 'fileName:lineNum:funcName'
  1836. if 'verbose' is true, returns a longer string that matches the
  1837. format of Python stack trace dumps
  1838. returns empty string on error
  1839. """
  1840. fileName, lineNum, funcName = callerInfo(baseFileName)
  1841. if fileName is None:
  1842. return ''
  1843. if verbose:
  1844. return 'File "%s", line %s, in %s' % (fileName, lineNum, funcName)
  1845. else:
  1846. return '%s%s%s%s%s' % (fileName, separator, lineNum, separator,
  1847. funcName)
  1848. def findPythonModule(module):
  1849. # Look along the python load path for the indicated filename.
  1850. # Returns the located pathname, or None if the filename is not
  1851. # found.
  1852. filename = module + '.py'
  1853. for dir in sys.path:
  1854. pathname = os.path.join(dir, filename)
  1855. if os.path.exists(pathname):
  1856. return pathname
  1857. return None
  1858. def describeException(backTrace = 4):
  1859. # When called in an exception handler, returns a string describing
  1860. # the current exception.
  1861. def byteOffsetToLineno(code, byte):
  1862. # Returns the source line number corresponding to the given byte
  1863. # offset into the indicated Python code module.
  1864. import array
  1865. lnotab = array.array('B', code.co_lnotab)
  1866. line = code.co_firstlineno
  1867. for i in range(0, len(lnotab), 2):
  1868. byte -= lnotab[i]
  1869. if byte <= 0:
  1870. return line
  1871. line += lnotab[i+1]
  1872. return line
  1873. infoArr = sys.exc_info()
  1874. exception = infoArr[0]
  1875. exceptionName = getattr(exception, '__name__', None)
  1876. extraInfo = infoArr[1]
  1877. trace = infoArr[2]
  1878. stack = []
  1879. while trace.tb_next:
  1880. # We need to call byteOffsetToLineno to determine the true
  1881. # line number at which the exception occurred, even though we
  1882. # have both trace.tb_lineno and frame.f_lineno, which return
  1883. # the correct line number only in non-optimized mode.
  1884. frame = trace.tb_frame
  1885. module = frame.f_globals.get('__name__', None)
  1886. lineno = byteOffsetToLineno(frame.f_code, frame.f_lasti)
  1887. stack.append("%s:%s, " % (module, lineno))
  1888. trace = trace.tb_next
  1889. frame = trace.tb_frame
  1890. module = frame.f_globals.get('__name__', None)
  1891. lineno = byteOffsetToLineno(frame.f_code, frame.f_lasti)
  1892. stack.append("%s:%s, " % (module, lineno))
  1893. description = ""
  1894. for i in range(len(stack) - 1, max(len(stack) - backTrace, 0) - 1, -1):
  1895. description += stack[i]
  1896. description += "%s: %s" % (exceptionName, extraInfo)
  1897. return description
  1898. def clampScalar(value, a, b):
  1899. # calling this ought to be faster than calling both min and max
  1900. if a < b:
  1901. if value < a:
  1902. return a
  1903. elif value > b:
  1904. return b
  1905. else:
  1906. return value
  1907. else:
  1908. if value < b:
  1909. return b
  1910. elif value > a:
  1911. return a
  1912. else:
  1913. return value
  1914. def pivotScalar(scalar, pivot):
  1915. # reflect scalar about pivot; see tests below
  1916. return pivot + (pivot - scalar)
  1917. if __debug__ and __name__ == '__main__':
  1918. assert pivotScalar(1, 0) == -1
  1919. assert pivotScalar(-1, 0) == 1
  1920. assert pivotScalar(3, 5) == 7
  1921. assert pivotScalar(10, 1) == -8
  1922. def weightedChoice(choiceList, rng=random.random, sum=None):
  1923. """given a list of (weight, item) pairs, chooses an item based on the
  1924. weights. rng must return 0..1. if you happen to have the sum of the
  1925. weights, pass it in 'sum'."""
  1926. # TODO: add support for dicts
  1927. if sum is None:
  1928. sum = 0.
  1929. for weight, item in choiceList:
  1930. sum += weight
  1931. rand = rng()
  1932. accum = rand * sum
  1933. for weight, item in choiceList:
  1934. accum -= weight
  1935. if accum <= 0.:
  1936. return item
  1937. # rand is ~1., and floating-point error prevented accum from hitting 0.
  1938. # Or you passed in a 'sum' that was was too large.
  1939. # Return the last item.
  1940. return item
  1941. def randFloat(a, b=0., rng=random.random):
  1942. """returns a random float in [a, b]
  1943. call with single argument to generate random float between arg and zero
  1944. """
  1945. return lerp(a, b, rng())
  1946. def normalDistrib(a, b, gauss=random.gauss):
  1947. """
  1948. NOTE: assumes a < b
  1949. Returns random number between a and b, using gaussian distribution, with
  1950. mean=avg(a, b), and a standard deviation that fits ~99.7% of the curve
  1951. between a and b.
  1952. For ease of use, outlying results are re-computed until result is in [a, b]
  1953. This should fit the remaining .3% of the curve that lies outside [a, b]
  1954. uniformly onto the curve inside [a, b]
  1955. ------------------------------------------------------------------------
  1956. http://www-stat.stanford.edu/~naras/jsm/NormalDensity/NormalDensity.html
  1957. The 68-95-99.7% Rule
  1958. ====================
  1959. All normal density curves satisfy the following property which is often
  1960. referred to as the Empirical Rule:
  1961. 68% of the observations fall within 1 standard deviation of the mean.
  1962. 95% of the observations fall within 2 standard deviations of the mean.
  1963. 99.7% of the observations fall within 3 standard deviations of the mean.
  1964. Thus, for a normal distribution, almost all values lie within 3 standard
  1965. deviations of the mean.
  1966. ------------------------------------------------------------------------
  1967. In calculating our standard deviation, we divide (b-a) by 6, since the
  1968. 99.7% figure includes 3 standard deviations _on_either_side_ of the mean.
  1969. """
  1970. while True:
  1971. r = gauss((a+b)*.5, (b-a)/6.)
  1972. if (r >= a) and (r <= b):
  1973. return r
  1974. def weightedRand(valDict, rng=random.random):
  1975. """
  1976. pass in a dictionary with a selection -> weight mapping. Eg.
  1977. {"Choice 1": 10,
  1978. "Choice 2": 30,
  1979. "bear": 100}
  1980. -Weights need not add up to any particular value.
  1981. -The actual selection will be returned.
  1982. """
  1983. selections = valDict.keys()
  1984. weights = valDict.values()
  1985. totalWeight = 0
  1986. for weight in weights:
  1987. totalWeight += weight
  1988. # get a random value between 0 and the total of the weights
  1989. randomWeight = rng() * totalWeight
  1990. # find the index that corresponds with this weight
  1991. for i in range(len(weights)):
  1992. totalWeight -= weights[i]
  1993. if totalWeight <= randomWeight:
  1994. return selections[i]
  1995. assert True, "Should never get here"
  1996. return selections[-1]
  1997. def randUint31(rng=random.random):
  1998. """returns a random integer in [0..2^31).
  1999. rng must return float in [0..1]"""
  2000. return int(rng() * 0x7FFFFFFF)
  2001. def randInt32(rng=random.random):
  2002. """returns a random integer in [-2147483648..2147483647].
  2003. rng must return float in [0..1]
  2004. """
  2005. i = int(rng() * 0x7FFFFFFF)
  2006. if rng() < .5:
  2007. i *= -1
  2008. return i
  2009. def randUint32(rng=random.random):
  2010. """returns a random integer in [0..2^32).
  2011. rng must return float in [0..1]"""
  2012. return long(rng() * 0xFFFFFFFFL)
  2013. class SerialNumGen:
  2014. """generates serial numbers"""
  2015. def __init__(self, start=None):
  2016. if start is None:
  2017. start = 0
  2018. self.__counter = start-1
  2019. def next(self):
  2020. self.__counter += 1
  2021. return self.__counter
  2022. class SerialMaskedGen(SerialNumGen):
  2023. def __init__(self, mask, start=None):
  2024. self._mask = mask
  2025. SerialNumGen.__init__(self, start)
  2026. def next(self):
  2027. v = SerialNumGen.next(self)
  2028. return v & self._mask
  2029. _serialGen = SerialNumGen()
  2030. def serialNum():
  2031. global _serialGen
  2032. return _serialGen.next()
  2033. def uniqueName(name):
  2034. global _serialGen
  2035. return '%s-%s' % (name, _serialGen.next())
  2036. class EnumIter:
  2037. def __init__(self, enum):
  2038. self._values = enum._stringTable.keys()
  2039. self._index = 0
  2040. def __iter__(self):
  2041. return self
  2042. def next(self):
  2043. if self._index >= len(self._values):
  2044. raise StopIteration
  2045. self._index += 1
  2046. return self._values[self._index-1]
  2047. class Enum:
  2048. """Pass in list of strings or string of comma-separated strings.
  2049. Items are accessible as instance.item, and are assigned unique,
  2050. increasing integer values. Pass in integer for 'start' to override
  2051. starting value.
  2052. Example:
  2053. >>> colors = Enum('red, green, blue')
  2054. >>> colors.red
  2055. 0
  2056. >>> colors.green
  2057. 1
  2058. >>> colors.blue
  2059. 2
  2060. >>> colors.getString(colors.red)
  2061. 'red'
  2062. """
  2063. if __debug__:
  2064. # chars that cannot appear within an item string.
  2065. InvalidChars = string.whitespace
  2066. def _checkValidIdentifier(item):
  2067. invalidChars = string.whitespace+string.punctuation
  2068. invalidChars = invalidChars.replace('_','')
  2069. invalidFirstChars = invalidChars+string.digits
  2070. if item[0] in invalidFirstChars:
  2071. raise SyntaxError, ("Enum '%s' contains invalid first char" %
  2072. item)
  2073. if not disjoint(item, invalidChars):
  2074. for char in item:
  2075. if char in invalidChars:
  2076. raise SyntaxError, (
  2077. "Enum\n'%s'\ncontains illegal char '%s'" %
  2078. (item, char))
  2079. return 1
  2080. _checkValidIdentifier = staticmethod(_checkValidIdentifier)
  2081. def __init__(self, items, start=0):
  2082. if type(items) == types.StringType:
  2083. items = items.split(',')
  2084. self._stringTable = {}
  2085. # make sure we don't overwrite an existing element of the class
  2086. assert self._checkExistingMembers(items)
  2087. assert uniqueElements(items)
  2088. i = start
  2089. for item in items:
  2090. # remove leading/trailing whitespace
  2091. item = string.strip(item)
  2092. # is there anything left?
  2093. if len(item) == 0:
  2094. continue
  2095. # make sure there are no invalid characters
  2096. assert Enum._checkValidIdentifier(item)
  2097. self.__dict__[item] = i
  2098. self._stringTable[i] = item
  2099. i += 1
  2100. def __iter__(self):
  2101. return EnumIter(self)
  2102. def hasString(self, string):
  2103. return string in set(self._stringTable.values())
  2104. def fromString(self, string):
  2105. if self.hasString(string):
  2106. return self.__dict__[string]
  2107. # throw an error
  2108. {}[string]
  2109. def getString(self, value):
  2110. return self._stringTable[value]
  2111. def __contains__(self, value):
  2112. return value in self._stringTable
  2113. def __len__(self):
  2114. return len(self._stringTable)
  2115. def copyTo(self, obj):
  2116. # copies all members onto obj
  2117. for name, value in self._stringTable:
  2118. setattr(obj, name, value)
  2119. if __debug__:
  2120. def _checkExistingMembers(self, items):
  2121. for item in items:
  2122. if hasattr(self, item):
  2123. return 0
  2124. return 1
  2125. ############################################################
  2126. # class: Singleton
  2127. # Purpose: This provides a base metaclass for all classes
  2128. # that require one and only one instance.
  2129. #
  2130. # Example: class mySingleton:
  2131. # __metaclass__ = PythonUtil.Singleton
  2132. # def __init__(self, ...):
  2133. # ...
  2134. #
  2135. # Note: This class is based on Python's New-Style Class
  2136. # design. An error will occur if a defined class
  2137. # attemps to inherit from a Classic-Style Class only,
  2138. # ie: class myClassX:
  2139. # def __init__(self, ...):
  2140. # ...
  2141. #
  2142. # class myNewClassX(myClassX):
  2143. # __metaclass__ = PythonUtil.Singleton
  2144. # def __init__(self, ...):
  2145. # myClassX.__init__(self, ...)
  2146. # ...
  2147. #
  2148. # This causes problems because myNewClassX is a
  2149. # New-Style class that inherits from only a
  2150. # Classic-Style base class. There are two ways
  2151. # simple ways to resolve this issue.
  2152. #
  2153. # First, if possible, make myClassX a
  2154. # New-Style class by inheriting from object
  2155. # object. IE: class myClassX(object):
  2156. #
  2157. # If for some reason that is not an option, make
  2158. # myNewClassX inherit from object and myClassX.
  2159. # IE: class myNewClassX(object, myClassX):
  2160. ############################################################
  2161. class Singleton(type):
  2162. def __init__(cls, name, bases, dic):
  2163. super(Singleton, cls).__init__(name, bases, dic)
  2164. cls.instance=None
  2165. def __call__(cls, *args, **kw):
  2166. if cls.instance is None:
  2167. cls.instance=super(Singleton, cls).__call__(*args, **kw)
  2168. return cls.instance
  2169. class SingletonError(ValueError):
  2170. """ Used to indicate an inappropriate value for a Singleton."""
  2171. def printListEnumGen(l):
  2172. # log each individual item with a number in front of it
  2173. digits = 0
  2174. n = len(l)
  2175. while n > 0:
  2176. digits += 1
  2177. n //= 10
  2178. format = '%0' + '%s' % digits + 'i:%s'
  2179. for i in range(len(l)):
  2180. print format % (i, l[i])
  2181. yield None
  2182. def printListEnum(l):
  2183. for result in printListEnumGen(l):
  2184. pass
  2185. # base class for all Panda C++ objects
  2186. # libdtoolconfig doesn't seem to have this, grab it off of PandaNode
  2187. dtoolSuperBase = None
  2188. def _getDtoolSuperBase():
  2189. global dtoolSuperBase
  2190. from pandac.PandaModules import PandaNode
  2191. dtoolSuperBase = PandaNode('').__class__.__bases__[0].__bases__[0].__bases__[0]
  2192. assert repr(dtoolSuperBase) == "<type 'libdtoolconfig.DTOOL_SUPER_BASE111'>" \
  2193. or repr(dtoolSuperBase) == "<type 'libdtoolconfig.DTOOL_SUPPER_BASE111'>" \
  2194. or repr(dtoolSuperBase) == "<type 'dtoolconfig.DTOOL_SUPER_BASE111'>"
  2195. safeReprNotify = None
  2196. def _getSafeReprNotify():
  2197. global safeReprNotify
  2198. from direct.directnotify.DirectNotifyGlobal import directNotify
  2199. safeReprNotify = directNotify.newCategory("safeRepr")
  2200. return safeReprNotify
  2201. def safeRepr(obj):
  2202. global dtoolSuperBase
  2203. if dtoolSuperBase is None:
  2204. _getDtoolSuperBase()
  2205. global safeReprNotify
  2206. if safeReprNotify is None:
  2207. _getSafeReprNotify()
  2208. if isinstance(obj, dtoolSuperBase):
  2209. # repr of C++ object could crash, particularly if the object has been deleted
  2210. # log that we're calling repr
  2211. safeReprNotify.info('calling repr on instance of %s.%s' % (obj.__class__.__module__, obj.__class__.__name__))
  2212. sys.stdout.flush()
  2213. try:
  2214. return repr(obj)
  2215. except:
  2216. return '<** FAILED REPR OF %s instance at %s **>' % (obj.__class__.__name__, hex(id(obj)))
  2217. def safeReprTypeOnFail(obj):
  2218. global dtoolSuperBase
  2219. if dtoolSuperBase is None:
  2220. _getDtoolSuperBase()
  2221. global safeReprNotify
  2222. if safeReprNotify is None:
  2223. _getSafeReprNotify()
  2224. if isinstance(obj, dtoolSuperBase):
  2225. return type(obj)
  2226. try:
  2227. return repr(obj)
  2228. except:
  2229. return '<** FAILED REPR OF %s instance at %s **>' % (obj.__class__.__name__, hex(id(obj)))
  2230. def fastRepr(obj, maxLen=200, strFactor=10, _visitedIds=None):
  2231. """ caps the length of iterable types, so very large objects will print faster.
  2232. also prevents infinite recursion """
  2233. try:
  2234. if _visitedIds is None:
  2235. _visitedIds = set()
  2236. if id(obj) in _visitedIds:
  2237. return '<ALREADY-VISITED %s>' % itype(obj)
  2238. if type(obj) in (types.TupleType, types.ListType):
  2239. s = ''
  2240. s += {types.TupleType: '(',
  2241. types.ListType: '[',}[type(obj)]
  2242. if maxLen is not None and len(obj) > maxLen:
  2243. o = obj[:maxLen]
  2244. ellips = '...'
  2245. else:
  2246. o = obj
  2247. ellips = ''
  2248. _visitedIds.add(id(obj))
  2249. for item in o:
  2250. s += fastRepr(item, maxLen, _visitedIds=_visitedIds)
  2251. s += ', '
  2252. _visitedIds.remove(id(obj))
  2253. s += ellips
  2254. s += {types.TupleType: ')',
  2255. types.ListType: ']',}[type(obj)]
  2256. return s
  2257. elif type(obj) is types.DictType:
  2258. s = '{'
  2259. if maxLen is not None and len(obj) > maxLen:
  2260. o = obj.keys()[:maxLen]
  2261. ellips = '...'
  2262. else:
  2263. o = obj.keys()
  2264. ellips = ''
  2265. _visitedIds.add(id(obj))
  2266. for key in o:
  2267. value = obj[key]
  2268. s += '%s: %s, ' % (fastRepr(key, maxLen, _visitedIds=_visitedIds),
  2269. fastRepr(value, maxLen, _visitedIds=_visitedIds))
  2270. _visitedIds.remove(id(obj))
  2271. s += ellips
  2272. s += '}'
  2273. return s
  2274. elif type(obj) is types.StringType:
  2275. if maxLen is not None:
  2276. maxLen *= strFactor
  2277. if maxLen is not None and len(obj) > maxLen:
  2278. return safeRepr(obj[:maxLen])
  2279. else:
  2280. return safeRepr(obj)
  2281. else:
  2282. r = safeRepr(obj)
  2283. maxLen *= strFactor
  2284. if len(r) > maxLen:
  2285. r = r[:maxLen]
  2286. return r
  2287. except:
  2288. return '<** FAILED REPR OF %s **>' % obj.__class__.__name__
  2289. baseLine = {}
  2290. def baseLineCheck():
  2291. global baseLine
  2292. import gc
  2293. obj = gc.get_objects()
  2294. baseLine = {}
  2295. for i in obj:
  2296. baseLine[str(itype(i))] = 0
  2297. for i in obj:
  2298. baseLine[str(itype(i))] += 1
  2299. def diffSinceBaseLine():
  2300. import copy
  2301. import gc
  2302. obj = gc.get_objects()
  2303. since = copy.deepcopy(baseLine)
  2304. for i in obj:
  2305. since.setdefault(str(itype(i)), 0)
  2306. for i in obj:
  2307. since[str(itype(i))] -= 1
  2308. for i in since.keys():
  2309. if not since[i]:
  2310. del since[i]
  2311. else:
  2312. since[i] = abs(since[i])
  2313. final = [(since[x],x) for x in since]
  2314. final.sort()
  2315. final.reverse()
  2316. for i in final:
  2317. print i
  2318. final = []
  2319. since = []
  2320. # Recursively expand slist's objects
  2321. # into olist, using seen to track
  2322. # already processed objects.
  2323. def _getr(slist, olist, seen):
  2324. for e in slist:
  2325. if id(e) in seen:
  2326. continue
  2327. seen[id(e)] = None
  2328. olist.append(e)
  2329. tl = gc.get_referents(e)
  2330. if tl:
  2331. _getr(tl, olist, seen)
  2332. # The public function.
  2333. def get_all_objects():
  2334. """Return a list of all live Python
  2335. objects, not including the list itself."""
  2336. gcl = gc.get_objects()
  2337. olist = []
  2338. seen = {}
  2339. # Just in case:
  2340. seen[id(gcl)] = None
  2341. seen[id(olist)] = None
  2342. seen[id(seen)] = None
  2343. # _getr does the real work.
  2344. _getr(gcl, olist, seen)
  2345. return olist
  2346. def getIdList():
  2347. baseList = get_all_objects()
  2348. idList = {}
  2349. for i in baseList:
  2350. idList[id(i)] = i
  2351. return idList
  2352. ftype = None
  2353. def getTree(obj):
  2354. global ftype
  2355. if not ftype:
  2356. ftype = itype(sys._getframe())
  2357. objId = id(obj)
  2358. obj = None
  2359. idList = getIdList()
  2360. objList = [objId]
  2361. objTree = {objId:{}}
  2362. r_add_chain(objId, objList, objTree[objId], idList, 0 )
  2363. return convertTree(objTree, idList)
  2364. def convertTree(objTree, idList):
  2365. newTree = {}
  2366. for key in objTree.keys():
  2367. obj = (idList[key],)
  2368. newTree[obj] = {}
  2369. r_convertTree(objTree[key], newTree[obj], idList)
  2370. return newTree
  2371. def r_convertTree(oldTree, newTree, idList):
  2372. for key in oldTree.keys():
  2373. obj = idList.get(key)
  2374. if(not obj):
  2375. continue
  2376. obj = str(obj)[:100]
  2377. newTree[obj] = {}
  2378. r_convertTree(oldTree[key], newTree[obj], idList)
  2379. def pretty_print(tree):
  2380. for name in tree.keys():
  2381. print name
  2382. r_pretty_print(tree[name], 0)
  2383. def r_pretty_print(tree, num):
  2384. num+=1
  2385. for name in tree.keys():
  2386. print " "*num,name
  2387. r_pretty_print(tree[name],num)
  2388. def r_add_chain(objId, objList, objTree, idList, num):
  2389. num+=1
  2390. obj = idList.get(objId)
  2391. if(not obj):
  2392. return
  2393. refList = gc.get_referrers(obj)
  2394. for ref in refList:
  2395. refId = id(ref)
  2396. if ref == __builtins__:
  2397. continue
  2398. if ref == objList:
  2399. continue
  2400. if refId in objList:
  2401. continue
  2402. if(ref == idList):
  2403. continue
  2404. if(itype(ref) == ftype):
  2405. continue
  2406. if(itype(ref) == itype(sys)):
  2407. continue
  2408. objList.append(refId)
  2409. objTree[refId] = {}
  2410. refList = None
  2411. for refId in objTree:
  2412. r_add_chain(refId, objList, objTree[refId], idList, num)
  2413. def tagRepr(obj, tag):
  2414. """adds a string onto the repr output of an instance"""
  2415. def reprWithTag(oldRepr, tag, self):
  2416. return oldRepr() + '::<TAG=' + tag + '>'
  2417. oldRepr = getattr(obj, '__repr__', None)
  2418. if oldRepr is None:
  2419. def stringer(s):
  2420. return s
  2421. oldRepr = Functor(stringer, repr(obj))
  2422. stringer = None
  2423. obj.__repr__ = types.MethodType(Functor(reprWithTag, oldRepr, tag), obj, obj.__class__)
  2424. reprWithTag = None
  2425. return obj
  2426. def tagWithCaller(obj):
  2427. """add info about the caller of the caller"""
  2428. tagRepr(obj, str(callerInfo(howFarBack=1)))
  2429. def isDefaultValue(x):
  2430. return x == type(x)()
  2431. def notNone(A, B):
  2432. # returns A if not None, B otherwise
  2433. if A is None:
  2434. return B
  2435. return A
  2436. def appendStr(obj, st):
  2437. """adds a string onto the __str__ output of an instance"""
  2438. def appendedStr(oldStr, st, self):
  2439. return oldStr() + st
  2440. oldStr = getattr(obj, '__str__', None)
  2441. if oldStr is None:
  2442. def stringer(s):
  2443. return s
  2444. oldStr = Functor(stringer, str(obj))
  2445. stringer = None
  2446. obj.__str__ = types.MethodType(Functor(appendedStr, oldStr, st), obj, obj.__class__)
  2447. appendedStr = None
  2448. return obj
  2449. # convenience shortcuts for __dev__ debugging
  2450. # we don't have the __dev__ flag at this point
  2451. try:
  2452. import pdb
  2453. set_trace = pdb.set_trace
  2454. # set_trace that can be asserted
  2455. def setTrace():
  2456. set_trace()
  2457. return True
  2458. pm = pdb.pm
  2459. except:
  2460. # we're in production, there is no pdb module. assign these to something so that the
  2461. # __builtin__ exports will work
  2462. # references in the code should either be if __dev__'d or asserted
  2463. set_trace = None
  2464. setTrace = None
  2465. pm = None
  2466. class ScratchPad:
  2467. """empty class to stick values onto"""
  2468. def __init__(self, **kArgs):
  2469. for key, value in kArgs.iteritems():
  2470. setattr(self, key, value)
  2471. self._keys = set(kArgs.keys())
  2472. def add(self, **kArgs):
  2473. for key, value in kArgs.iteritems():
  2474. setattr(self, key, value)
  2475. self._keys.update(kArgs.keys())
  2476. def destroy(self):
  2477. for key in self._keys:
  2478. delattr(self, key)
  2479. # allow dict [] syntax
  2480. def __getitem__(self, itemName):
  2481. return getattr(self, itemName)
  2482. def get(self, itemName, default=None):
  2483. return getattr(self, itemName, default)
  2484. # allow 'in'
  2485. def __contains__(self, itemName):
  2486. return itemName in self._keys
  2487. class DestructiveScratchPad(ScratchPad):
  2488. # automatically calls destroy() on elements passed to __init__
  2489. def add(self, **kArgs):
  2490. for key, value in kArgs.iteritems():
  2491. if hasattr(self, key):
  2492. getattr(self, key).destroy()
  2493. setattr(self, key, value)
  2494. self._keys.update(kArgs.keys())
  2495. def destroy(self):
  2496. for key in self._keys:
  2497. getattr(self, key).destroy()
  2498. ScratchPad.destroy(self)
  2499. class Sync:
  2500. _SeriesGen = SerialNumGen()
  2501. def __init__(self, name, other=None):
  2502. self._name = name
  2503. if other is None:
  2504. self._series = self._SeriesGen.next()
  2505. self._value = 0
  2506. else:
  2507. self._series = other._series
  2508. self._value = other._value
  2509. def invalidate(self):
  2510. self._value = None
  2511. def change(self):
  2512. self._value += 1
  2513. def sync(self, other):
  2514. if (self._series != other._series) or (self._value != other._value):
  2515. self._series = other._series
  2516. self._value = other._value
  2517. return True
  2518. else:
  2519. return False
  2520. def isSynced(self, other):
  2521. return ((self._series == other._series) and
  2522. (self._value == other._value))
  2523. def __repr__(self):
  2524. return '%s(%s)<family=%s,value=%s>' % (self.__class__.__name__,
  2525. self._name, self._series, self._value)
  2526. class RefCounter:
  2527. def __init__(self, byId=False):
  2528. self._byId = byId
  2529. self._refCounts = {}
  2530. def _getKey(self, item):
  2531. if self._byId:
  2532. key = id(item)
  2533. else:
  2534. key = item
  2535. def inc(self, item):
  2536. key = self._getKey(item)
  2537. self._refCounts.setdefault(key, 0)
  2538. self._refCounts[key] += 1
  2539. def dec(self, item):
  2540. """returns True if ref count has hit zero"""
  2541. key = self._getKey(item)
  2542. self._refCounts[key] -= 1
  2543. result = False
  2544. if self._refCounts[key] == 0:
  2545. result = True
  2546. del self._refCounts[key]
  2547. return result
  2548. def itype(obj):
  2549. # version of type that gives more complete information about instance types
  2550. global dtoolSuperBase
  2551. t = type(obj)
  2552. if t is types.InstanceType:
  2553. return '%s of <class %s>>' % (repr(types.InstanceType)[:-1],
  2554. str(obj.__class__))
  2555. else:
  2556. # C++ object instances appear to be types via type()
  2557. # check if this is a C++ object
  2558. if dtoolSuperBase is None:
  2559. _getDtoolSuperBase()
  2560. if isinstance(obj, dtoolSuperBase):
  2561. return '%s of %s>' % (repr(types.InstanceType)[:-1],
  2562. str(obj.__class__))
  2563. return t
  2564. def deeptype(obj, maxLen=100, _visitedIds=None):
  2565. if _visitedIds is None:
  2566. _visitedIds = set()
  2567. if id(obj) in _visitedIds:
  2568. return '<ALREADY-VISITED %s>' % itype(obj)
  2569. t = type(obj)
  2570. if t in (types.TupleType, types.ListType):
  2571. s = ''
  2572. s += {types.TupleType: '(',
  2573. types.ListType: '[',}[type(obj)]
  2574. if maxLen is not None and len(obj) > maxLen:
  2575. o = obj[:maxLen]
  2576. ellips = '...'
  2577. else:
  2578. o = obj
  2579. ellips = ''
  2580. _visitedIds.add(id(obj))
  2581. for item in o:
  2582. s += deeptype(item, maxLen, _visitedIds=_visitedIds)
  2583. s += ', '
  2584. _visitedIds.remove(id(obj))
  2585. s += ellips
  2586. s += {types.TupleType: ')',
  2587. types.ListType: ']',}[type(obj)]
  2588. return s
  2589. elif type(obj) is types.DictType:
  2590. s = '{'
  2591. if maxLen is not None and len(obj) > maxLen:
  2592. o = obj.keys()[:maxLen]
  2593. ellips = '...'
  2594. else:
  2595. o = obj.keys()
  2596. ellips = ''
  2597. _visitedIds.add(id(obj))
  2598. for key in o:
  2599. value = obj[key]
  2600. s += '%s: %s, ' % (deeptype(key, maxLen, _visitedIds=_visitedIds),
  2601. deeptype(value, maxLen, _visitedIds=_visitedIds))
  2602. _visitedIds.remove(id(obj))
  2603. s += ellips
  2604. s += '}'
  2605. return s
  2606. else:
  2607. return str(itype(obj))
  2608. def getNumberedTypedString(items, maxLen=5000, numPrefix=''):
  2609. """get a string that has each item of the list on its own line,
  2610. and each item is numbered on the left from zero"""
  2611. digits = 0
  2612. n = len(items)
  2613. while n > 0:
  2614. digits += 1
  2615. n //= 10
  2616. digits = digits
  2617. format = numPrefix + '%0' + '%s' % digits + 'i:%s \t%s'
  2618. first = True
  2619. s = ''
  2620. snip = '<SNIP>'
  2621. for i in xrange(len(items)):
  2622. if not first:
  2623. s += '\n'
  2624. first = False
  2625. objStr = fastRepr(items[i])
  2626. if len(objStr) > maxLen:
  2627. objStr = '%s%s' % (objStr[:(maxLen-len(snip))], snip)
  2628. s += format % (i, itype(items[i]), objStr)
  2629. return s
  2630. def getNumberedTypedSortedString(items, maxLen=5000, numPrefix=''):
  2631. """get a string that has each item of the list on its own line,
  2632. the items are stringwise-sorted, and each item is numbered on
  2633. the left from zero"""
  2634. digits = 0
  2635. n = len(items)
  2636. while n > 0:
  2637. digits += 1
  2638. n //= 10
  2639. digits = digits
  2640. format = numPrefix + '%0' + '%s' % digits + 'i:%s \t%s'
  2641. snip = '<SNIP>'
  2642. strs = []
  2643. for item in items:
  2644. objStr = fastRepr(item)
  2645. if len(objStr) > maxLen:
  2646. objStr = '%s%s' % (objStr[:(maxLen-len(snip))], snip)
  2647. strs.append(objStr)
  2648. first = True
  2649. s = ''
  2650. strs.sort()
  2651. for i in xrange(len(strs)):
  2652. if not first:
  2653. s += '\n'
  2654. first = False
  2655. objStr = strs[i]
  2656. s += format % (i, itype(items[i]), strs[i])
  2657. return s
  2658. def getNumberedTypedSortedStringWithReferrersGen(items, maxLen=10000, numPrefix=''):
  2659. """get a string that has each item of the list on its own line,
  2660. the items are stringwise-sorted, the object's referrers are shown,
  2661. and each item is numbered on the left from zero"""
  2662. digits = 0
  2663. n = len(items)
  2664. while n > 0:
  2665. digits += 1
  2666. n //= 10
  2667. digits = digits
  2668. format = numPrefix + '%0' + '%s' % digits + 'i:%s @ %s \t%s'
  2669. snip = '<SNIP>'
  2670. strs = []
  2671. for item in items:
  2672. strs.append(fastRepr(item))
  2673. strs.sort()
  2674. for i in xrange(len(strs)):
  2675. item = items[i]
  2676. objStr = strs[i]
  2677. objStr += ', \tREFERRERS=['
  2678. referrers = gc.get_referrers(item)
  2679. for ref in referrers:
  2680. objStr += '%s@%s, ' % (itype(ref), id(ref))
  2681. objStr += ']'
  2682. if len(objStr) > maxLen:
  2683. objStr = '%s%s' % (objStr[:(maxLen-len(snip))], snip)
  2684. yield format % (i, itype(items[i]), id(items[i]), objStr)
  2685. def getNumberedTypedSortedStringWithReferrers(items, maxLen=10000, numPrefix=''):
  2686. """get a string that has each item of the list on its own line,
  2687. the items are stringwise-sorted, the object's referrers are shown,
  2688. and each item is numbered on the left from zero"""
  2689. s = ''
  2690. for line in getNumberedTypedSortedStringWithReferrersGen(items, maxLen, numPrefix):
  2691. s += '%s\n' % line
  2692. return s
  2693. def printNumberedTyped(items, maxLen=5000):
  2694. """print out each item of the list on its own line,
  2695. with each item numbered on the left from zero"""
  2696. digits = 0
  2697. n = len(items)
  2698. while n > 0:
  2699. digits += 1
  2700. n //= 10
  2701. digits = digits
  2702. format = '%0' + '%s' % digits + 'i:%s \t%s'
  2703. for i in xrange(len(items)):
  2704. objStr = fastRepr(items[i])
  2705. if len(objStr) > maxLen:
  2706. snip = '<SNIP>'
  2707. objStr = '%s%s' % (objStr[:(maxLen-len(snip))], snip)
  2708. print format % (i, itype(items[i]), objStr)
  2709. def printNumberedTypesGen(items, maxLen=5000):
  2710. digits = 0
  2711. n = len(items)
  2712. while n > 0:
  2713. digits += 1
  2714. n //= 10
  2715. digits = digits
  2716. format = '%0' + '%s' % digits + 'i:%s'
  2717. for i in xrange(len(items)):
  2718. print format % (i, itype(items[i]))
  2719. yield None
  2720. def printNumberedTypes(items, maxLen=5000):
  2721. """print out the type of each item of the list on its own line,
  2722. with each item numbered on the left from zero"""
  2723. for result in printNumberedTypesGen(items, maxLen):
  2724. yield result
  2725. class DelayedCall:
  2726. """ calls a func after a specified delay """
  2727. def __init__(self, func, name=None, delay=None):
  2728. if name is None:
  2729. name = 'anonymous'
  2730. if delay is None:
  2731. delay = .01
  2732. self._func = func
  2733. self._taskName = 'DelayedCallback-%s' % name
  2734. self._delay = delay
  2735. self._finished = False
  2736. self._addDoLater()
  2737. def destroy(self):
  2738. self._finished = True
  2739. self._removeDoLater()
  2740. def finish(self):
  2741. if not self._finished:
  2742. self._doCallback()
  2743. self.destroy()
  2744. def _addDoLater(self):
  2745. taskMgr.doMethodLater(self._delay, self._doCallback, self._taskName)
  2746. def _removeDoLater(self):
  2747. taskMgr.remove(self._taskName)
  2748. def _doCallback(self, task):
  2749. self._finished = True
  2750. func = self._func
  2751. del self._func
  2752. func()
  2753. class FrameDelayedCall:
  2754. """ calls a func after N frames """
  2755. def __init__(self, name, callback, frames=None, cancelFunc=None):
  2756. # checkFunc is optional; called every frame, if returns True, FrameDelay is cancelled
  2757. # and callback is not called
  2758. if frames is None:
  2759. frames = 1
  2760. self._name = name
  2761. self._frames = frames
  2762. self._callback = callback
  2763. self._cancelFunc = cancelFunc
  2764. self._taskName = uniqueName('%s-%s' % (self.__class__.__name__, self._name))
  2765. self._finished = False
  2766. self._startTask()
  2767. def destroy(self):
  2768. self._finished = True
  2769. self._stopTask()
  2770. def finish(self):
  2771. if not self._finished:
  2772. self._finished = True
  2773. self._callback()
  2774. self.destroy()
  2775. def _startTask(self):
  2776. taskMgr.add(self._frameTask, self._taskName)
  2777. self._counter = 0
  2778. def _stopTask(self):
  2779. taskMgr.remove(self._taskName)
  2780. def _frameTask(self, task):
  2781. if self._cancelFunc and self._cancelFunc():
  2782. self.destroy()
  2783. return task.done
  2784. self._counter += 1
  2785. if self._counter >= self._frames:
  2786. self.finish()
  2787. return task.done
  2788. return task.cont
  2789. class DelayedFunctor:
  2790. """ Waits for this object to be called, then calls supplied functor after a delay.
  2791. Effectively inserts a time delay between the caller and the functor. """
  2792. def __init__(self, functor, name=None, delay=None):
  2793. self._functor = functor
  2794. self._name = name
  2795. # FunctionInterval requires __name__
  2796. self.__name__ = self._name
  2797. self._delay = delay
  2798. def _callFunctor(self):
  2799. cb = Functor(self._functor, *self._args, **self._kwArgs)
  2800. del self._functor
  2801. del self._name
  2802. del self._delay
  2803. del self._args
  2804. del self._kwArgs
  2805. del self._delayedCall
  2806. del self.__name__
  2807. cb()
  2808. def __call__(self, *args, **kwArgs):
  2809. self._args = args
  2810. self._kwArgs = kwArgs
  2811. self._delayedCall = DelayedCall(self._callFunctor, self._name, self._delay)
  2812. class SubframeCall:
  2813. """Calls a callback at a specific time during the frame using the
  2814. task system"""
  2815. def __init__(self, functor, taskPriority, name=None):
  2816. self._functor = functor
  2817. self._name = name
  2818. self._taskName = uniqueName('SubframeCall-%s' % self._name)
  2819. taskMgr.add(self._doCallback,
  2820. self._taskName,
  2821. priority=taskPriority)
  2822. def _doCallback(self, task):
  2823. functor = self._functor
  2824. del self._functor
  2825. functor()
  2826. del self._name
  2827. self._taskName = None
  2828. return task.done
  2829. def cleanup(self):
  2830. if (self._taskName):
  2831. taskMgr.remove(self._taskName)
  2832. self._taskName = None
  2833. class ArgumentEater:
  2834. def __init__(self, numToEat, func):
  2835. self._numToEat = numToEat
  2836. self._func = func
  2837. def destroy(self):
  2838. del self._func
  2839. def __call__(self, *args, **kwArgs):
  2840. self._func(*args[self._numToEat:], **kwArgs)
  2841. class ClassTree:
  2842. def __init__(self, instanceOrClass):
  2843. if type(instanceOrClass) in (types.ClassType, types.TypeType):
  2844. cls = instanceOrClass
  2845. else:
  2846. cls = instanceOrClass.__class__
  2847. self._cls = cls
  2848. self._bases = []
  2849. for base in self._cls.__bases__:
  2850. if base not in (types.ObjectType, types.TypeType):
  2851. self._bases.append(ClassTree(base))
  2852. def getAllClasses(self):
  2853. # returns set of this class and all base classes
  2854. classes = set()
  2855. classes.add(self._cls)
  2856. for base in self._bases:
  2857. classes.update(base.getAllClasses())
  2858. return classes
  2859. def _getStr(self, indent=None, clsLeftAtIndent=None):
  2860. # indent is how far to the right to indent (i.e. how many levels
  2861. # deep in the hierarchy from the most-derived)
  2862. #
  2863. # clsLeftAtIndent is an array of # of classes left to be
  2864. # printed at each level of the hierarchy; most-derived is
  2865. # at index 0
  2866. if indent is None:
  2867. indent = 0
  2868. clsLeftAtIndent = [1]
  2869. s = ''
  2870. if (indent > 1):
  2871. for i in range(1, indent):
  2872. # if we have not printed all base classes at
  2873. # this indent level, keep printing the vertical
  2874. # column
  2875. if clsLeftAtIndent[i] > 0:
  2876. s += ' |'
  2877. else:
  2878. s += ' '
  2879. if (indent > 0):
  2880. s += ' +'
  2881. s += self._cls.__name__
  2882. clsLeftAtIndent[indent] -= 1
  2883. """
  2884. ### show the module to the right of the class name
  2885. moduleIndent = 48
  2886. if len(s) >= moduleIndent:
  2887. moduleIndent = (len(s) % 4) + 4
  2888. padding = moduleIndent - len(s)
  2889. s += padding * ' '
  2890. s += self._cls.__module__
  2891. ###
  2892. """
  2893. if len(self._bases):
  2894. newList = list(clsLeftAtIndent)
  2895. newList.append(len(self._bases))
  2896. bases = self._bases
  2897. # print classes with fewer bases first
  2898. bases.sort(lambda x,y: len(x._bases)-len(y._bases))
  2899. for base in bases:
  2900. s += '\n%s' % base._getStr(indent+1, newList)
  2901. return s
  2902. def __repr__(self):
  2903. return self._getStr()
  2904. class PStatScope:
  2905. collectors = {}
  2906. def __init__(self, level = None):
  2907. self.levels = []
  2908. if level:
  2909. self.levels.append(level)
  2910. def copy(self, push = None):
  2911. c = PStatScope()
  2912. c.levels = self.levels[:]
  2913. if push:
  2914. c.push(push)
  2915. return c
  2916. def __repr__(self):
  2917. return 'PStatScope - \'%s\'' % (self,)
  2918. def __str__(self):
  2919. return ':'.join(self.levels)
  2920. def push(self, level):
  2921. self.levels.append(level.replace('_',''))
  2922. def pop(self):
  2923. return self.levels.pop()
  2924. def start(self, push = None):
  2925. if push:
  2926. self.push(push)
  2927. pass
  2928. self.getCollector().start()
  2929. def stop(self, pop = False):
  2930. self.getCollector().stop()
  2931. if pop:
  2932. self.pop()
  2933. def getCollector(self):
  2934. label = str(self)
  2935. if label not in self.collectors:
  2936. from pandac.PandaModules import PStatCollector
  2937. self.collectors[label] = PStatCollector(label)
  2938. pass
  2939. # print ' ',self.collectors[label]
  2940. return self.collectors[label]
  2941. def pstatcollect(scope, level = None):
  2942. def decorator(f):
  2943. return f
  2944. try:
  2945. if not (__dev__ or config.GetBool('force-pstatcollect', 0)) or \
  2946. not scope:
  2947. return decorator
  2948. def decorator(f):
  2949. def wrap(*args, **kw):
  2950. scope.start(push = (level or f.__name__))
  2951. val = f(*args, **kw)
  2952. scope.stop(pop = True)
  2953. return val
  2954. return wrap
  2955. pass
  2956. except:
  2957. pass
  2958. return decorator
  2959. __report_indent = 0
  2960. def report(types = [], prefix = '', xform = None, notifyFunc = None, dConfigParam = []):
  2961. """
  2962. This is a decorator generating function. Use is similar to
  2963. a @decorator, except you must be sure to call it as a function.
  2964. It actually returns the decorator which is then used to transform
  2965. your decorated function. Confusing at first, I know.
  2966. Decoration occurs at function definition time.
  2967. If __dev__ is not defined, or resolves to False, this function
  2968. has no effect and no wrapping/transform occurs. So in production,
  2969. it's as if the report has been asserted out.
  2970. Parameters::
  2971. types : A subset list of ['timeStamp', 'frameCount', 'avLocation']
  2972. This allows you to specify certain useful bits of info.
  2973. module: Prints the module that this report statement
  2974. can be found in.
  2975. args: Prints the arguments as they were passed to
  2976. this function.
  2977. timeStamp: Adds the current frame time to the output.
  2978. deltaStamp: Adds the current AI synched frame time to
  2979. the output
  2980. frameCount: Adds the current frame count to the output.
  2981. Usually cleaner than the timeStamp output.
  2982. avLocation: Adds the localAvatar's network location
  2983. to the output. Useful for interest debugging.
  2984. interests: Prints the current interest state after the
  2985. report.
  2986. stackTrace: Prints a stack trace after the report.
  2987. prefix: Optional string to prepend to output, just before the function.
  2988. Allows for easy grepping and is useful when merging AI/Client
  2989. reports into a single file.
  2990. xform: Optional callback that accepts a single parameter: argument 0 to
  2991. the decorated function. (assumed to be 'self')
  2992. It should return a value to be inserted into the report output string.
  2993. notifyFunc: A notify function such as info, debug, warning, etc.
  2994. By default the report will be printed to stdout. This
  2995. will allow you send the report to a designated 'notify'
  2996. output.
  2997. dConfigParam: A list of Config.prc string variables.
  2998. By default the report will always print. If you
  2999. specify this param, it will only print if one of the
  3000. specified config strings resolve to True.
  3001. """
  3002. def indent(str):
  3003. global __report_indent
  3004. return ' '*__report_indent+str
  3005. def decorator(f):
  3006. return f
  3007. try:
  3008. if not (__dev__ or config.GetBool('force-reports', 0)):
  3009. return decorator
  3010. # determine whether we should use the decorator
  3011. # based on the value of dConfigParam.
  3012. dConfigParamList = []
  3013. doPrint = False
  3014. if not dConfigParam:
  3015. doPrint = True
  3016. else:
  3017. if not isinstance(dConfigParam, (list,tuple)):
  3018. dConfigParams = (dConfigParam,)
  3019. else:
  3020. dConfigParams = dConfigParam
  3021. dConfigParamList = [param for param in dConfigParams \
  3022. if config.GetBool('want-%s-report' % (param,), 0)]
  3023. doPrint = bool(dConfigParamList)
  3024. pass
  3025. if not doPrint:
  3026. return decorator
  3027. # Determine any prefixes defined in our Config.prc.
  3028. if prefix:
  3029. prefixes = set([prefix])
  3030. else:
  3031. prefixes = set()
  3032. pass
  3033. for param in dConfigParamList:
  3034. prefix = config.GetString('prefix-%s-report' % (param,), '')
  3035. if prefix:
  3036. prefixes.add(prefix)
  3037. pass
  3038. pass
  3039. except NameError,e:
  3040. return decorator
  3041. from direct.distributed.ClockDelta import globalClockDelta
  3042. def decorator(f):
  3043. def wrap(*args,**kwargs):
  3044. if args:
  3045. rArgs = [args[0].__class__.__name__ + ', ']
  3046. else:
  3047. rArgs = []
  3048. if 'args' in types:
  3049. rArgs += [repr(x)+', ' for x in args[1:]] + \
  3050. [ x + ' = ' + '%s, ' % repr(y) for x,y in kwargs.items()]
  3051. if not rArgs:
  3052. rArgs = '()'
  3053. else:
  3054. rArgs = '(' + reduce(str.__add__,rArgs)[:-2] + ')'
  3055. outStr = '%s%s' % (f.func_name, rArgs)
  3056. # Insert prefix place holder, if needed
  3057. if prefixes:
  3058. outStr = '%%s %s' % (outStr,)
  3059. if 'module' in types:
  3060. outStr = '%s {M:%s}' % (outStr, f.__module__.split('.')[-1])
  3061. if 'frameCount' in types:
  3062. outStr = '%-8d : %s' % (globalClock.getFrameCount(), outStr)
  3063. if 'timeStamp' in types:
  3064. outStr = '%-8.3f : %s' % (globalClock.getFrameTime(), outStr)
  3065. if 'deltaStamp' in types:
  3066. outStr = '%-8.2f : %s' % (globalClock.getRealTime() - \
  3067. globalClockDelta.delta, outStr)
  3068. if 'avLocation' in types:
  3069. outStr = '%s : %s' % (outStr, str(localAvatar.getLocation()))
  3070. if xform:
  3071. outStr = '%s : %s' % (outStr, xform(args[0]))
  3072. if prefixes:
  3073. # This will print the same report once for each prefix
  3074. for prefix in prefixes:
  3075. if notifyFunc:
  3076. notifyFunc(outStr % (prefix,))
  3077. else:
  3078. print indent(outStr % (prefix,))
  3079. else:
  3080. if notifyFunc:
  3081. notifyFunc(outStr)
  3082. else:
  3083. print indent(outStr)
  3084. if 'interests' in types:
  3085. base.cr.printInterestSets()
  3086. if 'stackTrace' in types:
  3087. print StackTrace()
  3088. global __report_indent
  3089. rVal = None
  3090. try:
  3091. __report_indent += 1
  3092. rVal = f(*args,**kwargs)
  3093. finally:
  3094. __report_indent -= 1
  3095. if rVal is not None:
  3096. print indent(' -> '+repr(rVal))
  3097. pass
  3098. pass
  3099. return rVal
  3100. wrap.func_name = f.func_name
  3101. wrap.func_dict = f.func_dict
  3102. wrap.func_doc = f.func_doc
  3103. wrap.__module__ = f.__module__
  3104. return wrap
  3105. return decorator
  3106. def getBase():
  3107. try:
  3108. return base
  3109. except:
  3110. return simbase
  3111. def getRepository():
  3112. try:
  3113. return base.cr
  3114. except:
  3115. return simbase.air
  3116. exceptionLoggedNotify = None
  3117. def exceptionLogged(append=True):
  3118. """decorator that outputs the function name and all arguments
  3119. if an exception passes back through the stack frame
  3120. if append is true, string is appended to the __str__ output of
  3121. the exception. if append is false, string is printed to the log
  3122. directly. If the output will take up many lines, it's recommended
  3123. to set append to False so that the exception stack is not hidden
  3124. by the output of this decorator.
  3125. """
  3126. try:
  3127. null = not __dev__
  3128. except:
  3129. null = not __debug__
  3130. if null:
  3131. # if we're not in __dev__, just return the function itself. This
  3132. # results in zero runtime overhead, since decorators are evaluated
  3133. # at module-load.
  3134. def nullDecorator(f):
  3135. return f
  3136. return nullDecorator
  3137. def _decoratorFunc(f, append=append):
  3138. global exceptionLoggedNotify
  3139. if exceptionLoggedNotify is None:
  3140. from direct.directnotify.DirectNotifyGlobal import directNotify
  3141. exceptionLoggedNotify = directNotify.newCategory("ExceptionLogged")
  3142. def _exceptionLogged(*args, **kArgs):
  3143. try:
  3144. return f(*args, **kArgs)
  3145. except Exception, e:
  3146. try:
  3147. s = '%s(' % f.func_name
  3148. for arg in args:
  3149. s += '%s, ' % arg
  3150. for key, value in kArgs.items():
  3151. s += '%s=%s, ' % (key, value)
  3152. if len(args) or len(kArgs):
  3153. s = s[:-2]
  3154. s += ')'
  3155. if append:
  3156. appendStr(e, '\n%s' % s)
  3157. else:
  3158. exceptionLoggedNotify.info(s)
  3159. except:
  3160. exceptionLoggedNotify.info(
  3161. '%s: ERROR IN PRINTING' % f.func_name)
  3162. raise
  3163. _exceptionLogged.__doc__ = f.__doc__
  3164. return _exceptionLogged
  3165. return _decoratorFunc
  3166. # class 'decorator' that records the stack at the time of creation
  3167. # be careful with this, it creates a StackTrace, and that can take a
  3168. # lot of CPU
  3169. def recordCreationStack(cls):
  3170. if not hasattr(cls, '__init__'):
  3171. raise 'recordCreationStack: class \'%s\' must define __init__' % cls.__name__
  3172. cls.__moved_init__ = cls.__init__
  3173. def __recordCreationStack_init__(self, *args, **kArgs):
  3174. self._creationStackTrace = StackTrace(start=1)
  3175. return self.__moved_init__(*args, **kArgs)
  3176. def getCreationStackTrace(self):
  3177. return self._creationStackTrace
  3178. def getCreationStackTraceCompactStr(self):
  3179. return self._creationStackTrace.compact()
  3180. def printCreationStackTrace(self):
  3181. print self._creationStackTrace
  3182. cls.__init__ = __recordCreationStack_init__
  3183. cls.getCreationStackTrace = getCreationStackTrace
  3184. cls.getCreationStackTraceCompactStr = getCreationStackTraceCompactStr
  3185. cls.printCreationStackTrace = printCreationStackTrace
  3186. return cls
  3187. # like recordCreationStack but stores the stack as a compact stack list-of-strings
  3188. # scales well for memory usage
  3189. def recordCreationStackStr(cls):
  3190. if not hasattr(cls, '__init__'):
  3191. raise 'recordCreationStackStr: class \'%s\' must define __init__' % cls.__name__
  3192. cls.__moved_init__ = cls.__init__
  3193. def __recordCreationStackStr_init__(self, *args, **kArgs):
  3194. # store as list of strings to conserve memory
  3195. self._creationStackTraceStrLst = StackTrace(start=1).compact().split(',')
  3196. return self.__moved_init__(*args, **kArgs)
  3197. def getCreationStackTraceCompactStr(self):
  3198. return ','.join(self._creationStackTraceStrLst)
  3199. def printCreationStackTrace(self):
  3200. print ','.join(self._creationStackTraceStrLst)
  3201. cls.__init__ = __recordCreationStackStr_init__
  3202. cls.getCreationStackTraceCompactStr = getCreationStackTraceCompactStr
  3203. cls.printCreationStackTrace = printCreationStackTrace
  3204. return cls
  3205. # class 'decorator' that logs all method calls for a particular class
  3206. def logMethodCalls(cls):
  3207. if not hasattr(cls, 'notify'):
  3208. raise 'logMethodCalls: class \'%s\' must have a notify' % cls.__name__
  3209. for name in dir(cls):
  3210. method = getattr(cls, name)
  3211. if hasattr(method, '__call__'):
  3212. def getLoggedMethodCall(method):
  3213. def __logMethodCall__(obj, *args, **kArgs):
  3214. s = '%s(' % method.__name__
  3215. for arg in args:
  3216. try:
  3217. argStr = repr(arg)
  3218. except:
  3219. argStr = 'bad repr: %s' % arg.__class__
  3220. s += '%s, ' % argStr
  3221. for karg, value in kArgs.items():
  3222. s += '%s=%s, ' % (karg, repr(value))
  3223. if len(args) or len(kArgs):
  3224. s = s[:-2]
  3225. s += ')'
  3226. obj.notify.info(s)
  3227. return method(obj, *args, **kArgs)
  3228. return __logMethodCall__
  3229. setattr(cls, name, getLoggedMethodCall(method))
  3230. __logMethodCall__ = None
  3231. return cls
  3232. # http://en.wikipedia.org/wiki/Golden_ratio
  3233. GoldenRatio = (1. + math.sqrt(5.)) / 2.
  3234. class GoldenRectangle:
  3235. @staticmethod
  3236. def getLongerEdge(shorter):
  3237. return shorter * GoldenRatio
  3238. @staticmethod
  3239. def getShorterEdge(longer):
  3240. return longer / GoldenRatio
  3241. class HotkeyBreaker:
  3242. def __init__(self,breakKeys = []):
  3243. from direct.showbase.DirectObject import DirectObject
  3244. self.do = DirectObject()
  3245. self.breakKeys = {}
  3246. if not isinstance(breakKeys, (list,tuple)):
  3247. breakKeys = (breakKeys,)
  3248. for key in breakKeys:
  3249. self.addBreakKey(key)
  3250. def addBreakKey(self,breakKey):
  3251. if __dev__:
  3252. self.do.accept(breakKey,self.breakFunc,extraArgs = [breakKey])
  3253. def removeBreakKey(self,breakKey):
  3254. if __dev__:
  3255. self.do.ignore(breakKey)
  3256. def breakFunc(self,breakKey):
  3257. if __dev__:
  3258. self.breakKeys[breakKey] = True
  3259. def setBreakPt(self, breakKey = None, persistent = False):
  3260. if __dev__:
  3261. if not breakKey:
  3262. import pdb;pdb.set_trace()
  3263. return True
  3264. else:
  3265. if self.breakKeys.get(breakKey,False):
  3266. if not persistent:
  3267. self.breakKeys.pop(breakKey)
  3268. import pdb;pdb.set_trace()
  3269. return True
  3270. return True
  3271. def clearBreakPt(self, breakKey):
  3272. if __dev__:
  3273. return bool(self.breakKeys.pop(breakKey,None))
  3274. def nullGen():
  3275. # generator that ends immediately
  3276. if False:
  3277. # yield that never runs but still exists, making this func a generator
  3278. yield None
  3279. def loopGen(l):
  3280. # generator that yields the items of an iterable object forever
  3281. def _gen(l):
  3282. while True:
  3283. for item in l:
  3284. yield item
  3285. gen = _gen(l)
  3286. # don't leak
  3287. _gen = None
  3288. return gen
  3289. def makeFlywheelGen(objects, countList=None, countFunc=None, scale=None):
  3290. # iterates and finally yields a flywheel generator object
  3291. # the number of appearances for each object is controlled by passing in
  3292. # a list of counts, or a functor that returns a count when called with
  3293. # an object from the 'objects' list.
  3294. # if scale is provided, all counts are scaled by the scale value and then int()'ed.
  3295. def flywheel(index2objectAndCount):
  3296. # generator to produce a sequence whose elements appear a specific number of times
  3297. while len(index2objectAndCount):
  3298. keyList = index2objectAndCount.keys()
  3299. for key in keyList:
  3300. if index2objectAndCount[key][1] > 0:
  3301. yield index2objectAndCount[key][0]
  3302. index2objectAndCount[key][1] -= 1
  3303. if index2objectAndCount[key][1] <= 0:
  3304. del index2objectAndCount[key]
  3305. # if we were not given a list of counts, create it by calling countFunc
  3306. if countList is None:
  3307. countList = []
  3308. for object in objects:
  3309. yield None
  3310. countList.append(countFunc(object))
  3311. if scale is not None:
  3312. # scale the counts if we've got a scale factor
  3313. for i in xrange(len(countList)):
  3314. yield None
  3315. if countList[i] > 0:
  3316. countList[i] = max(1, int(countList[i] * scale))
  3317. # create a dict for the flywheel to use during its iteration to efficiently select
  3318. # the objects for the sequence
  3319. index2objectAndCount = {}
  3320. for i in xrange(len(countList)):
  3321. yield None
  3322. index2objectAndCount[i] = [objects[i], countList[i]]
  3323. # create the flywheel generator
  3324. yield flywheel(index2objectAndCount)
  3325. def flywheel(*args, **kArgs):
  3326. # create a flywheel generator
  3327. # see arguments and comments in flywheelGen above
  3328. # example usage:
  3329. """
  3330. >>> for i in flywheel([1,2,3], countList=[10, 5, 1]):
  3331. ... print i,
  3332. ...
  3333. 1 2 3 1 2 1 2 1 2 1 2 1 1 1 1 1
  3334. """
  3335. for flywheel in makeFlywheelGen(*args, **kArgs):
  3336. pass
  3337. return flywheel
  3338. if __debug__ and __name__ == '__main__':
  3339. f = flywheel(['a','b','c','d'], countList=[11,20,3,4])
  3340. obj2count = {}
  3341. for obj in f:
  3342. obj2count.setdefault(obj, 0)
  3343. obj2count[obj] += 1
  3344. assert obj2count['a'] == 11
  3345. assert obj2count['b'] == 20
  3346. assert obj2count['c'] == 3
  3347. assert obj2count['d'] == 4
  3348. f = flywheel([1,2,3,4], countFunc=lambda x: x*2)
  3349. obj2count = {}
  3350. for obj in f:
  3351. obj2count.setdefault(obj, 0)
  3352. obj2count[obj] += 1
  3353. assert obj2count[1] == 2
  3354. assert obj2count[2] == 4
  3355. assert obj2count[3] == 6
  3356. assert obj2count[4] == 8
  3357. f = flywheel([1,2,3,4], countFunc=lambda x: x, scale = 3)
  3358. obj2count = {}
  3359. for obj in f:
  3360. obj2count.setdefault(obj, 0)
  3361. obj2count[obj] += 1
  3362. assert obj2count[1] == 1 * 3
  3363. assert obj2count[2] == 2 * 3
  3364. assert obj2count[3] == 3 * 3
  3365. assert obj2count[4] == 4 * 3
  3366. def quickProfile(name="unnamed"):
  3367. import pstats
  3368. def profileDecorator(f):
  3369. if(not config.GetBool("use-profiler",0)):
  3370. return f
  3371. def _profiled(*args, **kArgs):
  3372. # must do this in here because we don't have base/simbase
  3373. # at the time that PythonUtil is loaded
  3374. if(not config.GetBool("profile-debug",0)):
  3375. #dumb timings
  3376. st=globalClock.getRealTime()
  3377. f(*args,**kArgs)
  3378. s=globalClock.getRealTime()-st
  3379. print "Function %s.%s took %s seconds"%(f.__module__, f.__name__,s)
  3380. else:
  3381. import profile as prof, pstats
  3382. #detailed profile, stored in base.stats under (
  3383. if(not hasattr(base,"stats")):
  3384. base.stats={}
  3385. if(not base.stats.get(name)):
  3386. base.stats[name]=[]
  3387. prof.runctx('f(*args, **kArgs)', {'f':f,'args':args,'kArgs':kArgs},None,"t.prof")
  3388. s=pstats.Stats("t.prof")
  3389. #p=hotshot.Profile("t.prof")
  3390. #p.runctx('f(*args, **kArgs)', {'f':f,'args':args,'kArgs':kArgs},None)
  3391. #s = hotshot.stats.load("t.prof")
  3392. s.strip_dirs()
  3393. s.sort_stats("cumulative")
  3394. base.stats[name].append(s)
  3395. _profiled.__doc__ = f.__doc__
  3396. return _profiled
  3397. return profileDecorator
  3398. def getTotalAnnounceTime():
  3399. td=0
  3400. for objs in base.stats.values():
  3401. for stat in objs:
  3402. td+=getAnnounceGenerateTime(stat)
  3403. return td
  3404. def getAnnounceGenerateTime(stat):
  3405. val=0
  3406. stats=stat.stats
  3407. for i in stats.keys():
  3408. if(i[2]=="announceGenerate"):
  3409. newVal=stats[i][3]
  3410. if(newVal>val):
  3411. val=newVal
  3412. return val
  3413. def choice(condition, ifTrue, ifFalse):
  3414. # equivalent of C++ (condition ? ifTrue : ifFalse)
  3415. if condition:
  3416. return ifTrue
  3417. else:
  3418. return ifFalse
  3419. class MiniLog:
  3420. def __init__(self, name):
  3421. self.indent = 1
  3422. self.name = name
  3423. self.lines = []
  3424. def __str__(self):
  3425. return '%s\nMiniLog: %s\n%s\n%s\n%s' % \
  3426. ('*'*50, self.name, '-'*50, '\n'.join(self.lines), '*'*50)
  3427. def enterFunction(self, funcName, *args, **kw):
  3428. rArgs = [repr(x)+', ' for x in args] + \
  3429. [ x + ' = ' + '%s, ' % repr(y) for x,y in kw.items()]
  3430. if not rArgs:
  3431. rArgs = '()'
  3432. else:
  3433. rArgs = '(' + reduce(str.__add__,rArgs)[:-2] + ')'
  3434. line = '%s%s' % (funcName, rArgs)
  3435. self.appendFunctionCall(line)
  3436. self.indent += 1
  3437. return line
  3438. def exitFunction(self):
  3439. self.indent -= 1
  3440. return self.indent
  3441. def appendFunctionCall(self, line):
  3442. self.lines.append(' '*(self.indent*2) + line)
  3443. return line
  3444. def appendLine(self, line):
  3445. self.lines.append(' '*(self.indent*2) + '<< ' + line + ' >>')
  3446. return line
  3447. def flush(self):
  3448. outStr = str(self)
  3449. self.indent = 0
  3450. self.lines = []
  3451. return outStr
  3452. class MiniLogSentry:
  3453. def __init__(self, log, funcName, *args, **kw):
  3454. self.log = log
  3455. if self.log:
  3456. self.log.enterFunction(funcName, *args, **kw)
  3457. def __del__(self):
  3458. if self.log:
  3459. self.log.exitFunction()
  3460. del self.log
  3461. def logBlock(id, msg):
  3462. print '<< LOGBLOCK(%03d)' % id
  3463. print str(msg)
  3464. print '/LOGBLOCK(%03d) >>' % id
  3465. class HierarchyException(Exception):
  3466. JOSWILSO = 0
  3467. def __init__(self, owner, description):
  3468. self.owner = owner
  3469. self.desc = description
  3470. def __str__(self):
  3471. return '(%s): %s' % (self.owner, self.desc)
  3472. def __repr__(self):
  3473. return 'HierarchyException(%s)' % (self.owner, )
  3474. # __dev__ is not defined at import time, call this after it's defined
  3475. def recordFunctorCreationStacks():
  3476. global Functor
  3477. from pandac.PandaModules import getConfigShowbase
  3478. config = getConfigShowbase()
  3479. # off by default, very slow
  3480. if __dev__ and config.GetBool('record-functor-creation-stacks', 0):
  3481. if not hasattr(Functor, '_functorCreationStacksRecorded'):
  3482. Functor = recordCreationStackStr(Functor)
  3483. Functor._functorCreationStacksRecorded = True
  3484. Functor.__call__ = Functor._exceptionLoggedCreationStack__call__
  3485. def formatTimeCompact(seconds):
  3486. # returns string in format '1d3h22m43s'
  3487. result = ''
  3488. a = int(seconds)
  3489. seconds = a % 60
  3490. a //= 60
  3491. if a > 0:
  3492. minutes = a % 60
  3493. a //= 60
  3494. if a > 0:
  3495. hours = a % 24
  3496. a //= 24
  3497. if a > 0:
  3498. days = a
  3499. result += '%sd' % days
  3500. result += '%sh' % hours
  3501. result += '%sm' % minutes
  3502. result += '%ss' % seconds
  3503. return result
  3504. if __debug__ and __name__ == '__main__':
  3505. ftc = formatTimeCompact
  3506. assert ftc(0) == '0s'
  3507. assert ftc(1) == '1s'
  3508. assert ftc(60) == '1m0s'
  3509. assert ftc(64) == '1m4s'
  3510. assert ftc(60*60) == '1h0m0s'
  3511. assert ftc(24*60*60) == '1d0h0m0s'
  3512. assert ftc(24*60*60 + 2*60*60 + 34*60 + 12) == '1d2h34m12s'
  3513. del ftc
  3514. def formatTimeExact(seconds):
  3515. # like formatTimeCompact but leaves off '0 seconds', '0 minutes' etc. for
  3516. # times that are e.g. 1 hour, 3 days etc.
  3517. # returns string in format '1d3h22m43s'
  3518. result = ''
  3519. a = int(seconds)
  3520. seconds = a % 60
  3521. a //= 60
  3522. if a > 0:
  3523. minutes = a % 60
  3524. a //= 60
  3525. if a > 0:
  3526. hours = a % 24
  3527. a //= 24
  3528. if a > 0:
  3529. days = a
  3530. result += '%sd' % days
  3531. if hours or minutes or seconds:
  3532. result += '%sh' % hours
  3533. if minutes or seconds:
  3534. result += '%sm' % minutes
  3535. if seconds or result == '':
  3536. result += '%ss' % seconds
  3537. return result
  3538. if __debug__ and __name__ == '__main__':
  3539. fte = formatTimeExact
  3540. assert fte(0) == '0s'
  3541. assert fte(1) == '1s'
  3542. assert fte(2) == '2s'
  3543. assert fte(61) == '1m1s'
  3544. assert fte(60) == '1m'
  3545. assert fte(60*60) == '1h'
  3546. assert fte(24*60*60) == '1d'
  3547. assert fte((24*60*60) + (2 * 60)) == '1d0h2m'
  3548. del fte
  3549. class AlphabetCounter:
  3550. # object that produces 'A', 'B', 'C', ... 'AA', 'AB', etc.
  3551. def __init__(self):
  3552. self._curCounter = ['A']
  3553. def next(self):
  3554. result = ''.join([c for c in self._curCounter])
  3555. index = -1
  3556. while True:
  3557. curChar = self._curCounter[index]
  3558. if curChar is 'Z':
  3559. nextChar = 'A'
  3560. carry = True
  3561. else:
  3562. nextChar = chr(ord(self._curCounter[index])+1)
  3563. carry = False
  3564. self._curCounter[index] = nextChar
  3565. if carry:
  3566. if (-index) == len(self._curCounter):
  3567. self._curCounter = ['A',] + self._curCounter
  3568. break
  3569. else:
  3570. index -= 1
  3571. carry = False
  3572. else:
  3573. break
  3574. return result
  3575. if __debug__ and __name__ == '__main__':
  3576. def testAlphabetCounter():
  3577. tempList = []
  3578. ac = AlphabetCounter()
  3579. for i in xrange(26*3):
  3580. tempList.append(ac.next())
  3581. assert tempList == [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  3582. 'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ',
  3583. 'BA','BB','BC','BD','BE','BF','BG','BH','BI','BJ','BK','BL','BM','BN','BO','BP','BQ','BR','BS','BT','BU','BV','BW','BX','BY','BZ',]
  3584. ac = AlphabetCounter()
  3585. num = 26 # A-Z
  3586. num += (26*26) # AA-ZZ
  3587. num += 26 # AAZ
  3588. num += 1 # ABA
  3589. num += 2 # ABC
  3590. for i in xrange(num):
  3591. x = ac.next()
  3592. assert x == 'ABC'
  3593. testAlphabetCounter()
  3594. del testAlphabetCounter
  3595. globalPdb = None
  3596. traceCalled = False
  3597. def setupPdb():
  3598. import pdb;
  3599. class pandaPdb(pdb.Pdb):
  3600. def stop_here(self, frame):
  3601. global traceCalled
  3602. if(traceCalled):
  3603. result = pdb.Pdb.stop_here(self, frame)
  3604. if(result == True):
  3605. traceCalled = False
  3606. return result
  3607. if frame is self.stopframe:
  3608. return True
  3609. return False
  3610. global globalPdb
  3611. globalPdb = pandaPdb()
  3612. globalPdb.reset()
  3613. sys.settrace(globalPdb.trace_dispatch)
  3614. def pandaTrace():
  3615. if __dev__:
  3616. if not globalPdb:
  3617. setupPdb()
  3618. global traceCalled
  3619. globalPdb.set_trace(sys._getframe().f_back)
  3620. traceCalled = True
  3621. packageMap = {
  3622. "toontown":"$TOONTOWN",
  3623. "direct":"$DIRECT",
  3624. "otp":"$OTP",
  3625. "pirates":"$PIRATES",
  3626. }
  3627. #assuming . dereferncing for nice linking to imports
  3628. def pandaBreak(dotpath, linenum, temporary = 0, cond = None):
  3629. if __dev__:
  3630. from pandac.PandaModules import Filename
  3631. if not globalPdb:
  3632. setupPdb()
  3633. dirs = dotpath.split(".")
  3634. root = Filename.expandFrom(packageMap[dirs[0]]).toOsSpecific()
  3635. filename = root + "\\src"
  3636. for d in dirs[1:]:
  3637. filename="%s\\%s"%(filename,d)
  3638. print filename
  3639. globalPdb.set_break(filename+".py", linenum, temporary, cond)
  3640. class Default:
  3641. # represents 'use the default value'
  3642. # useful for keyword arguments to virtual methods
  3643. pass
  3644. superLogFile = None
  3645. def startSuperLog(customFunction = None):
  3646. global superLogFile
  3647. if(not superLogFile):
  3648. superLogFile = open("c:\\temp\\superLog.txt", "w")
  3649. def trace_dispatch(a,b,c):
  3650. if(b=='call' and a.f_code.co_name != '?' and a.f_code.co_name.find("safeRepr")<0):
  3651. vars = dict(a.f_locals)
  3652. if 'self' in vars:
  3653. del vars['self']
  3654. if '__builtins__' in vars:
  3655. del vars['__builtins__']
  3656. for i in vars:
  3657. vars[i] = safeReprTypeOnFail(vars[i])
  3658. if customFunction:
  3659. superLogFile.write( "before = %s\n"%customFunction())
  3660. superLogFile.write( "%s(%s):%s:%s\n"%(a.f_code.co_filename.split("\\")[-1],a.f_code.co_firstlineno, a.f_code.co_name, vars))
  3661. if customFunction:
  3662. superLogFile.write( "after = %s\n"%customFunction())
  3663. return trace_dispatch
  3664. sys.settrace(trace_dispatch)
  3665. def endSuperLog():
  3666. global superLogFile
  3667. if(superLogFile):
  3668. sys.settrace(None)
  3669. superLogFile.close()
  3670. superLogFile = None
  3671. def isInteger(n):
  3672. return type(n) in (types.IntType, types.LongType)
  3673. def configIsToday(configName):
  3674. # TODO: replace usage of strptime with something else
  3675. # returns true if config string is a valid representation of today's date
  3676. today = time.localtime()
  3677. confStr = config.GetString(configName, '')
  3678. for format in ('%m/%d/%Y', '%m-%d-%Y', '%m.%d.%Y'):
  3679. try:
  3680. confDate = time.strptime(confStr, format)
  3681. except ValueError:
  3682. pass
  3683. else:
  3684. if (confDate.tm_year == today.tm_year and
  3685. confDate.tm_mon == today.tm_mon and
  3686. confDate.tm_mday == today.tm_mday):
  3687. return True
  3688. return False
  3689. def typeName(o):
  3690. if hasattr(o, '__class__'):
  3691. return o.__class__.__name__
  3692. else:
  3693. return o.__name__
  3694. def safeTypeName(o):
  3695. try:
  3696. return typeName(o)
  3697. except:
  3698. pass
  3699. try:
  3700. return type(o)
  3701. except:
  3702. pass
  3703. return '<failed safeTypeName()>'
  3704. def histogramDict(l):
  3705. d = {}
  3706. for e in l:
  3707. d.setdefault(e, 0)
  3708. d[e] += 1
  3709. return d
  3710. def unescapeHtmlString(s):
  3711. # converts %## to corresponding character
  3712. # replaces '+' with ' '
  3713. result = ''
  3714. i = 0
  3715. while i < len(s):
  3716. char = s[i]
  3717. if char == '+':
  3718. char = ' '
  3719. elif char == '%':
  3720. if i < (len(s)-2):
  3721. num = int(s[i+1:i+3], 16)
  3722. char = chr(num)
  3723. i += 2
  3724. i += 1
  3725. result += char
  3726. return result
  3727. if __debug__ and __name__ == '__main__':
  3728. assert unescapeHtmlString('asdf') == 'asdf'
  3729. assert unescapeHtmlString('as+df') == 'as df'
  3730. assert unescapeHtmlString('as%32df') == 'as2df'
  3731. assert unescapeHtmlString('asdf%32') == 'asdf2'
  3732. def repeatableRepr(obj):
  3733. if type(obj) is types.DictType:
  3734. keys = obj.keys()
  3735. keys.sort()
  3736. s = '{'
  3737. for i in xrange(len(keys)):
  3738. key = keys[i]
  3739. s += repeatableRepr(key)
  3740. s += ': '
  3741. s += repeatableRepr(obj[key])
  3742. if i < (len(keys)-1):
  3743. s += ', '
  3744. s += '}'
  3745. return s
  3746. elif type(obj) is type(set()):
  3747. l = []
  3748. for item in obj:
  3749. l.append(item)
  3750. l.sort()
  3751. return repeatableRepr(l)
  3752. return repr(obj)
  3753. if __debug__ and __name__ == '__main__':
  3754. assert repeatableRepr({1: 'a', 2: 'b'}) == repeatableRepr({2: 'b', 1: 'a'})
  3755. assert repeatableRepr(set([1,2,3])) == repeatableRepr(set([3,2,1]))
  3756. #set up bpdb
  3757. bpdb = BpDb.BpDb()
  3758. def bpdbGetEnabled():
  3759. enabled = True
  3760. try:
  3761. enabled = __dev__
  3762. enabled = ConfigVariableBool('force-breakpoints', enabled).getValue()
  3763. finally:
  3764. return enabled
  3765. bpdb.setEnabledCallback(bpdbGetEnabled)
  3766. bpdb.setConfigCallback(lambda cfg: ConfigVariableBool('want-bp-%s' % (cfg.lower(),), 0).getValue())
  3767. def u2ascii(s):
  3768. # Unicode -> ASCII
  3769. if type(s) is types.UnicodeType:
  3770. return unicodedata.normalize('NFKD', s).encode('ascii', 'backslashreplace')
  3771. else:
  3772. return str(s)
  3773. def unicodeUtf8(s):
  3774. # * -> Unicode UTF-8
  3775. if type(s) is types.UnicodeType:
  3776. return s
  3777. else:
  3778. return unicode(str(s), 'utf-8')
  3779. def encodedUtf8(s):
  3780. # * -> 8-bit-encoded UTF-8
  3781. return unicodeUtf8(s).encode('utf-8')
  3782. class PriorityCallbacks:
  3783. """ manage a set of prioritized callbacks, and allow them to be invoked in order of priority """
  3784. def __init__(self):
  3785. self._callbacks = []
  3786. def clear(self):
  3787. while self._callbacks:
  3788. self._callbacks.pop()
  3789. def add(self, callback, priority=None):
  3790. if priority is None:
  3791. priority = 0
  3792. item = (priority, callback)
  3793. bisect.insort(self._callbacks, item)
  3794. return item
  3795. def remove(self, item):
  3796. self._callbacks.pop(bisect.bisect_left(self._callbacks, item))
  3797. def __call__(self):
  3798. for priority, callback in self._callbacks:
  3799. callback()
  3800. if __debug__ and __name__ == '__main__':
  3801. l = []
  3802. def a(l=l):
  3803. l.append('a')
  3804. def b(l=l):
  3805. l.append('b')
  3806. def c(l=l):
  3807. l.append('c')
  3808. pc = PriorityCallbacks()
  3809. pc.add(a)
  3810. pc()
  3811. assert l == ['a']
  3812. while len(l):
  3813. l.pop()
  3814. bItem = pc.add(b)
  3815. pc()
  3816. assert 'a' in l
  3817. assert 'b' in l
  3818. assert len(l) == 2
  3819. while len(l):
  3820. l.pop()
  3821. pc.remove(bItem)
  3822. pc()
  3823. assert l == ['a']
  3824. while len(l):
  3825. l.pop()
  3826. pc.add(c, 2)
  3827. bItem = pc.add(b, 10)
  3828. pc()
  3829. assert l == ['a', 'c', 'b']
  3830. while len(l):
  3831. l.pop()
  3832. pc.remove(bItem)
  3833. pc()
  3834. assert l == ['a', 'c']
  3835. while len(l):
  3836. l.pop()
  3837. pc.clear()
  3838. pc()
  3839. assert len(l) == 0
  3840. del l
  3841. del a
  3842. del b
  3843. del c
  3844. del pc
  3845. del bItem
  3846. import __builtin__
  3847. __builtin__.Functor = Functor
  3848. __builtin__.Stack = Stack
  3849. __builtin__.Queue = Queue
  3850. __builtin__.Enum = Enum
  3851. __builtin__.SerialNumGen = SerialNumGen
  3852. __builtin__.SerialMaskedGen = SerialMaskedGen
  3853. __builtin__.ScratchPad = ScratchPad
  3854. __builtin__.DestructiveScratchPad = DestructiveScratchPad
  3855. __builtin__.uniqueName = uniqueName
  3856. __builtin__.serialNum = serialNum
  3857. __builtin__.profiled = profiled
  3858. __builtin__.set_trace = set_trace
  3859. __builtin__.setTrace = setTrace
  3860. __builtin__.pm = pm
  3861. __builtin__.itype = itype
  3862. __builtin__.exceptionLogged = exceptionLogged
  3863. __builtin__.appendStr = appendStr
  3864. __builtin__.bound = bound
  3865. __builtin__.clamp = clamp
  3866. __builtin__.lerp = lerp
  3867. __builtin__.notNone = notNone
  3868. __builtin__.clampScalar = clampScalar
  3869. __builtin__.makeList = makeList
  3870. __builtin__.makeTuple = makeTuple
  3871. __builtin__.printStack = printStack
  3872. __builtin__.printReverseStack = printReverseStack
  3873. __builtin__.printVerboseStack = printVerboseStack
  3874. __builtin__.DelayedCall = DelayedCall
  3875. __builtin__.DelayedFunctor = DelayedFunctor
  3876. __builtin__.FrameDelayedCall = FrameDelayedCall
  3877. __builtin__.SubframeCall = SubframeCall
  3878. __builtin__.ArgumentEater = ArgumentEater
  3879. __builtin__.ClassTree = ClassTree
  3880. __builtin__.invertDict = invertDict
  3881. __builtin__.invertDictLossless = invertDictLossless
  3882. __builtin__.getBase = getBase
  3883. __builtin__.getRepository = getRepository
  3884. __builtin__.safeRepr = safeRepr
  3885. __builtin__.fastRepr = fastRepr
  3886. __builtin__.nullGen = nullGen
  3887. __builtin__.flywheel = flywheel
  3888. __builtin__.loopGen = loopGen
  3889. __builtin__.StackTrace = StackTrace
  3890. __builtin__.choice = choice
  3891. __builtin__.report = report
  3892. __builtin__.pstatcollect = pstatcollect
  3893. __builtin__.MiniLog = MiniLog
  3894. __builtin__.MiniLogSentry = MiniLogSentry
  3895. __builtin__.logBlock = logBlock
  3896. __builtin__.HierarchyException = HierarchyException
  3897. __builtin__.pdir = pdir
  3898. __builtin__.deeptype = deeptype
  3899. __builtin__.Default = Default
  3900. __builtin__.isInteger = isInteger
  3901. __builtin__.configIsToday = configIsToday
  3902. __builtin__.typeName = typeName
  3903. __builtin__.safeTypeName = safeTypeName
  3904. __builtin__.histogramDict = histogramDict
  3905. __builtin__.repeatableRepr = repeatableRepr
  3906. __builtin__.bpdb = bpdb
  3907. __builtin__.u2ascii = u2ascii
  3908. __builtin__.unicodeUtf8 = unicodeUtf8
  3909. __builtin__.encodedUtf8 = encodedUtf8