comparison SrvTxtUtils.py @ 613:c57d80a649ea

CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist https://it-dev.mpiwg-berlin.mpg.de/tracs/mpdl-project-software/ticket/281
author casties
date Thu, 17 Oct 2013 16:25:39 +0200
parents 83eeed69793f
children d16da6e739ef
comparison
equal deleted inserted replaced
612:a79e4e4b3e37 613:c57d80a649ea
5 5
6 import sys 6 import sys
7 import os 7 import os
8 import stat 8 import stat
9 import urllib 9 import urllib
10 import urllib2
11 import logging 10 import logging
12 11 import time
13 12 import re
14 srvTxtUtilsVersion = "1.6" 13 import string
14 import datetime
15 try:
16 import httplib2
17 httplib = 'httplib2'
18 except:
19 logging.warn("Unable to import httplib2! Falling back to urllib2!")
20 import urllib2
21 httplib = 'urllib2'
22
23 import xml.etree.ElementTree as ET
24
25 srvTxtUtilsVersion = "1.12.1"
26
27 map_months = {'en': [u"",
28 u"January",
29 u"February",
30 u"March",
31 u"April",
32 u"May",
33 u"June",
34 u"July",
35 u"August",
36 u"September",
37 u"October",
38 u"November",
39 u"December"],
40 'de': [u"",
41 u"Januar",
42 u"Februar",
43 u"M\u00e4rz",
44 u"April",
45 u"Mai",
46 u"Juni",
47 u"Juli",
48 u"August",
49 u"September",
50 u"Oktober",
51 u"November",
52 u"Dezember"]}
15 53
16 def getInt(number, default=0): 54 def getInt(number, default=0):
17 """returns always an int (0 in case of problems)""" 55 """returns always an int (0 in case of problems)"""
18 try: 56 try:
19 return int(number) 57 return int(number)
35 try: 73 try:
36 return s.decode('utf-8') 74 return s.decode('utf-8')
37 except: 75 except:
38 return s.decode('latin-1') 76 return s.decode('latin-1')
39 else: 77 else:
40 return unicode(s) 78 return s
41 79
42 def utf8ify(s): 80 def utf8ify(s):
43 """encode unicode object or string into byte string in utf-8 representation. 81 """encode unicode object or string into byte string in utf-8 representation.
44 assumes string objects to be utf-8""" 82 assumes string objects to be utf-8"""
45 if not s: 83 if not s:
46 return "" 84 return ""
47 if isinstance(s, unicode): 85 if isinstance(s, str):
86 return s
87 else:
48 return s.encode('utf-8') 88 return s.encode('utf-8')
49 else:
50 return str(s)
51 89
52 def getText(node, recursive=0): 90 def getText(node, recursive=0):
53 """returns all text content of a node and its subnodes""" 91 """returns all text content of a (etree) node and its subnodes"""
54 if node is None: 92 if node is None:
55 return '' 93 return ''
56 94
57 # ElementTree: 95 # ElementTree:
58 text = node.text or '' 96 text = node.text or ''
72 # text = text + n.data 110 # text = text + n.data
73 111
74 return text 112 return text
75 113
76 114
77 115 def serialize(node):
78 def getHttpData(url, data=None, num_tries=3, timeout=10, noExceptions=False): 116 """returns a string containing an XML snippet of (etree) node"""
117 s = ET.tostring(node, 'UTF-8')
118 # snip off XML declaration
119 if s.startswith('<?xml'):
120 i = s.find('?>')
121 return s[i+3:]
122
123 return s
124
125
126 def getMonthName(mon, lang):
127 """returns the name of the month mon in the language lang"""
128 return map_months[lang][mon]
129
130
131 def getDateString(date=None, lang='en', withYear=True, monthNames=True, abbrev=False):
132 """Return formatted date string."""
133 if date is None:
134 return None
135
136 ds = None
137 if callable(date.day):
138 # callable members
139 day = date.day()
140 month = date.month()
141 year = date.year()
142 else:
143 # data members
144 day = date.day
145 month = date.month
146 year = date.year
147
148 if lang.lower() == 'en':
149 ds = "%s %s"%(getMonthName(month, lang), day)
150 if withYear:
151 ds += ", %s"%year
152
153 elif lang.lower() == 'de':
154 ds = "%s. %s"%(day, getMonthName(month, lang))
155 if withYear:
156 ds += " %s"%year
157
158 elif lang.lower() == 'iso':
159 ds = date.isoformat()
160
161 return ds
162
163
164 def getDate(date):
165 """return date object from date or datetime date."""
166 if isinstance(date, datetime.datetime):
167 # strip time
168 return date.date()
169
170 return date
171
172
173 def getDatetime(date):
174 """return datetime object from date or datetime date."""
175 if isinstance(date, datetime.date):
176 # add time 0:00
177 return datetime.datetime.combine(date, datetime.time())
178
179 return date
180
181
182 def getHttpData(url, data=None, num_tries=3, timeout=10, username=None, password=None, cache=None, insecure=False, noExceptions=False):
79 """returns result from url+data HTTP request""" 183 """returns result from url+data HTTP request"""
80 # we do GET (by appending data to url) 184 # we do GET (by appending data to url)
81 if isinstance(data, str) or isinstance(data, unicode): 185 if isinstance(data, str) or isinstance(data, unicode):
82 # if data is string then append 186 # if data is string then append
83 url = "%s?%s"%(url,data) 187 url = "%s?%s"%(url,data)
84 elif isinstance(data, dict) or isinstance(data, list) or isinstance(data, tuple): 188 elif isinstance(data, dict) or isinstance(data, list) or isinstance(data, tuple):
85 # urlencode 189 # urlencode
86 url = "%s?%s"%(url,urllib.urlencode(data)) 190 url = "%s?%s"%(url,urllib.urlencode(data))
87 191
88 response = None
89 errmsg = None 192 errmsg = None
90 for cnt in range(num_tries): 193 if httplib == 'httplib2':
91 try: 194 # use httplib2
92 logging.debug("getHttpData(#%s %ss) url=%s"%(cnt+1,timeout,url)) 195 for cnt in range(num_tries):
93 if sys.version_info < (2, 6): 196 try:
94 # set timeout on socket -- ugly :-( 197 logging.debug("getHttp(lib2)Data(#%s %ss) url=%s"%(cnt+1,timeout,url))
95 import socket 198 h = httplib2.Http(cache=cache, timeout=float(timeout), disable_ssl_certificate_validation=insecure)
96 socket.setdefaulttimeout(float(timeout)) 199 if username:
97 response = urllib2.urlopen(url) 200 h.add_credentials(username, password)
98 else: 201
99 # timeout as parameter 202 resp, data = h.request(url)
100 response = urllib2.urlopen(url,timeout=float(timeout)) 203 return data
101 # check result? 204
102 break 205 except httplib2.HttpLib2Error, e:
103 except urllib2.HTTPError, e: 206 logging.error("getHttp(lib2)Data: HTTP error(%s): %s"%(e.code,e))
104 logging.error("getHttpData: HTTP error(%s): %s"%(e.code,e)) 207 errmsg = str(e)
105 errmsg = str(e) 208 # stop trying
106 # stop trying 209 break
107 break 210
108 except urllib2.URLError, e: 211 else:
109 logging.error("getHttpData: URLLIB error(%s): %s"%(e.reason,e)) 212 # use urllib2
110 errmsg = str(e) 213 response = None
111 # stop trying 214 for cnt in range(num_tries):
112 #break 215 try:
113 216 logging.debug("getHttpData(#%s %ss) url=%s"%(cnt+1,timeout,url))
114 if response is not None: 217 if sys.version_info < (2, 6):
115 data = response.read() 218 # set timeout on socket -- ugly :-(
116 response.close() 219 import socket
117 return data 220 socket.setdefaulttimeout(float(timeout))
221 response = urllib2.urlopen(url)
222 else:
223 # timeout as parameter
224 response = urllib2.urlopen(url,timeout=float(timeout))
225 # check result?
226 data = response.read()
227 response.close()
228 return data
229
230 except urllib2.HTTPError, e:
231 logging.error("getHttpData: HTTP error(%s): %s"%(e.code,e))
232 errmsg = str(e)
233 # stop trying
234 break
235 except urllib2.URLError, e:
236 logging.error("getHttpData: URLLIB error(%s): %s"%(e.reason,e))
237 errmsg = str(e)
238 # stop trying
239 #break
118 240
119 if noExceptions: 241 if noExceptions:
120 return None 242 return None
121 243
122 raise IOError("ERROR fetching HTTP data from %s: %s"%(url,errmsg)) 244 raise IOError("ERROR fetching HTTP data from %s: %s"%(url,errmsg))
131 self.lmh = rfc1123_date(self.lmt) 253 self.lmh = rfc1123_date(self.lmt)
132 # call original method 254 # call original method
133 return ImageFile.index_html(self, REQUEST, RESPONSE) 255 return ImageFile.index_html(self, REQUEST, RESPONSE)
134 256
135 257
258 def shortenString(s, l, ellipsis='...'):
259 """returns a string of length l (or l-1) by omitting characters in the middle of s, replacing with ellipsis."""
260 if len(s) <= l:
261 return s
262
263 l1 = int((l - len(ellipsis)) / 2)
264 return "%s%s%s"%(s[:l1],ellipsis,s[-l1:])
265
266
267 def sqlName(s, lc=True, more=''):
268 """returns restricted ASCII-only version of string"""
269 if s is None:
270 return ""
271
272 if not isinstance(s, basestring):
273 # make string object
274 s = str(s)
275
276 # remove '
277 s = s.replace("'","")
278 # all else -> "_"
279 s = re.sub('[^A-Za-z0-9_'+more+']','_',s)
280 if lc:
281 return s.lower()
282
283 return s
284
285
136 def getBrowserType(self): 286 def getBrowserType(self):
137 """check the browsers request to find out the browser type""" 287 """(legacy) check the browsers request to find out the browser type"""
138 bt = {} 288 bt = {}
139 ua = self.REQUEST.get_header("HTTP_USER_AGENT") 289 ua = self.REQUEST.get_header("HTTP_USER_AGENT")
140 bt['ua'] = ua 290 bt['ua'] = ua
141 bt['isIE'] = False 291 bt['isIE'] = False
142 bt['isN4'] = False 292 bt['isN4'] = False
158 bt['isIEMac'] = bt['isIE'] and bt['isMac'] 308 bt['isIEMac'] = bt['isIE'] and bt['isMac']
159 bt['staticHTML'] = False 309 bt['staticHTML'] = False
160 310
161 return bt 311 return bt
162 312
163