Mercurial > hg > documentViewer
annotate SrvTxtUtils.py @ 580:0806cb9061c1
fixed another bug when url starts with /mpiwg/online/
author | casties |
---|---|
date | Mon, 29 Oct 2012 11:57:30 +0100 |
parents | 2fe04b61ed95 |
children | 83eeed69793f |
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 | |
553
2fe04b61ed95
make sure unicodify and utf8ify return str and unicode.
casties
parents:
546
diff
changeset
|
14 srvTxtUtilsVersion = "1.5.1" |
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 |
514 | 30 def unicodify(s): |
31 """decode str (utf-8 or latin-1 representation) into unicode object""" | |
32 if not s: | |
33 return u"" | |
34 if isinstance(s, str): | |
35 try: | |
36 return s.decode('utf-8') | |
37 except: | |
38 return s.decode('latin-1') | |
39 else: | |
553
2fe04b61ed95
make sure unicodify and utf8ify return str and unicode.
casties
parents:
546
diff
changeset
|
40 return unicode(s) |
514 | 41 |
42 def utf8ify(s): | |
43 """encode unicode object or string into byte string in utf-8 representation. | |
44 assumes string objects to be utf-8""" | |
45 if not s: | |
46 return "" | |
553
2fe04b61ed95
make sure unicodify and utf8ify return str and unicode.
casties
parents:
546
diff
changeset
|
47 if isinstance(s, unicode): |
2fe04b61ed95
make sure unicodify and utf8ify return str and unicode.
casties
parents:
546
diff
changeset
|
48 return s.encode('utf-8') |
514 | 49 else: |
553
2fe04b61ed95
make sure unicodify and utf8ify return str and unicode.
casties
parents:
546
diff
changeset
|
50 return str(s) |
514 | 51 |
490
6f116b86a226
more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents:
464
diff
changeset
|
52 def getText(node, recursive=0): |
458 | 53 """returns all text content of a node and its subnodes""" |
54 if node is None: | |
490
6f116b86a226
more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents:
464
diff
changeset
|
55 return '' |
6f116b86a226
more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents:
464
diff
changeset
|
56 |
458 | 57 # ElementTree: |
490
6f116b86a226
more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents:
464
diff
changeset
|
58 text = node.text or '' |
458 | 59 for e in node: |
490
6f116b86a226
more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents:
464
diff
changeset
|
60 if recursive: |
6f116b86a226
more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents:
464
diff
changeset
|
61 text += getText(e) |
6f116b86a226
more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents:
464
diff
changeset
|
62 else: |
6f116b86a226
more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents:
464
diff
changeset
|
63 text += e.text or '' |
458 | 64 if e.tail: |
65 text += e.tail | |
66 | |
67 # 4Suite: | |
68 #nodelist=node.childNodes | |
69 #text = "" | |
70 #for n in nodelist: | |
71 # if n.nodeType == node.TEXT_NODE: | |
72 # text = text + n.data | |
73 | |
74 return text | |
75 | |
76 | |
77 | |
546 | 78 def getHttpData(url, data=None, num_tries=3, timeout=10, noExceptions=False): |
458 | 79 """returns result from url+data HTTP request""" |
80 # we do GET (by appending data to url) | |
81 if isinstance(data, str) or isinstance(data, unicode): | |
82 # if data is string then append | |
83 url = "%s?%s"%(url,data) | |
84 elif isinstance(data, dict) or isinstance(data, list) or isinstance(data, tuple): | |
85 # urlencode | |
86 url = "%s?%s"%(url,urllib.urlencode(data)) | |
87 | |
88 response = None | |
89 errmsg = None | |
90 for cnt in range(num_tries): | |
91 try: | |
92 logging.debug("getHttpData(#%s %ss) url=%s"%(cnt+1,timeout,url)) | |
93 if sys.version_info < (2, 6): | |
94 # set timeout on socket -- ugly :-( | |
95 import socket | |
96 socket.setdefaulttimeout(float(timeout)) | |
97 response = urllib2.urlopen(url) | |
98 else: | |
99 # timeout as parameter | |
100 response = urllib2.urlopen(url,timeout=float(timeout)) | |
101 # check result? | |
102 break | |
103 except urllib2.HTTPError, e: | |
104 logging.error("getHttpData: HTTP error(%s): %s"%(e.code,e)) | |
105 errmsg = str(e) | |
106 # stop trying | |
107 break | |
108 except urllib2.URLError, e: | |
109 logging.error("getHttpData: URLLIB error(%s): %s"%(e.reason,e)) | |
110 errmsg = str(e) | |
111 # stop trying | |
112 #break | |
113 | |
114 if response is not None: | |
115 data = response.read() | |
116 response.close() | |
117 return data | |
118 | |
546 | 119 if noExceptions: |
120 return None | |
121 | |
458 | 122 raise IOError("ERROR fetching HTTP data from %s: %s"%(url,errmsg)) |
123 #return None | |
124 | |
490
6f116b86a226
more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents:
464
diff
changeset
|
125 |
6f116b86a226
more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents:
464
diff
changeset
|
126 def refreshingImageFileIndexHtml(self, REQUEST, RESPONSE): |
6f116b86a226
more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents:
464
diff
changeset
|
127 """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
|
128 stat_info = os.stat(self.path) |
6f116b86a226
more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents:
464
diff
changeset
|
129 self.size = stat_info[stat.ST_SIZE] |
6f116b86a226
more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents:
464
diff
changeset
|
130 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
|
131 self.lmh = rfc1123_date(self.lmt) |
6f116b86a226
more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents:
464
diff
changeset
|
132 # call original method |
6f116b86a226
more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents:
464
diff
changeset
|
133 return ImageFile.index_html(self, REQUEST, RESPONSE) |
6f116b86a226
more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents:
464
diff
changeset
|
134 |