Reorging the code. Don't worry, no layoffs
[doneit] / src / toolbox.py
index fe20e00..e21ca82 100644 (file)
@@ -1,6 +1,7 @@
 import sys
 import StringIO
 import urllib
+import urlparse
 from xml.dom import minidom
 import datetime
 
@@ -155,6 +156,36 @@ def load_xml(source, alternative=None):
        return xmldoc
 
 
+def abbreviate(text, expectedLen):
+       singleLine = " ".join(text.split("\n"))
+       lineLen = len(singleLine)
+       if lineLen <= expectedLen:
+               return singleLine
+
+       abbrev = "..."
+
+       leftLen = expectedLen // 2 - 1
+       rightLen = max(expectedLen - leftLen - len(abbrev) + 1, 1)
+
+       abbrevText =  singleLine[0:leftLen] + abbrev + singleLine[-rightLen:-1]
+       assert len(abbrevText) <= expectedLen, "Too long: '%s'" % abbrevText
+       return abbrevText
+
+
+def abbreviate_url(url, domainLength, pathLength):
+       urlParts = urlparse.urlparse(url)
+
+       netloc = urlParts.netloc
+       path = urlParts.path
+
+       pathLength += max(domainLength - len(netloc), 0)
+       domainLength += max(pathLength - len(path), 0)
+
+       netloc = abbreviate(netloc, domainLength)
+       path = abbreviate(path, pathLength)
+       return netloc + path
+
+
 def is_same_year(targetDate, todaysDate = datetime.datetime.today()):
        return targetDate.year == todaysDate.year