annotate SrvTxtUtils.py @ 585:83eeed69793f

new annotator layer for images.
author casties
date Tue, 13 Nov 2012 17:33:34 +0100
parents 2fe04b61ed95
children c57d80a649ea
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
585
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
14 srvTxtUtilsVersion = "1.6"
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
514
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
30 def unicodify(s):
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
31 """decode str (utf-8 or latin-1 representation) into unicode object"""
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
32 if not s:
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
33 return u""
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
34 if isinstance(s, str):
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
35 try:
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
36 return s.decode('utf-8')
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
37 except:
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
38 return s.decode('latin-1')
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
39 else:
553
2fe04b61ed95 make sure unicodify and utf8ify return str and unicode.
casties
parents: 546
diff changeset
40 return unicode(s)
514
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
41
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
42 def utf8ify(s):
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
43 """encode unicode object or string into byte string in utf-8 representation.
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
44 assumes string objects to be utf-8"""
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
45 if not s:
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
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
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
49 else:
553
2fe04b61ed95 make sure unicodify and utf8ify return str and unicode.
casties
parents: 546
diff changeset
50 return str(s)
514
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
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
48b135b089c8 more renovation
casties
parents:
diff changeset
53 """returns all text content of a node and its subnodes"""
48b135b089c8 more renovation
casties
parents:
diff changeset
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
48b135b089c8 more renovation
casties
parents:
diff changeset
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
48b135b089c8 more renovation
casties
parents:
diff changeset
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
48b135b089c8 more renovation
casties
parents:
diff changeset
64 if e.tail:
48b135b089c8 more renovation
casties
parents:
diff changeset
65 text += e.tail
48b135b089c8 more renovation
casties
parents:
diff changeset
66
48b135b089c8 more renovation
casties
parents:
diff changeset
67 # 4Suite:
48b135b089c8 more renovation
casties
parents:
diff changeset
68 #nodelist=node.childNodes
48b135b089c8 more renovation
casties
parents:
diff changeset
69 #text = ""
48b135b089c8 more renovation
casties
parents:
diff changeset
70 #for n in nodelist:
48b135b089c8 more renovation
casties
parents:
diff changeset
71 # if n.nodeType == node.TEXT_NODE:
48b135b089c8 more renovation
casties
parents:
diff changeset
72 # text = text + n.data
48b135b089c8 more renovation
casties
parents:
diff changeset
73
48b135b089c8 more renovation
casties
parents:
diff changeset
74 return text
48b135b089c8 more renovation
casties
parents:
diff changeset
75
48b135b089c8 more renovation
casties
parents:
diff changeset
76
48b135b089c8 more renovation
casties
parents:
diff changeset
77
546
2928037f9a75 ASSIGNED - # 249: Annotations shared in groups
casties
parents: 528
diff changeset
78 def getHttpData(url, data=None, num_tries=3, timeout=10, noExceptions=False):
458
48b135b089c8 more renovation
casties
parents:
diff changeset
79 """returns result from url+data HTTP request"""
48b135b089c8 more renovation
casties
parents:
diff changeset
80 # we do GET (by appending data to url)
48b135b089c8 more renovation
casties
parents:
diff changeset
81 if isinstance(data, str) or isinstance(data, unicode):
48b135b089c8 more renovation
casties
parents:
diff changeset
82 # if data is string then append
48b135b089c8 more renovation
casties
parents:
diff changeset
83 url = "%s?%s"%(url,data)
48b135b089c8 more renovation
casties
parents:
diff changeset
84 elif isinstance(data, dict) or isinstance(data, list) or isinstance(data, tuple):
48b135b089c8 more renovation
casties
parents:
diff changeset
85 # urlencode
48b135b089c8 more renovation
casties
parents:
diff changeset
86 url = "%s?%s"%(url,urllib.urlencode(data))
48b135b089c8 more renovation
casties
parents:
diff changeset
87
48b135b089c8 more renovation
casties
parents:
diff changeset
88 response = None
48b135b089c8 more renovation
casties
parents:
diff changeset
89 errmsg = None
48b135b089c8 more renovation
casties
parents:
diff changeset
90 for cnt in range(num_tries):
48b135b089c8 more renovation
casties
parents:
diff changeset
91 try:
48b135b089c8 more renovation
casties
parents:
diff changeset
92 logging.debug("getHttpData(#%s %ss) url=%s"%(cnt+1,timeout,url))
48b135b089c8 more renovation
casties
parents:
diff changeset
93 if sys.version_info < (2, 6):
48b135b089c8 more renovation
casties
parents:
diff changeset
94 # set timeout on socket -- ugly :-(
48b135b089c8 more renovation
casties
parents:
diff changeset
95 import socket
48b135b089c8 more renovation
casties
parents:
diff changeset
96 socket.setdefaulttimeout(float(timeout))
48b135b089c8 more renovation
casties
parents:
diff changeset
97 response = urllib2.urlopen(url)
48b135b089c8 more renovation
casties
parents:
diff changeset
98 else:
48b135b089c8 more renovation
casties
parents:
diff changeset
99 # timeout as parameter
48b135b089c8 more renovation
casties
parents:
diff changeset
100 response = urllib2.urlopen(url,timeout=float(timeout))
48b135b089c8 more renovation
casties
parents:
diff changeset
101 # check result?
48b135b089c8 more renovation
casties
parents:
diff changeset
102 break
48b135b089c8 more renovation
casties
parents:
diff changeset
103 except urllib2.HTTPError, e:
48b135b089c8 more renovation
casties
parents:
diff changeset
104 logging.error("getHttpData: HTTP error(%s): %s"%(e.code,e))
48b135b089c8 more renovation
casties
parents:
diff changeset
105 errmsg = str(e)
48b135b089c8 more renovation
casties
parents:
diff changeset
106 # stop trying
48b135b089c8 more renovation
casties
parents:
diff changeset
107 break
48b135b089c8 more renovation
casties
parents:
diff changeset
108 except urllib2.URLError, e:
48b135b089c8 more renovation
casties
parents:
diff changeset
109 logging.error("getHttpData: URLLIB error(%s): %s"%(e.reason,e))
48b135b089c8 more renovation
casties
parents:
diff changeset
110 errmsg = str(e)
48b135b089c8 more renovation
casties
parents:
diff changeset
111 # stop trying
48b135b089c8 more renovation
casties
parents:
diff changeset
112 #break
48b135b089c8 more renovation
casties
parents:
diff changeset
113
48b135b089c8 more renovation
casties
parents:
diff changeset
114 if response is not None:
48b135b089c8 more renovation
casties
parents:
diff changeset
115 data = response.read()
48b135b089c8 more renovation
casties
parents:
diff changeset
116 response.close()
48b135b089c8 more renovation
casties
parents:
diff changeset
117 return data
48b135b089c8 more renovation
casties
parents:
diff changeset
118
546
2928037f9a75 ASSIGNED - # 249: Annotations shared in groups
casties
parents: 528
diff changeset
119 if noExceptions:
2928037f9a75 ASSIGNED - # 249: Annotations shared in groups
casties
parents: 528
diff changeset
120 return None
2928037f9a75 ASSIGNED - # 249: Annotations shared in groups
casties
parents: 528
diff changeset
121
458
48b135b089c8 more renovation
casties
parents:
diff changeset
122 raise IOError("ERROR fetching HTTP data from %s: %s"%(url,errmsg))
48b135b089c8 more renovation
casties
parents:
diff changeset
123 #return None
48b135b089c8 more renovation
casties
parents:
diff changeset
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
585
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
135
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
136 def getBrowserType(self):
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
137 """check the browsers request to find out the browser type"""
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
138 bt = {}
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
139 ua = self.REQUEST.get_header("HTTP_USER_AGENT")
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
140 bt['ua'] = ua
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
141 bt['isIE'] = False
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
142 bt['isN4'] = False
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
143 if string.find(ua, 'MSIE') > -1:
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
144 bt['isIE'] = True
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
145 else:
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
146 bt['isN4'] = (string.find(ua, 'Mozilla/4.') > -1)
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
147
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
148 try:
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
149 nav = ua[string.find(ua, '('):]
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
150 ie = string.split(nav, "; ")[1]
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
151 if string.find(ie, "MSIE") > -1:
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
152 bt['versIE'] = string.split(ie, " ")[1]
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
153 except: pass
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
154
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
155 bt['isMac'] = string.find(ua, 'Macintosh') > -1
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
156 bt['isWin'] = string.find(ua, 'Windows') > -1
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
157 bt['isIEWin'] = bt['isIE'] and bt['isWin']
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
158 bt['isIEMac'] = bt['isIE'] and bt['isMac']
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
159 bt['staticHTML'] = False
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
160
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
161 return bt
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
162
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
163