--- MPIWGWeb/Attic/SrvTxtUtils.py 2012/03/22 13:30:56 1.1.2.1 +++ MPIWGWeb/Attic/SrvTxtUtils.py 2012/03/23 15:27:05 1.1.2.2 @@ -11,7 +11,7 @@ import urllib2 import logging -srvTxtUtilsVersion = "1.3" +srvTxtUtilsVersion = "1.5" def getInt(number, default=0): """returns always an int (0 in case of problems)""" @@ -27,6 +27,28 @@ def getAt(array, idx, default=None): except: return default +def unicodify(s): + """decode str (utf-8 or latin-1 representation) into unicode object""" + if not s: + return u"" + if isinstance(s, str): + try: + return s.decode('utf-8') + except: + return s.decode('latin-1') + else: + return s + +def utf8ify(s): + """encode unicode object or string into byte string in utf-8 representation. + assumes string objects to be utf-8""" + if not s: + return "" + if isinstance(s, str): + return s + else: + return s.encode('utf-8') + def getText(node, recursive=0): """returns all text content of a node and its subnodes""" if node is None: @@ -53,7 +75,7 @@ def getText(node, recursive=0): -def getHttpData(url, data=None, num_tries=3, timeout=10): +def getHttpData(url, data=None, num_tries=3, timeout=10, noExceptions=False): """returns result from url+data HTTP request""" # we do GET (by appending data to url) if isinstance(data, str) or isinstance(data, unicode): @@ -94,6 +116,9 @@ def getHttpData(url, data=None, num_trie response.close() return data + if noExceptions: + return None + raise IOError("ERROR fetching HTTP data from %s: %s"%(url,errmsg)) #return None