+
+
+def Truncate(text, length):
+ """Truncate string and add ellipsis if needed.
+
+ @type text: string
+ @param text: Text
+ @type length: integer
+ @param length: Desired length
+ @rtype: string
+ @return: Truncated text
+
+ """
+ assert length > len(_ASCII_ELLIPSIS)
+
+ # Serialize if necessary
+ if not isinstance(text, basestring):
+ text = str(text)
+
+ if len(text) <= length:
+ return text
+ else:
+ return text[:length - len(_ASCII_ELLIPSIS)] + _ASCII_ELLIPSIS