1 | """Utility methods for handling XML, reading HTTP, etc""" |
---|
2 | |
---|
3 | from App.ImageFile import ImageFile |
---|
4 | from App.Common import rfc1123_date |
---|
5 | |
---|
6 | import sys |
---|
7 | import os |
---|
8 | import stat |
---|
9 | import urllib |
---|
10 | import logging |
---|
11 | import time |
---|
12 | import re |
---|
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.2" |
---|
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"]} |
---|
53 | |
---|
54 | def getInt(number, default=0): |
---|
55 | """returns always an int (0 in case of problems)""" |
---|
56 | try: |
---|
57 | return int(number) |
---|
58 | except: |
---|
59 | return int(default) |
---|
60 | |
---|
61 | def getAt(array, idx, default=None): |
---|
62 | """returns element idx from array or default (in case of problems)""" |
---|
63 | try: |
---|
64 | return array[idx] |
---|
65 | except: |
---|
66 | return default |
---|
67 | |
---|
68 | def unicodify(s): |
---|
69 | """decode str (utf-8 or latin-1 representation) into unicode object""" |
---|
70 | if not s: |
---|
71 | return u"" |
---|
72 | if isinstance(s, str): |
---|
73 | try: |
---|
74 | return s.decode('utf-8') |
---|
75 | except: |
---|
76 | return s.decode('latin-1') |
---|
77 | else: |
---|
78 | return s |
---|
79 | |
---|
80 | def utf8ify(s): |
---|
81 | """encode unicode object or string into byte string in utf-8 representation. |
---|
82 | assumes string objects to be utf-8""" |
---|
83 | if not s: |
---|
84 | return "" |
---|
85 | if isinstance(s, str): |
---|
86 | return s |
---|
87 | elif isinstance(s, unicode): |
---|
88 | return s.encode('utf-8') |
---|
89 | else: |
---|
90 | return str(s) |
---|
91 | |
---|
92 | def getText(node, recursive=0): |
---|
93 | """returns all text content of a (etree) node and its subnodes""" |
---|
94 | if node is None: |
---|
95 | return '' |
---|
96 | |
---|
97 | # ElementTree: |
---|
98 | text = node.text or '' |
---|
99 | for e in node: |
---|
100 | if recursive: |
---|
101 | text += getText(e) |
---|
102 | else: |
---|
103 | text += e.text or '' |
---|
104 | if e.tail: |
---|
105 | text += e.tail |
---|
106 | |
---|
107 | # 4Suite: |
---|
108 | #nodelist=node.childNodes |
---|
109 | #text = "" |
---|
110 | #for n in nodelist: |
---|
111 | # if n.nodeType == node.TEXT_NODE: |
---|
112 | # text = text + n.data |
---|
113 | |
---|
114 | return text |
---|
115 | |
---|
116 | |
---|
117 | def serialize(node): |
---|
118 | """returns a string containing an XML snippet of (etree) node""" |
---|
119 | s = ET.tostring(node, 'UTF-8') |
---|
120 | # snip off XML declaration |
---|
121 | if s.startswith('<?xml'): |
---|
122 | i = s.find('?>') |
---|
123 | return s[i+3:] |
---|
124 | |
---|
125 | return s |
---|
126 | |
---|
127 | |
---|
128 | def getMonthName(mon, lang): |
---|
129 | """returns the name of the month mon in the language lang""" |
---|
130 | return map_months[lang][mon] |
---|
131 | |
---|
132 | |
---|
133 | def getDateString(date=None, lang='en', withYear=True, monthNames=True, abbrev=False): |
---|
134 | """Return formatted date string.""" |
---|
135 | if date is None: |
---|
136 | return None |
---|
137 | |
---|
138 | ds = None |
---|
139 | if callable(date.day): |
---|
140 | # callable members |
---|
141 | day = date.day() |
---|
142 | month = date.month() |
---|
143 | year = date.year() |
---|
144 | else: |
---|
145 | # data members |
---|
146 | day = date.day |
---|
147 | month = date.month |
---|
148 | year = date.year |
---|
149 | |
---|
150 | if lang.lower() == 'en': |
---|
151 | ds = "%s %s"%(getMonthName(month, lang), day) |
---|
152 | if withYear: |
---|
153 | ds += ", %s"%year |
---|
154 | |
---|
155 | elif lang.lower() == 'de': |
---|
156 | ds = "%s. %s"%(day, getMonthName(month, lang)) |
---|
157 | if withYear: |
---|
158 | ds += " %s"%year |
---|
159 | |
---|
160 | elif lang.lower() == 'iso': |
---|
161 | ds = date.isoformat() |
---|
162 | |
---|
163 | return ds |
---|
164 | |
---|
165 | |
---|
166 | def getDate(date): |
---|
167 | """return date object from date or datetime date.""" |
---|
168 | if isinstance(date, datetime.datetime): |
---|
169 | # strip time |
---|
170 | return date.date() |
---|
171 | |
---|
172 | return date |
---|
173 | |
---|
174 | |
---|
175 | def getDatetime(date): |
---|
176 | """return datetime object from date or datetime date.""" |
---|
177 | if isinstance(date, datetime.date): |
---|
178 | # add time 0:00 |
---|
179 | return datetime.datetime.combine(date, datetime.time()) |
---|
180 | |
---|
181 | return date |
---|
182 | |
---|
183 | |
---|
184 | def getHttpData(url, data=None, num_tries=3, timeout=10, username=None, password=None, cache=None, insecure=False, noExceptions=False): |
---|
185 | """returns result from url+data HTTP request""" |
---|
186 | # we do GET (by appending data to url) |
---|
187 | if isinstance(data, str) or isinstance(data, unicode): |
---|
188 | # if data is string then append |
---|
189 | url = "%s?%s"%(url,data) |
---|
190 | elif isinstance(data, dict) or isinstance(data, list) or isinstance(data, tuple): |
---|
191 | # urlencode |
---|
192 | url = "%s?%s"%(url,urllib.urlencode(data)) |
---|
193 | |
---|
194 | errmsg = None |
---|
195 | if httplib == 'httplib2': |
---|
196 | # use httplib2 |
---|
197 | for cnt in range(num_tries): |
---|
198 | try: |
---|
199 | logging.debug("getHttp(lib2)Data(#%s %ss) url=%s"%(cnt+1,timeout,url)) |
---|
200 | h = httplib2.Http(cache=cache, timeout=float(timeout), disable_ssl_certificate_validation=insecure) |
---|
201 | if username: |
---|
202 | h.add_credentials(username, password) |
---|
203 | |
---|
204 | resp, data = h.request(url) |
---|
205 | return data |
---|
206 | |
---|
207 | except httplib2.HttpLib2Error, e: |
---|
208 | logging.error("getHttp(lib2)Data: HTTP error(%s): %s"%(e.code,e)) |
---|
209 | errmsg = str(e) |
---|
210 | # stop trying |
---|
211 | break |
---|
212 | |
---|
213 | else: |
---|
214 | # use urllib2 |
---|
215 | response = None |
---|
216 | for cnt in range(num_tries): |
---|
217 | try: |
---|
218 | logging.debug("getHttpData(#%s %ss) url=%s"%(cnt+1,timeout,url)) |
---|
219 | if sys.version_info < (2, 6): |
---|
220 | # set timeout on socket -- ugly :-( |
---|
221 | import socket |
---|
222 | socket.setdefaulttimeout(float(timeout)) |
---|
223 | response = urllib2.urlopen(url) |
---|
224 | else: |
---|
225 | # timeout as parameter |
---|
226 | response = urllib2.urlopen(url,timeout=float(timeout)) |
---|
227 | # check result? |
---|
228 | data = response.read() |
---|
229 | response.close() |
---|
230 | return data |
---|
231 | |
---|
232 | except urllib2.HTTPError, e: |
---|
233 | logging.error("getHttpData: HTTP error(%s): %s"%(e.code,e)) |
---|
234 | errmsg = str(e) |
---|
235 | # stop trying |
---|
236 | break |
---|
237 | except urllib2.URLError, e: |
---|
238 | logging.error("getHttpData: URLLIB error(%s): %s"%(e.reason,e)) |
---|
239 | errmsg = str(e) |
---|
240 | # stop trying |
---|
241 | #break |
---|
242 | |
---|
243 | if noExceptions: |
---|
244 | return None |
---|
245 | |
---|
246 | raise IOError("ERROR fetching HTTP data from %s: %s"%(url,errmsg)) |
---|
247 | #return None |
---|
248 | |
---|
249 | |
---|
250 | def refreshingImageFileIndexHtml(self, REQUEST, RESPONSE): |
---|
251 | """index_html method for App.ImageFile that updates the file info for each request.""" |
---|
252 | stat_info = os.stat(self.path) |
---|
253 | self.size = stat_info[stat.ST_SIZE] |
---|
254 | self.lmt = float(stat_info[stat.ST_MTIME]) or time.time() |
---|
255 | self.lmh = rfc1123_date(self.lmt) |
---|
256 | # call original method |
---|
257 | return ImageFile.index_html(self, REQUEST, RESPONSE) |
---|
258 | |
---|
259 | |
---|
260 | def shortenString(s, l, ellipsis='...'): |
---|
261 | """returns a string of length l (or l-1) by omitting characters in the middle of s, replacing with ellipsis.""" |
---|
262 | if len(s) <= l: |
---|
263 | return s |
---|
264 | |
---|
265 | l1 = int((l - len(ellipsis)) / 2) |
---|
266 | return "%s%s%s"%(s[:l1],ellipsis,s[-l1:]) |
---|
267 | |
---|
268 | |
---|
269 | def sqlName(s, lc=True, more=''): |
---|
270 | """returns restricted ASCII-only version of string""" |
---|
271 | if s is None: |
---|
272 | return "" |
---|
273 | |
---|
274 | if not isinstance(s, basestring): |
---|
275 | # make string object |
---|
276 | s = str(s) |
---|
277 | |
---|
278 | # remove ' |
---|
279 | s = s.replace("'","") |
---|
280 | # all else -> "_" |
---|
281 | s = re.sub('[^A-Za-z0-9_'+more+']','_',s) |
---|
282 | if lc: |
---|
283 | return s.lower() |
---|
284 | |
---|
285 | return s |
---|
286 | |
---|
287 | |
---|
288 | def getBrowserType(self): |
---|
289 | """(legacy) check the browsers request to find out the browser type""" |
---|
290 | bt = {} |
---|
291 | ua = self.REQUEST.get_header("HTTP_USER_AGENT") |
---|
292 | bt['ua'] = ua |
---|
293 | bt['isIE'] = False |
---|
294 | bt['isN4'] = False |
---|
295 | if string.find(ua, 'MSIE') > -1: |
---|
296 | bt['isIE'] = True |
---|
297 | else: |
---|
298 | bt['isN4'] = (string.find(ua, 'Mozilla/4.') > -1) |
---|
299 | |
---|
300 | try: |
---|
301 | nav = ua[string.find(ua, '('):] |
---|
302 | ie = string.split(nav, "; ")[1] |
---|
303 | if string.find(ie, "MSIE") > -1: |
---|
304 | bt['versIE'] = string.split(ie, " ")[1] |
---|
305 | except: pass |
---|
306 | |
---|
307 | bt['isMac'] = string.find(ua, 'Macintosh') > -1 |
---|
308 | bt['isWin'] = string.find(ua, 'Windows') > -1 |
---|
309 | bt['isIEWin'] = bt['isIE'] and bt['isWin'] |
---|
310 | bt['isIEMac'] = bt['isIE'] and bt['isMac'] |
---|
311 | bt['staticHTML'] = False |
---|
312 | |
---|
313 | return bt |
---|
314 | |
---|