Mercurial > hg > documentViewer
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 |
rev | line source |
---|---|
458 | 1 """Utility methods for handling XML, reading HTTP, etc""" |
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 | 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 | 9 import urllib |
10 import urllib2 | |
11 import logging | |
12 | |
13 | |
490
6f116b86a226
more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents:
464
diff
changeset
|
14 srvTxtUtilsVersion = "1.3" |
458 | 15 |
16 def getInt(number, default=0): | |
17 """returns always an int (0 in case of problems)""" | |
18 try: | |
19 return int(number) | |
20 except: | |
21 return int(default) | |
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 | 31 """returns all text content of a node and its subnodes""" |
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 | 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 | 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 | 42 if e.tail: |
43 text += e.tail | |
44 | |
45 # 4Suite: | |
46 #nodelist=node.childNodes | |
47 #text = "" | |
48 #for n in nodelist: | |
49 # if n.nodeType == node.TEXT_NODE: | |
50 # text = text + n.data | |
51 | |
52 return text | |
53 | |
54 | |
55 | |
56 def getHttpData(url, data=None, num_tries=3, timeout=10): | |
57 """returns result from url+data HTTP request""" | |
58 # we do GET (by appending data to url) | |
59 if isinstance(data, str) or isinstance(data, unicode): | |
60 # if data is string then append | |
61 url = "%s?%s"%(url,data) | |
62 elif isinstance(data, dict) or isinstance(data, list) or isinstance(data, tuple): | |
63 # urlencode | |
64 url = "%s?%s"%(url,urllib.urlencode(data)) | |
65 | |
66 response = None | |
67 errmsg = None | |
68 for cnt in range(num_tries): | |
69 try: | |
70 logging.debug("getHttpData(#%s %ss) url=%s"%(cnt+1,timeout,url)) | |
71 if sys.version_info < (2, 6): | |
72 # set timeout on socket -- ugly :-( | |
73 import socket | |
74 socket.setdefaulttimeout(float(timeout)) | |
75 response = urllib2.urlopen(url) | |
76 else: | |
77 # timeout as parameter | |
78 response = urllib2.urlopen(url,timeout=float(timeout)) | |
79 # check result? | |
80 break | |
81 except urllib2.HTTPError, e: | |
82 logging.error("getHttpData: HTTP error(%s): %s"%(e.code,e)) | |
83 errmsg = str(e) | |
84 # stop trying | |
85 break | |
86 except urllib2.URLError, e: | |
87 logging.error("getHttpData: URLLIB error(%s): %s"%(e.reason,e)) | |
88 errmsg = str(e) | |
89 # stop trying | |
90 #break | |
91 | |
92 if response is not None: | |
93 data = response.read() | |
94 response.close() | |
95 return data | |
96 | |
97 raise IOError("ERROR fetching HTTP data from %s: %s"%(url,errmsg)) | |
98 #return None | |
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 |