annotate SrvTxtUtils.py @ 458:48b135b089c8 elementtree

more renovation
author casties
date Tue, 19 Jul 2011 20:46:35 +0200
parents
children 19bd41d95f62
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
458
48b135b089c8 more renovation
casties
parents:
diff changeset
1 """Utility methods for handling XML, reading HTTP, etc"""
48b135b089c8 more renovation
casties
parents:
diff changeset
2
48b135b089c8 more renovation
casties
parents:
diff changeset
3 import sys
48b135b089c8 more renovation
casties
parents:
diff changeset
4 import urllib
48b135b089c8 more renovation
casties
parents:
diff changeset
5 import urllib2
48b135b089c8 more renovation
casties
parents:
diff changeset
6 import logging
48b135b089c8 more renovation
casties
parents:
diff changeset
7
48b135b089c8 more renovation
casties
parents:
diff changeset
8
48b135b089c8 more renovation
casties
parents:
diff changeset
9 srvTxtUtilsVersion = "1.0"
48b135b089c8 more renovation
casties
parents:
diff changeset
10
48b135b089c8 more renovation
casties
parents:
diff changeset
11 def getInt(number, default=0):
48b135b089c8 more renovation
casties
parents:
diff changeset
12 """returns always an int (0 in case of problems)"""
48b135b089c8 more renovation
casties
parents:
diff changeset
13 try:
48b135b089c8 more renovation
casties
parents:
diff changeset
14 return int(number)
48b135b089c8 more renovation
casties
parents:
diff changeset
15 except:
48b135b089c8 more renovation
casties
parents:
diff changeset
16 return int(default)
48b135b089c8 more renovation
casties
parents:
diff changeset
17
48b135b089c8 more renovation
casties
parents:
diff changeset
18 def getText(node):
48b135b089c8 more renovation
casties
parents:
diff changeset
19 """returns all text content of a node and its subnodes"""
48b135b089c8 more renovation
casties
parents:
diff changeset
20 if node is None:
48b135b089c8 more renovation
casties
parents:
diff changeset
21 return ""
48b135b089c8 more renovation
casties
parents:
diff changeset
22 # ElementTree:
48b135b089c8 more renovation
casties
parents:
diff changeset
23 text = node.text or ""
48b135b089c8 more renovation
casties
parents:
diff changeset
24 for e in node:
48b135b089c8 more renovation
casties
parents:
diff changeset
25 text += gettext(e)
48b135b089c8 more renovation
casties
parents:
diff changeset
26 if e.tail:
48b135b089c8 more renovation
casties
parents:
diff changeset
27 text += e.tail
48b135b089c8 more renovation
casties
parents:
diff changeset
28
48b135b089c8 more renovation
casties
parents:
diff changeset
29 # 4Suite:
48b135b089c8 more renovation
casties
parents:
diff changeset
30 #nodelist=node.childNodes
48b135b089c8 more renovation
casties
parents:
diff changeset
31 #text = ""
48b135b089c8 more renovation
casties
parents:
diff changeset
32 #for n in nodelist:
48b135b089c8 more renovation
casties
parents:
diff changeset
33 # if n.nodeType == node.TEXT_NODE:
48b135b089c8 more renovation
casties
parents:
diff changeset
34 # text = text + n.data
48b135b089c8 more renovation
casties
parents:
diff changeset
35
48b135b089c8 more renovation
casties
parents:
diff changeset
36 return text
48b135b089c8 more renovation
casties
parents:
diff changeset
37
48b135b089c8 more renovation
casties
parents:
diff changeset
38
48b135b089c8 more renovation
casties
parents:
diff changeset
39
48b135b089c8 more renovation
casties
parents:
diff changeset
40 def getHttpData(url, data=None, num_tries=3, timeout=10):
48b135b089c8 more renovation
casties
parents:
diff changeset
41 """returns result from url+data HTTP request"""
48b135b089c8 more renovation
casties
parents:
diff changeset
42 # we do GET (by appending data to url)
48b135b089c8 more renovation
casties
parents:
diff changeset
43 if isinstance(data, str) or isinstance(data, unicode):
48b135b089c8 more renovation
casties
parents:
diff changeset
44 # if data is string then append
48b135b089c8 more renovation
casties
parents:
diff changeset
45 url = "%s?%s"%(url,data)
48b135b089c8 more renovation
casties
parents:
diff changeset
46 elif isinstance(data, dict) or isinstance(data, list) or isinstance(data, tuple):
48b135b089c8 more renovation
casties
parents:
diff changeset
47 # urlencode
48b135b089c8 more renovation
casties
parents:
diff changeset
48 url = "%s?%s"%(url,urllib.urlencode(data))
48b135b089c8 more renovation
casties
parents:
diff changeset
49
48b135b089c8 more renovation
casties
parents:
diff changeset
50 response = None
48b135b089c8 more renovation
casties
parents:
diff changeset
51 errmsg = None
48b135b089c8 more renovation
casties
parents:
diff changeset
52 for cnt in range(num_tries):
48b135b089c8 more renovation
casties
parents:
diff changeset
53 try:
48b135b089c8 more renovation
casties
parents:
diff changeset
54 logging.debug("getHttpData(#%s %ss) url=%s"%(cnt+1,timeout,url))
48b135b089c8 more renovation
casties
parents:
diff changeset
55 if sys.version_info < (2, 6):
48b135b089c8 more renovation
casties
parents:
diff changeset
56 # set timeout on socket -- ugly :-(
48b135b089c8 more renovation
casties
parents:
diff changeset
57 import socket
48b135b089c8 more renovation
casties
parents:
diff changeset
58 socket.setdefaulttimeout(float(timeout))
48b135b089c8 more renovation
casties
parents:
diff changeset
59 response = urllib2.urlopen(url)
48b135b089c8 more renovation
casties
parents:
diff changeset
60 else:
48b135b089c8 more renovation
casties
parents:
diff changeset
61 # timeout as parameter
48b135b089c8 more renovation
casties
parents:
diff changeset
62 response = urllib2.urlopen(url,timeout=float(timeout))
48b135b089c8 more renovation
casties
parents:
diff changeset
63 # check result?
48b135b089c8 more renovation
casties
parents:
diff changeset
64 break
48b135b089c8 more renovation
casties
parents:
diff changeset
65 except urllib2.HTTPError, e:
48b135b089c8 more renovation
casties
parents:
diff changeset
66 logging.error("getHttpData: HTTP error(%s): %s"%(e.code,e))
48b135b089c8 more renovation
casties
parents:
diff changeset
67 errmsg = str(e)
48b135b089c8 more renovation
casties
parents:
diff changeset
68 # stop trying
48b135b089c8 more renovation
casties
parents:
diff changeset
69 break
48b135b089c8 more renovation
casties
parents:
diff changeset
70 except urllib2.URLError, e:
48b135b089c8 more renovation
casties
parents:
diff changeset
71 logging.error("getHttpData: URLLIB error(%s): %s"%(e.reason,e))
48b135b089c8 more renovation
casties
parents:
diff changeset
72 errmsg = str(e)
48b135b089c8 more renovation
casties
parents:
diff changeset
73 # stop trying
48b135b089c8 more renovation
casties
parents:
diff changeset
74 #break
48b135b089c8 more renovation
casties
parents:
diff changeset
75
48b135b089c8 more renovation
casties
parents:
diff changeset
76 if response is not None:
48b135b089c8 more renovation
casties
parents:
diff changeset
77 data = response.read()
48b135b089c8 more renovation
casties
parents:
diff changeset
78 response.close()
48b135b089c8 more renovation
casties
parents:
diff changeset
79 return data
48b135b089c8 more renovation
casties
parents:
diff changeset
80
48b135b089c8 more renovation
casties
parents:
diff changeset
81 raise IOError("ERROR fetching HTTP data from %s: %s"%(url,errmsg))
48b135b089c8 more renovation
casties
parents:
diff changeset
82 #return None
48b135b089c8 more renovation
casties
parents:
diff changeset
83