# HG changeset patch # User casties # Date 1418314785 -3600 # Node ID a71ae589d34277fc587a8b973ad3715f269997d5 # Parent 79973dee60bd57647d618de599680b7fe45b0b39 use https for annotations and fix bug when logging httplib errors. diff -r 79973dee60bd -r a71ae589d342 SrvTxtUtils.py --- a/SrvTxtUtils.py Mon Nov 24 14:40:00 2014 +0100 +++ b/SrvTxtUtils.py Thu Dec 11 17:19:45 2014 +0100 @@ -10,7 +10,6 @@ import logging import time import re -import string import datetime try: import httplib2 @@ -22,34 +21,54 @@ import xml.etree.ElementTree as ET -srvTxtUtilsVersion = "1.12.2" +srvTxtUtilsVersion = "1.12.3" map_months = {'en': [u"", - u"January", - u"February", - u"March", - u"April", - u"May", - u"June", - u"July", - u"August", - u"September", - u"October", - u"November", - u"December"], + u"January", + u"February", + u"March", + u"April", + u"May", + u"June", + u"July", + u"August", + u"September", + u"October", + u"November", + u"December"], 'de': [u"", - u"Januar", - u"Februar", - u"M\u00e4rz", - u"April", - u"Mai", - u"Juni", - u"Juli", - u"August", - u"September", - u"Oktober", - u"November", - u"Dezember"]} + u"Januar", + u"Februar", + u"M\u00e4rz", + u"April", + u"Mai", + u"Juni", + u"Juli", + u"August", + u"September", + u"Oktober", + u"November", + u"Dezember"]} + +map_weekdays_short = {'en': [ + u"Mo", + u"Tu", + u"We", + u"Th", + u"Fr", + u"Sa", + u"Su", + ], + 'de': [ + u"Mo", + u"Di", + u"Mi", + u"Do", + u"Fr", + u"Sa", + u"So", + ]} + def getInt(number, default=0): """returns always an int (0 in case of problems)""" @@ -84,13 +103,17 @@ return "" if isinstance(s, str): return s - elif isinstance(s, unicode): + else: return s.encode('utf-8') - else: - return str(s) + -def getText(node, recursive=0): - """returns all text content of a (etree) node and its subnodes""" +def getTextFromNode(node, recursive=False, length=0): + """Return all text content of a (etree) node. + + :param recursive: descend subnodes + + :returns: text string + """ if node is None: return '' @@ -113,6 +136,55 @@ return text +getText = getTextFromNode + +def getPlaintext(text, length=0, wordwrap=False, ignoretags=[]): + """Return plain text content by filtering out XML tags. + + :param text: string or etree node + :param length: length of text to return (0=all) + :param wordwrap: try not to break the last word (may return shorter string) + :returns: text string + """ + if text is None: + return '' + + try: + if isinstance(text, basestring): + xmltext = utf8ify("
%s
"%text) + dom = ET.fromstring(xmltext) + else: + dom = text + + plaintext = '' + for elem in dom.iter(): + if elem.tag in ignoretags: + # ignore tag + continue + + if elem.text: + plaintext += elem.text + if elem.tail: + plaintext += elem.tail + + if length > 0 and len(plaintext) > length: + break + + text = plaintext + + except Exception, e: + logging.warn("getPlaintext: error parsing text! Returning everything. %s"%e) + + if length > 0 and len(text) > length: + # try to not break words + if wordwrap and text[length] not in [' ', '.', '?', '!']: + # search the last blank + length = text.rfind(' ', 0, length) + + return text[:length] + '...' + + return text + def serialize(node): """returns a string containing an XML snippet of (etree) node""" @@ -127,10 +199,15 @@ def getMonthName(mon, lang): """returns the name of the month mon in the language lang""" - return map_months[lang][mon] + return map_months[lang.lower()][mon] -def getDateString(date=None, lang='en', withYear=True, monthNames=True, abbrev=False): +def getWeekdayName(day, lang, short=True): + """returns the name of the weekday day in the language lang""" + return map_weekdays_short[lang.lower()][day] + + +def getDateString(date=None, lang='en', short=False, withYear=True, monthNames=True, abbrev=False): """Return formatted date string.""" if date is None: return None @@ -148,14 +225,20 @@ year = date.year if lang.lower() == 'en': - ds = "%s %s"%(getMonthName(month, lang), day) - if withYear: - ds += ", %s"%year + if short: + ds = "%s/%s/%s"%(year,month,day) + else: + ds = "%s %s"%(getMonthName(month, lang), day) + if withYear: + ds += ", %s"%year elif lang.lower() == 'de': - ds = "%s. %s"%(day, getMonthName(month, lang)) - if withYear: - ds += " %s"%year + if short: + ds = "%s.%s.%s"%(day,month,year) + else: + ds = "%s. %s"%(day, getMonthName(month, lang)) + if withYear: + ds += " %s"%year elif lang.lower() == 'iso': ds = date.isoformat() @@ -205,7 +288,7 @@ return data except httplib2.HttpLib2Error, e: - logging.error("getHttp(lib2)Data: HTTP error(%s): %s"%(e.code,e)) + logging.error("getHttp(lib2)Data: HTTP error(%s): %s"%(getattr(e, 'code','?'),e)) errmsg = str(e) # stop trying break @@ -285,30 +368,3 @@ return s -def getBrowserType(self): - """(legacy) check the browsers request to find out the browser type""" - bt = {} - ua = self.REQUEST.get_header("HTTP_USER_AGENT") - bt['ua'] = ua - bt['isIE'] = False - bt['isN4'] = False - if string.find(ua, 'MSIE') > -1: - bt['isIE'] = True - else: - bt['isN4'] = (string.find(ua, 'Mozilla/4.') > -1) - - try: - nav = ua[string.find(ua, '('):] - ie = string.split(nav, "; ")[1] - if string.find(ie, "MSIE") > -1: - bt['versIE'] = string.split(ie, " ")[1] - except: pass - - bt['isMac'] = string.find(ua, 'Macintosh') > -1 - bt['isWin'] = string.find(ua, 'Windows') > -1 - bt['isIEWin'] = bt['isIE'] and bt['isWin'] - bt['isIEMac'] = bt['isIE'] and bt['isMac'] - bt['staticHTML'] = False - - return bt - diff -r 79973dee60bd -r a71ae589d342 zpt/viewer/layer_text_annotator.zpt --- a/zpt/viewer/layer_text_annotator.zpt Mon Nov 24 14:40:00 2014 +0100 +++ b/zpt/viewer/layer_text_annotator.zpt Thu Dec 11 17:19:45 2014 +0100 @@ -27,7 +27,7 @@