oldschool-toontown/otp/otpbase/PythonUtil.py
2019-11-08 18:57:25 -05:00

57 lines
2 KiB
Python

import __builtin__
# class 'decorator' that records the stack at the time of creation
# be careful with this, it creates a StackTrace, and that can take a
# lot of CPU
def recordCreationStack(cls):
if not hasattr(cls, '__init__'):
raise 'recordCreationStack: class \'%s\' must define __init__' % cls.__name__
cls.__moved_init__ = cls.__init__
def __recordCreationStack_init__(self, *args, **kArgs):
self._creationStackTrace = StackTrace(start=1)
return self.__moved_init__(*args, **kArgs)
def getCreationStackTrace(self):
return self._creationStackTrace
def getCreationStackTraceCompactStr(self):
return self._creationStackTrace.compact()
def printCreationStackTrace(self):
print self._creationStackTrace
cls.__init__ = __recordCreationStack_init__
cls.getCreationStackTrace = getCreationStackTrace
cls.getCreationStackTraceCompactStr = getCreationStackTraceCompactStr
cls.printCreationStackTrace = printCreationStackTrace
return cls
def pdir(obj, str = None, width = None,
fTruncate = 1, lineWidth = 75, wantPrivate = 0):
# Remove redundant class entries
uniqueLineage = []
for l in getClassLineage(obj):
if type(l) == types.ClassType:
if l in uniqueLineage:
break
uniqueLineage.append(l)
# Pretty print out directory info
uniqueLineage.reverse()
for obj in uniqueLineage:
_pdir(obj, str, width, fTruncate, lineWidth, wantPrivate)
print
def quantize(value, divisor):
# returns new value that is multiple of (1. / divisor)
return float(int(value * int(divisor))) / int(divisor)
def quantizeVec(vec, divisor):
# in-place
vec[0] = quantize(vec[0], divisor)
vec[1] = quantize(vec[1], divisor)
vec[2] = quantize(vec[2], divisor)
def isClient():
if hasattr(__builtin__, 'simbase') and not hasattr(__builtin__, 'base'):
return False
return True
__builtin__.pdir = pdir
__builtin__.isClient = isClient