annotate SrvTxtUtils.py @ 635:8d460ddb45b7 default tip

update digilib dirInfo-xml path.
author casties
date Fri, 06 May 2016 18:59:35 +0200
parents 447251b5af65
children
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
623
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
10 from urlparse import urlparse, urlunparse
458
48b135b089c8 more renovation
casties
parents:
diff changeset
11 import logging
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
12 import time
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
13 import re
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
14 import datetime
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
15 try:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
16 import httplib2
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
17 httplib = 'httplib2'
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
18 except:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
19 logging.warn("Unable to import httplib2! Falling back to urllib2!")
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
20 import urllib2
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
21 httplib = 'urllib2'
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
22
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
23 import xml.etree.ElementTree as ET
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
24
628
447251b5af65 make sure utf8ify and unicodify output text.
casties
parents: 623
diff changeset
25 srvTxtUtilsVersion = "1.13.1"
458
48b135b089c8 more renovation
casties
parents:
diff changeset
26
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
27 map_months = {'en': [u"",
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
28 u"January",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
29 u"February",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
30 u"March",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
31 u"April",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
32 u"May",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
33 u"June",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
34 u"July",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
35 u"August",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
36 u"September",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
37 u"October",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
38 u"November",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
39 u"December"],
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
40 'de': [u"",
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
41 u"Januar",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
42 u"Februar",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
43 u"M\u00e4rz",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
44 u"April",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
45 u"Mai",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
46 u"Juni",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
47 u"Juli",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
48 u"August",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
49 u"September",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
50 u"Oktober",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
51 u"November",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
52 u"Dezember"]}
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
53
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
54 map_weekdays_short = {'en': [
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
55 u"Mo",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
56 u"Tu",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
57 u"We",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
58 u"Th",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
59 u"Fr",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
60 u"Sa",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
61 u"Su",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
62 ],
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
63 'de': [
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
64 u"Mo",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
65 u"Di",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
66 u"Mi",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
67 u"Do",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
68 u"Fr",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
69 u"Sa",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
70 u"So",
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
71 ]}
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
72
458
48b135b089c8 more renovation
casties
parents:
diff changeset
73
48b135b089c8 more renovation
casties
parents:
diff changeset
74 def getInt(number, default=0):
48b135b089c8 more renovation
casties
parents:
diff changeset
75 """returns always an int (0 in case of problems)"""
48b135b089c8 more renovation
casties
parents:
diff changeset
76 try:
48b135b089c8 more renovation
casties
parents:
diff changeset
77 return int(number)
48b135b089c8 more renovation
casties
parents:
diff changeset
78 except:
48b135b089c8 more renovation
casties
parents:
diff changeset
79 return int(default)
48b135b089c8 more renovation
casties
parents:
diff changeset
80
490
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
81 def getAt(array, idx, default=None):
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
82 """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
83 try:
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
84 return array[idx]
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
85 except:
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
86 return default
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
87
514
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
88 def unicodify(s):
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
89 """decode str (utf-8 or latin-1 representation) into unicode object"""
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
90 if not s:
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
91 return u""
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
92 if isinstance(s, str):
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
93 try:
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
94 return s.decode('utf-8')
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
95 except:
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
96 return s.decode('latin-1')
628
447251b5af65 make sure utf8ify and unicodify output text.
casties
parents: 623
diff changeset
97 elif isinstance(s, unicode):
447251b5af65 make sure utf8ify and unicodify output text.
casties
parents: 623
diff changeset
98 return s
514
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
99 else:
628
447251b5af65 make sure utf8ify and unicodify output text.
casties
parents: 623
diff changeset
100 return unicode(s)
514
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
101
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
102 def utf8ify(s):
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
103 """encode unicode object or string into byte string in utf-8 representation.
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
104 assumes string objects to be utf-8"""
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
105 if not s:
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
106 return ""
628
447251b5af65 make sure utf8ify and unicodify output text.
casties
parents: 623
diff changeset
107 if isinstance(s, unicode):
447251b5af65 make sure utf8ify and unicodify output text.
casties
parents: 623
diff changeset
108 return s.encode('utf-8')
447251b5af65 make sure utf8ify and unicodify output text.
casties
parents: 623
diff changeset
109 elif isinstance(s, str):
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
110 return s
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
111 else:
628
447251b5af65 make sure utf8ify and unicodify output text.
casties
parents: 623
diff changeset
112 return str(s)
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
113
514
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
114
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
115 def getTextFromNode(node, recursive=False, length=0):
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
116 """Return all text content of a (etree) node.
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
117
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
118 :param recursive: descend subnodes
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
119
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
120 :returns: text string
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
121 """
458
48b135b089c8 more renovation
casties
parents:
diff changeset
122 if node is None:
490
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
123 return ''
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
124
458
48b135b089c8 more renovation
casties
parents:
diff changeset
125 # ElementTree:
490
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
126 text = node.text or ''
458
48b135b089c8 more renovation
casties
parents:
diff changeset
127 for e in node:
490
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
128 if recursive:
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
129 text += getText(e)
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
130 else:
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
131 text += e.text or ''
458
48b135b089c8 more renovation
casties
parents:
diff changeset
132 if e.tail:
48b135b089c8 more renovation
casties
parents:
diff changeset
133 text += e.tail
48b135b089c8 more renovation
casties
parents:
diff changeset
134
48b135b089c8 more renovation
casties
parents:
diff changeset
135 # 4Suite:
48b135b089c8 more renovation
casties
parents:
diff changeset
136 #nodelist=node.childNodes
48b135b089c8 more renovation
casties
parents:
diff changeset
137 #text = ""
48b135b089c8 more renovation
casties
parents:
diff changeset
138 #for n in nodelist:
48b135b089c8 more renovation
casties
parents:
diff changeset
139 # if n.nodeType == node.TEXT_NODE:
48b135b089c8 more renovation
casties
parents:
diff changeset
140 # text = text + n.data
48b135b089c8 more renovation
casties
parents:
diff changeset
141
48b135b089c8 more renovation
casties
parents:
diff changeset
142 return text
48b135b089c8 more renovation
casties
parents:
diff changeset
143
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
144 getText = getTextFromNode
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
145
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
146 def getPlaintext(text, length=0, wordwrap=False, ignoretags=[]):
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
147 """Return plain text content by filtering out XML tags.
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
148
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
149 :param text: string or etree node
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
150 :param length: length of text to return (0=all)
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
151 :param wordwrap: try not to break the last word (may return shorter string)
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
152 :returns: text string
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
153 """
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
154 if text is None:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
155 return ''
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
156
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
157 try:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
158 if isinstance(text, basestring):
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
159 xmltext = utf8ify("<div>%s</div>"%text)
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
160 dom = ET.fromstring(xmltext)
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
161 else:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
162 dom = text
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
163
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
164 plaintext = ''
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
165 for elem in dom.iter():
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
166 if elem.tag in ignoretags:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
167 # ignore tag
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
168 continue
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
169
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
170 if elem.text:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
171 plaintext += elem.text
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
172 if elem.tail:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
173 plaintext += elem.tail
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
174
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
175 if length > 0 and len(plaintext) > length:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
176 break
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
177
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
178 text = plaintext
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
179
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
180 except Exception, e:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
181 logging.warn("getPlaintext: error parsing text! Returning everything. %s"%e)
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
182
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
183 if length > 0 and len(text) > length:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
184 # try to not break words
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
185 if wordwrap and text[length] not in [' ', '.', '?', '!']:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
186 # search the last blank
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
187 length = text.rfind(' ', 0, length)
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
188
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
189 return text[:length] + '...'
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
190
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
191 return text
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
192
458
48b135b089c8 more renovation
casties
parents:
diff changeset
193
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
194 def serialize(node):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
195 """returns a string containing an XML snippet of (etree) node"""
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
196 s = ET.tostring(node, 'UTF-8')
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
197 # snip off XML declaration
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
198 if s.startswith('<?xml'):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
199 i = s.find('?>')
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
200 return s[i+3:]
458
48b135b089c8 more renovation
casties
parents:
diff changeset
201
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
202 return s
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
203
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
204
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
205 def getMonthName(mon, lang):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
206 """returns the name of the month mon in the language lang"""
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
207 return map_months[lang.lower()][mon]
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
208
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
209
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
210 def getWeekdayName(day, lang, short=True):
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
211 """returns the name of the weekday day in the language lang"""
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
212 return map_weekdays_short[lang.lower()][day]
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
213
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
214
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
215 def getDateString(date=None, lang='en', short=False, withYear=True, monthNames=True, abbrev=False):
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
216 """Return formatted date string."""
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
217 if date is None:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
218 return None
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
219
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
220 ds = None
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
221 if callable(date.day):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
222 # callable members
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
223 day = date.day()
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
224 month = date.month()
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
225 year = date.year()
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
226 else:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
227 # data members
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
228 day = date.day
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
229 month = date.month
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
230 year = date.year
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
231
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
232 if lang.lower() == 'en':
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
233 if short:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
234 ds = "%s/%s/%s"%(year,month,day)
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
235 else:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
236 ds = "%s %s"%(getMonthName(month, lang), day)
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
237 if withYear:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
238 ds += ", %s"%year
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
239
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
240 elif lang.lower() == 'de':
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
241 if short:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
242 ds = "%s.%s.%s"%(day,month,year)
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
243 else:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
244 ds = "%s. %s"%(day, getMonthName(month, lang))
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
245 if withYear:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
246 ds += " %s"%year
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
247
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
248 elif lang.lower() == 'iso':
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
249 ds = date.isoformat()
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
250
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
251 return ds
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
252
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
253
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
254 def getDate(date):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
255 """return date object from date or datetime date."""
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
256 if isinstance(date, datetime.datetime):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
257 # strip time
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
258 return date.date()
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
259
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
260 return date
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
261
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
262
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
263 def getDatetime(date):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
264 """return datetime object from date or datetime date."""
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
265 if isinstance(date, datetime.date):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
266 # add time 0:00
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
267 return datetime.datetime.combine(date, datetime.time())
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
268
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
269 return date
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
270
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
271
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
272 def getHttpData(url, data=None, num_tries=3, timeout=10, username=None, password=None, cache=None, insecure=False, noExceptions=False):
458
48b135b089c8 more renovation
casties
parents:
diff changeset
273 """returns result from url+data HTTP request"""
48b135b089c8 more renovation
casties
parents:
diff changeset
274 # we do GET (by appending data to url)
48b135b089c8 more renovation
casties
parents:
diff changeset
275 if isinstance(data, str) or isinstance(data, unicode):
48b135b089c8 more renovation
casties
parents:
diff changeset
276 # if data is string then append
48b135b089c8 more renovation
casties
parents:
diff changeset
277 url = "%s?%s"%(url,data)
48b135b089c8 more renovation
casties
parents:
diff changeset
278 elif isinstance(data, dict) or isinstance(data, list) or isinstance(data, tuple):
48b135b089c8 more renovation
casties
parents:
diff changeset
279 # urlencode
48b135b089c8 more renovation
casties
parents:
diff changeset
280 url = "%s?%s"%(url,urllib.urlencode(data))
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
281
458
48b135b089c8 more renovation
casties
parents:
diff changeset
282 errmsg = None
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
283 if httplib == 'httplib2':
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
284 # use httplib2
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
285 for cnt in range(num_tries):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
286 try:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
287 logging.debug("getHttp(lib2)Data(#%s %ss) url=%s"%(cnt+1,timeout,url))
622
bc68ca0d2c0a make annotationServerUrl work with protocol-relative URL (//).
casties
parents: 621
diff changeset
288 # I would prefer at least disable_ssl_certificate_validation=insecure
bc68ca0d2c0a make annotationServerUrl work with protocol-relative URL (//).
casties
parents: 621
diff changeset
289 # but python < 2.7.9 doesn't do SNI :-(
621
a7b287122ce8 relax certificate checking even more.
casties
parents: 620
diff changeset
290 h = httplib2.Http(cache=cache, timeout=float(timeout), disable_ssl_certificate_validation=True)
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
291 if username:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
292 h.add_credentials(username, password)
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
293
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
294 resp, data = h.request(url)
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
295 return data
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
296
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
297 except httplib2.HttpLib2Error, e:
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
298 logging.error("getHttp(lib2)Data: HTTP error(%s): %s"%(getattr(e, 'code','?'),e))
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
299 errmsg = str(e)
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
300 # stop trying
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
301 break
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
302
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
303 else:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
304 # use urllib2
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
305 response = None
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
306 for cnt in range(num_tries):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
307 try:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
308 logging.debug("getHttpData(#%s %ss) url=%s"%(cnt+1,timeout,url))
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
309 if sys.version_info < (2, 6):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
310 # set timeout on socket -- ugly :-(
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
311 import socket
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
312 socket.setdefaulttimeout(float(timeout))
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
313 response = urllib2.urlopen(url)
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
314 else:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
315 # timeout as parameter
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
316 response = urllib2.urlopen(url,timeout=float(timeout))
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
317 # check result?
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
318 data = response.read()
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
319 response.close()
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
320 return data
458
48b135b089c8 more renovation
casties
parents:
diff changeset
321
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
322 except urllib2.HTTPError, e:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
323 logging.error("getHttpData: HTTP error(%s): %s"%(e.code,e))
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
324 errmsg = str(e)
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
325 # stop trying
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
326 break
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
327 except urllib2.URLError, e:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
328 logging.error("getHttpData: URLLIB error(%s): %s"%(e.reason,e))
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
329 errmsg = str(e)
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
330 # stop trying
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
331 #break
458
48b135b089c8 more renovation
casties
parents:
diff changeset
332
546
2928037f9a75 ASSIGNED - # 249: Annotations shared in groups
casties
parents: 528
diff changeset
333 if noExceptions:
2928037f9a75 ASSIGNED - # 249: Annotations shared in groups
casties
parents: 528
diff changeset
334 return None
2928037f9a75 ASSIGNED - # 249: Annotations shared in groups
casties
parents: 528
diff changeset
335
458
48b135b089c8 more renovation
casties
parents:
diff changeset
336 raise IOError("ERROR fetching HTTP data from %s: %s"%(url,errmsg))
48b135b089c8 more renovation
casties
parents:
diff changeset
337 #return None
48b135b089c8 more renovation
casties
parents:
diff changeset
338
490
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
339
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
340 def refreshingImageFileIndexHtml(self, REQUEST, RESPONSE):
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
341 """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
342 stat_info = os.stat(self.path)
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
343 self.size = stat_info[stat.ST_SIZE]
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
344 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
345 self.lmh = rfc1123_date(self.lmt)
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
346 # call original method
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
347 return ImageFile.index_html(self, REQUEST, RESPONSE)
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
348
585
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
349
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
350 def shortenString(s, l, ellipsis='...'):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
351 """returns a string of length l (or l-1) by omitting characters in the middle of s, replacing with ellipsis."""
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
352 if len(s) <= l:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
353 return s
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
354
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
355 l1 = int((l - len(ellipsis)) / 2)
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
356 return "%s%s%s"%(s[:l1],ellipsis,s[-l1:])
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
357
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
358
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
359 def sqlName(s, lc=True, more=''):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
360 """returns restricted ASCII-only version of string"""
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
361 if s is None:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
362 return ""
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
363
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
364 if not isinstance(s, basestring):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
365 # make string object
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
366 s = str(s)
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
367
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
368 # remove '
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
369 s = s.replace("'","")
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
370 # all else -> "_"
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
371 s = re.sub('[^A-Za-z0-9_'+more+']','_',s)
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
372 if lc:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
373 return s.lower()
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
374
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
375 return s
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
376
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
377
623
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
378 def sslifyUrl(url, app=None, force=False):
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
379 """returns URL with http or https scheme.
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
380
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
381 Looks at app.REQUEST.URL to find the scheme of the current page.
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
382 Changes only schemeless (starting with //) URLs unless force=True.
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
383 """
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
384 thatUrl = urlparse(url)
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
385 if hasattr(app, 'REQUEST'):
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
386 # get current page URL
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
387 thisUrl = urlparse(app.REQUEST['URL'])
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
388 if thatUrl.scheme == '':
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
389 # schemeless URL -> use this scheme
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
390 return "%s:%s"%(thisUrl.scheme, url)
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
391 elif force:
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
392 # use this scheme
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
393 if thisUrl.scheme != thatUrl.scheme:
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
394 return urlunparse((thisUrl.scheme,)+thatUrl[1:])
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
395 else:
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
396 # keep scheme
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
397 return url
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
398
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
399 else:
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
400 # keep scheme
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
401 return url
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
402
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
403 else:
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
404 # no current page URL
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
405 if force:
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
406 # use https for force
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
407 return urlunparse(('https',)+thatUrl[1:])
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
408
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
409 return url