annotate SrvTxtUtils.py @ 490:6f116b86a226 elementtree

more new template stuff. moved ImageFile index method to SrvTxtUtils
author casties
date Mon, 29 Aug 2011 16:39:50 +0200
parents 19bd41d95f62
children c55e376be01b
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
490
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
3 from App.ImageFile import ImageFile
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
4 from App.Common import rfc1123_date
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
5
458
48b135b089c8 more renovation
casties
parents:
diff changeset
6 import sys
490
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
7 import os
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
8 import stat
458
48b135b089c8 more renovation
casties
parents:
diff changeset
9 import urllib
48b135b089c8 more renovation
casties
parents:
diff changeset
10 import urllib2
48b135b089c8 more renovation
casties
parents:
diff changeset
11 import logging
48b135b089c8 more renovation
casties
parents:
diff changeset
12
48b135b089c8 more renovation
casties
parents:
diff changeset
13
490
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
14 srvTxtUtilsVersion = "1.3"
458
48b135b089c8 more renovation
casties
parents:
diff changeset
15
48b135b089c8 more renovation
casties
parents:
diff changeset
16 def getInt(number, default=0):
48b135b089c8 more renovation
casties
parents:
diff changeset
17 """returns always an int (0 in case of problems)"""
48b135b089c8 more renovation
casties
parents:
diff changeset
18 try:
48b135b089c8 more renovation
casties
parents:
diff changeset
19 return int(number)
48b135b089c8 more renovation
casties
parents:
diff changeset
20 except:
48b135b089c8 more renovation
casties
parents:
diff changeset
21 return int(default)
48b135b089c8 more renovation
casties
parents:
diff changeset
22
490
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
23 def getAt(array, idx, default=None):
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
24 """returns element idx from array or default (in case of problems)"""
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
25 try:
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
26 return array[idx]
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
27 except:
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
28 return default
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
29
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
30 def getText(node, recursive=0):
458
48b135b089c8 more renovation
casties
parents:
diff changeset
31 """returns all text content of a node and its subnodes"""
48b135b089c8 more renovation
casties
parents:
diff changeset
32 if node is None:
490
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
33 return ''
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
34
458
48b135b089c8 more renovation
casties
parents:
diff changeset
35 # ElementTree:
490
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
36 text = node.text or ''
458
48b135b089c8 more renovation
casties
parents:
diff changeset
37 for e in node:
490
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
38 if recursive:
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
39 text += getText(e)
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
40 else:
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
41 text += e.text or ''
458
48b135b089c8 more renovation
casties
parents:
diff changeset
42 if e.tail:
48b135b089c8 more renovation
casties
parents:
diff changeset
43 text += e.tail
48b135b089c8 more renovation
casties
parents:
diff changeset
44
48b135b089c8 more renovation
casties
parents:
diff changeset
45 # 4Suite:
48b135b089c8 more renovation
casties
parents:
diff changeset
46 #nodelist=node.childNodes
48b135b089c8 more renovation
casties
parents:
diff changeset
47 #text = ""
48b135b089c8 more renovation
casties
parents:
diff changeset
48 #for n in nodelist:
48b135b089c8 more renovation
casties
parents:
diff changeset
49 # if n.nodeType == node.TEXT_NODE:
48b135b089c8 more renovation
casties
parents:
diff changeset
50 # text = text + n.data
48b135b089c8 more renovation
casties
parents:
diff changeset
51
48b135b089c8 more renovation
casties
parents:
diff changeset
52 return text
48b135b089c8 more renovation
casties
parents:
diff changeset
53
48b135b089c8 more renovation
casties
parents:
diff changeset
54
48b135b089c8 more renovation
casties
parents:
diff changeset
55
48b135b089c8 more renovation
casties
parents:
diff changeset
56 def getHttpData(url, data=None, num_tries=3, timeout=10):
48b135b089c8 more renovation
casties
parents:
diff changeset
57 """returns result from url+data HTTP request"""
48b135b089c8 more renovation
casties
parents:
diff changeset
58 # we do GET (by appending data to url)
48b135b089c8 more renovation
casties
parents:
diff changeset
59 if isinstance(data, str) or isinstance(data, unicode):
48b135b089c8 more renovation
casties
parents:
diff changeset
60 # if data is string then append
48b135b089c8 more renovation
casties
parents:
diff changeset
61 url = "%s?%s"%(url,data)
48b135b089c8 more renovation
casties
parents:
diff changeset
62 elif isinstance(data, dict) or isinstance(data, list) or isinstance(data, tuple):
48b135b089c8 more renovation
casties
parents:
diff changeset
63 # urlencode
48b135b089c8 more renovation
casties
parents:
diff changeset
64 url = "%s?%s"%(url,urllib.urlencode(data))
48b135b089c8 more renovation
casties
parents:
diff changeset
65
48b135b089c8 more renovation
casties
parents:
diff changeset
66 response = None
48b135b089c8 more renovation
casties
parents:
diff changeset
67 errmsg = None
48b135b089c8 more renovation
casties
parents:
diff changeset
68 for cnt in range(num_tries):
48b135b089c8 more renovation
casties
parents:
diff changeset
69 try:
48b135b089c8 more renovation
casties
parents:
diff changeset
70 logging.debug("getHttpData(#%s %ss) url=%s"%(cnt+1,timeout,url))
48b135b089c8 more renovation
casties
parents:
diff changeset
71 if sys.version_info < (2, 6):
48b135b089c8 more renovation
casties
parents:
diff changeset
72 # set timeout on socket -- ugly :-(
48b135b089c8 more renovation
casties
parents:
diff changeset
73 import socket
48b135b089c8 more renovation
casties
parents:
diff changeset
74 socket.setdefaulttimeout(float(timeout))
48b135b089c8 more renovation
casties
parents:
diff changeset
75 response = urllib2.urlopen(url)
48b135b089c8 more renovation
casties
parents:
diff changeset
76 else:
48b135b089c8 more renovation
casties
parents:
diff changeset
77 # timeout as parameter
48b135b089c8 more renovation
casties
parents:
diff changeset
78 response = urllib2.urlopen(url,timeout=float(timeout))
48b135b089c8 more renovation
casties
parents:
diff changeset
79 # check result?
48b135b089c8 more renovation
casties
parents:
diff changeset
80 break
48b135b089c8 more renovation
casties
parents:
diff changeset
81 except urllib2.HTTPError, e:
48b135b089c8 more renovation
casties
parents:
diff changeset
82 logging.error("getHttpData: HTTP error(%s): %s"%(e.code,e))
48b135b089c8 more renovation
casties
parents:
diff changeset
83 errmsg = str(e)
48b135b089c8 more renovation
casties
parents:
diff changeset
84 # stop trying
48b135b089c8 more renovation
casties
parents:
diff changeset
85 break
48b135b089c8 more renovation
casties
parents:
diff changeset
86 except urllib2.URLError, e:
48b135b089c8 more renovation
casties
parents:
diff changeset
87 logging.error("getHttpData: URLLIB error(%s): %s"%(e.reason,e))
48b135b089c8 more renovation
casties
parents:
diff changeset
88 errmsg = str(e)
48b135b089c8 more renovation
casties
parents:
diff changeset
89 # stop trying
48b135b089c8 more renovation
casties
parents:
diff changeset
90 #break
48b135b089c8 more renovation
casties
parents:
diff changeset
91
48b135b089c8 more renovation
casties
parents:
diff changeset
92 if response is not None:
48b135b089c8 more renovation
casties
parents:
diff changeset
93 data = response.read()
48b135b089c8 more renovation
casties
parents:
diff changeset
94 response.close()
48b135b089c8 more renovation
casties
parents:
diff changeset
95 return data
48b135b089c8 more renovation
casties
parents:
diff changeset
96
48b135b089c8 more renovation
casties
parents:
diff changeset
97 raise IOError("ERROR fetching HTTP data from %s: %s"%(url,errmsg))
48b135b089c8 more renovation
casties
parents:
diff changeset
98 #return None
48b135b089c8 more renovation
casties
parents:
diff changeset
99
490
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
100
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
101 def refreshingImageFileIndexHtml(self, REQUEST, RESPONSE):
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
102 """index_html method for App.ImageFile that updates the file info for each request."""
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
103 stat_info = os.stat(self.path)
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
104 self.size = stat_info[stat.ST_SIZE]
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
105 self.lmt = float(stat_info[stat.ST_MTIME]) or time.time()
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
106 self.lmh = rfc1123_date(self.lmt)
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
107 # call original method
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
108 return ImageFile.index_html(self, REQUEST, RESPONSE)
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
109