Mercurial > hg > documentViewer
comparison SrvTxtUtils.py @ 553:2fe04b61ed95
make sure unicodify and utf8ify return str and unicode.
| author | casties |
|---|---|
| date | Mon, 24 Sep 2012 16:24:22 +0200 |
| parents | 2928037f9a75 |
| children | 83eeed69793f |
comparison
equal
deleted
inserted
replaced
| 552:13eb36c0b00c | 553:2fe04b61ed95 |
|---|---|
| 9 import urllib | 9 import urllib |
| 10 import urllib2 | 10 import urllib2 |
| 11 import logging | 11 import logging |
| 12 | 12 |
| 13 | 13 |
| 14 srvTxtUtilsVersion = "1.5" | 14 srvTxtUtilsVersion = "1.5.1" |
| 15 | 15 |
| 16 def getInt(number, default=0): | 16 def getInt(number, default=0): |
| 17 """returns always an int (0 in case of problems)""" | 17 """returns always an int (0 in case of problems)""" |
| 18 try: | 18 try: |
| 19 return int(number) | 19 return int(number) |
| 35 try: | 35 try: |
| 36 return s.decode('utf-8') | 36 return s.decode('utf-8') |
| 37 except: | 37 except: |
| 38 return s.decode('latin-1') | 38 return s.decode('latin-1') |
| 39 else: | 39 else: |
| 40 return s | 40 return unicode(s) |
| 41 | 41 |
| 42 def utf8ify(s): | 42 def utf8ify(s): |
| 43 """encode unicode object or string into byte string in utf-8 representation. | 43 """encode unicode object or string into byte string in utf-8 representation. |
| 44 assumes string objects to be utf-8""" | 44 assumes string objects to be utf-8""" |
| 45 if not s: | 45 if not s: |
| 46 return "" | 46 return "" |
| 47 if isinstance(s, str): | 47 if isinstance(s, unicode): |
| 48 return s | 48 return s.encode('utf-8') |
| 49 else: | 49 else: |
| 50 return s.encode('utf-8') | 50 return str(s) |
| 51 | 51 |
| 52 def getText(node, recursive=0): | 52 def getText(node, recursive=0): |
| 53 """returns all text content of a node and its subnodes""" | 53 """returns all text content of a node and its subnodes""" |
| 54 if node is None: | 54 if node is None: |
| 55 return '' | 55 return '' |
