comparison SrvTxtUtils.py @ 628:447251b5af65

make sure utf8ify and unicodify output text.
author casties
date Wed, 11 Feb 2015 17:18:23 +0100
parents 6012fe93f78c
children
comparison
equal deleted inserted replaced
627:98fe8e168c01 628:447251b5af65
20 import urllib2 20 import urllib2
21 httplib = 'urllib2' 21 httplib = 'urllib2'
22 22
23 import xml.etree.ElementTree as ET 23 import xml.etree.ElementTree as ET
24 24
25 srvTxtUtilsVersion = "1.13" 25 srvTxtUtilsVersion = "1.13.1"
26 26
27 map_months = {'en': [u"", 27 map_months = {'en': [u"",
28 u"January", 28 u"January",
29 u"February", 29 u"February",
30 u"March", 30 u"March",
92 if isinstance(s, str): 92 if isinstance(s, str):
93 try: 93 try:
94 return s.decode('utf-8') 94 return s.decode('utf-8')
95 except: 95 except:
96 return s.decode('latin-1') 96 return s.decode('latin-1')
97 elif isinstance(s, unicode):
98 return s
97 else: 99 else:
98 return s 100 return unicode(s)
99 101
100 def utf8ify(s): 102 def utf8ify(s):
101 """encode unicode object or string into byte string in utf-8 representation. 103 """encode unicode object or string into byte string in utf-8 representation.
102 assumes string objects to be utf-8""" 104 assumes string objects to be utf-8"""
103 if not s: 105 if not s:
104 return "" 106 return ""
105 if isinstance(s, str): 107 if isinstance(s, unicode):
108 return s.encode('utf-8')
109 elif isinstance(s, str):
106 return s 110 return s
107 else: 111 else:
108 return s.encode('utf-8') 112 return str(s)
109 113
110 114
111 def getTextFromNode(node, recursive=False, length=0): 115 def getTextFromNode(node, recursive=False, length=0):
112 """Return all text content of a (etree) node. 116 """Return all text content of a (etree) node.
113 117