annotate SrvTxtUtils.py @ 623:6012fe93f78c

better scheme-less URL code.
author casties
date Mon, 15 Dec 2014 16:10:05 +0100
parents bc68ca0d2c0a
children 447251b5af65
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
623
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
25 srvTxtUtilsVersion = "1.13"
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')
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
97 else:
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
98 return s
514
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
99
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
100 def utf8ify(s):
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
101 """encode unicode object or string into byte string in utf-8 representation.
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
102 assumes string objects to be utf-8"""
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
103 if not s:
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
104 return ""
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
105 if isinstance(s, str):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
106 return s
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
107 else:
614
d16da6e739ef fix problem with utf8ify trying to encode ints.
casties
parents: 613
diff changeset
108 return s.encode('utf-8')
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
109
514
c55e376be01b search works even with unicode...
casties
parents: 490
diff changeset
110
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
111 def getTextFromNode(node, recursive=False, length=0):
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
112 """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
113
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
114 :param recursive: descend subnodes
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
115
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
116 :returns: text string
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
117 """
458
48b135b089c8 more renovation
casties
parents:
diff changeset
118 if node is None:
490
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
119 return ''
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
120
458
48b135b089c8 more renovation
casties
parents:
diff changeset
121 # ElementTree:
490
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
122 text = node.text or ''
458
48b135b089c8 more renovation
casties
parents:
diff changeset
123 for e in node:
490
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
124 if recursive:
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
125 text += getText(e)
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
126 else:
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
127 text += e.text or ''
458
48b135b089c8 more renovation
casties
parents:
diff changeset
128 if e.tail:
48b135b089c8 more renovation
casties
parents:
diff changeset
129 text += e.tail
48b135b089c8 more renovation
casties
parents:
diff changeset
130
48b135b089c8 more renovation
casties
parents:
diff changeset
131 # 4Suite:
48b135b089c8 more renovation
casties
parents:
diff changeset
132 #nodelist=node.childNodes
48b135b089c8 more renovation
casties
parents:
diff changeset
133 #text = ""
48b135b089c8 more renovation
casties
parents:
diff changeset
134 #for n in nodelist:
48b135b089c8 more renovation
casties
parents:
diff changeset
135 # if n.nodeType == node.TEXT_NODE:
48b135b089c8 more renovation
casties
parents:
diff changeset
136 # text = text + n.data
48b135b089c8 more renovation
casties
parents:
diff changeset
137
48b135b089c8 more renovation
casties
parents:
diff changeset
138 return text
48b135b089c8 more renovation
casties
parents:
diff changeset
139
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
140 getText = getTextFromNode
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
141
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
142 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
143 """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
144
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
145 :param text: string or etree node
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
146 :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
147 :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
148 :returns: text string
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
149 """
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
150 if text is None:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
151 return ''
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
152
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
153 try:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
154 if isinstance(text, basestring):
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
155 xmltext = utf8ify("<div>%s</div>"%text)
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
156 dom = ET.fromstring(xmltext)
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
157 else:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
158 dom = text
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
159
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
160 plaintext = ''
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
161 for elem in dom.iter():
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
162 if elem.tag in ignoretags:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
163 # ignore tag
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
164 continue
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
165
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
166 if elem.text:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
167 plaintext += elem.text
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
168 if elem.tail:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
169 plaintext += elem.tail
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
170
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
171 if length > 0 and len(plaintext) > length:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
172 break
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
173
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
174 text = plaintext
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
175
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
176 except Exception, e:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
177 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
178
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
179 if length > 0 and len(text) > length:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
180 # try to not break words
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
181 if wordwrap and text[length] not in [' ', '.', '?', '!']:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
182 # search the last blank
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
183 length = text.rfind(' ', 0, length)
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
184
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
185 return text[:length] + '...'
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
186
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
187 return text
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
188
458
48b135b089c8 more renovation
casties
parents:
diff changeset
189
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
190 def serialize(node):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
191 """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
192 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
193 # snip off XML declaration
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
194 if s.startswith('<?xml'):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
195 i = s.find('?>')
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
196 return s[i+3:]
458
48b135b089c8 more renovation
casties
parents:
diff changeset
197
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
198 return s
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
199
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
200
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
201 def getMonthName(mon, lang):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
202 """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
203 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
204
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
205
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
206 def getWeekdayName(day, lang, short=True):
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
207 """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
208 return map_weekdays_short[lang.lower()][day]
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
209
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
210
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
211 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
212 """Return formatted date string."""
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
213 if date is None:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
214 return None
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
215
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
216 ds = None
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
217 if callable(date.day):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
218 # callable members
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
219 day = date.day()
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
220 month = date.month()
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
221 year = date.year()
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
222 else:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
223 # data members
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
224 day = date.day
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
225 month = date.month
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
226 year = date.year
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
227
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
228 if lang.lower() == 'en':
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
229 if short:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
230 ds = "%s/%s/%s"%(year,month,day)
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
231 else:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
232 ds = "%s %s"%(getMonthName(month, lang), day)
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
233 if withYear:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
234 ds += ", %s"%year
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
235
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
236 elif lang.lower() == 'de':
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
237 if short:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
238 ds = "%s.%s.%s"%(day,month,year)
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
239 else:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
240 ds = "%s. %s"%(day, getMonthName(month, lang))
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
241 if withYear:
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
242 ds += " %s"%year
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
243
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
244 elif lang.lower() == 'iso':
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
245 ds = date.isoformat()
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
246
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
247 return ds
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
248
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
249
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
250 def getDate(date):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
251 """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
252 if isinstance(date, datetime.datetime):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
253 # strip time
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
254 return date.date()
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
255
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
256 return date
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
257
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
258
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
259 def getDatetime(date):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
260 """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
261 if isinstance(date, datetime.date):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
262 # add time 0:00
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
263 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
264
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
265 return date
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
266
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
267
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
268 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
269 """returns result from url+data HTTP request"""
48b135b089c8 more renovation
casties
parents:
diff changeset
270 # we do GET (by appending data to url)
48b135b089c8 more renovation
casties
parents:
diff changeset
271 if isinstance(data, str) or isinstance(data, unicode):
48b135b089c8 more renovation
casties
parents:
diff changeset
272 # if data is string then append
48b135b089c8 more renovation
casties
parents:
diff changeset
273 url = "%s?%s"%(url,data)
48b135b089c8 more renovation
casties
parents:
diff changeset
274 elif isinstance(data, dict) or isinstance(data, list) or isinstance(data, tuple):
48b135b089c8 more renovation
casties
parents:
diff changeset
275 # urlencode
48b135b089c8 more renovation
casties
parents:
diff changeset
276 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
277
458
48b135b089c8 more renovation
casties
parents:
diff changeset
278 errmsg = None
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
279 if httplib == 'httplib2':
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
280 # use httplib2
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
281 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
282 try:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
283 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
284 # I would prefer at least disable_ssl_certificate_validation=insecure
bc68ca0d2c0a make annotationServerUrl work with protocol-relative URL (//).
casties
parents: 621
diff changeset
285 # but python < 2.7.9 doesn't do SNI :-(
621
a7b287122ce8 relax certificate checking even more.
casties
parents: 620
diff changeset
286 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
287 if username:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
288 h.add_credentials(username, password)
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
289
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
290 resp, data = h.request(url)
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
291 return data
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
292
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
293 except httplib2.HttpLib2Error, e:
620
a71ae589d342 use https for annotations and fix bug when logging httplib errors.
casties
parents: 614
diff changeset
294 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
295 errmsg = str(e)
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
296 # stop trying
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
297 break
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
298
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
299 else:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
300 # use urllib2
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
301 response = None
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
302 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
303 try:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
304 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
305 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
306 # set timeout on socket -- ugly :-(
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
307 import socket
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
308 socket.setdefaulttimeout(float(timeout))
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
309 response = urllib2.urlopen(url)
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
310 else:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
311 # timeout as parameter
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
312 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
313 # check result?
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
314 data = response.read()
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
315 response.close()
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
316 return data
458
48b135b089c8 more renovation
casties
parents:
diff changeset
317
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
318 except urllib2.HTTPError, e:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
319 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
320 errmsg = str(e)
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
321 # stop trying
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
322 break
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
323 except urllib2.URLError, e:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
324 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
325 errmsg = str(e)
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
326 # stop trying
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
327 #break
458
48b135b089c8 more renovation
casties
parents:
diff changeset
328
546
2928037f9a75 ASSIGNED - # 249: Annotations shared in groups
casties
parents: 528
diff changeset
329 if noExceptions:
2928037f9a75 ASSIGNED - # 249: Annotations shared in groups
casties
parents: 528
diff changeset
330 return None
2928037f9a75 ASSIGNED - # 249: Annotations shared in groups
casties
parents: 528
diff changeset
331
458
48b135b089c8 more renovation
casties
parents:
diff changeset
332 raise IOError("ERROR fetching HTTP data from %s: %s"%(url,errmsg))
48b135b089c8 more renovation
casties
parents:
diff changeset
333 #return None
48b135b089c8 more renovation
casties
parents:
diff changeset
334
490
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
335
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
336 def refreshingImageFileIndexHtml(self, REQUEST, RESPONSE):
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
337 """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
338 stat_info = os.stat(self.path)
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
339 self.size = stat_info[stat.ST_SIZE]
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
340 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
341 self.lmh = rfc1123_date(self.lmt)
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
342 # call original method
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
343 return ImageFile.index_html(self, REQUEST, RESPONSE)
6f116b86a226 more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents: 464
diff changeset
344
585
83eeed69793f new annotator layer for images.
casties
parents: 553
diff changeset
345
613
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
346 def shortenString(s, l, ellipsis='...'):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
347 """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
348 if len(s) <= l:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
349 return s
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
350
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
351 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
352 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
353
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 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
356 """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
357 if s is None:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
358 return ""
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
359
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
360 if not isinstance(s, basestring):
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
361 # make string object
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
362 s = str(s)
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 # remove '
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
365 s = s.replace("'","")
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
366 # all else -> "_"
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
367 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
368 if lc:
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
369 return s.lower()
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
370
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
371 return s
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
372
c57d80a649ea CLOSED - # 281: List of thumbnails verschluckt Seite, wenn odd-scan-position gesetzt ist
casties
parents: 585
diff changeset
373
623
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
374 def sslifyUrl(url, app=None, force=False):
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
375 """returns URL with http or https scheme.
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
376
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
377 Looks at app.REQUEST.URL to find the scheme of the current page.
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
378 Changes only schemeless (starting with //) URLs unless force=True.
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
379 """
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
380 thatUrl = urlparse(url)
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
381 if hasattr(app, 'REQUEST'):
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
382 # get current page URL
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
383 thisUrl = urlparse(app.REQUEST['URL'])
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
384 if thatUrl.scheme == '':
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
385 # schemeless URL -> use this scheme
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
386 return "%s:%s"%(thisUrl.scheme, url)
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
387 elif force:
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
388 # use this scheme
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
389 if thisUrl.scheme != thatUrl.scheme:
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
390 return urlunparse((thisUrl.scheme,)+thatUrl[1:])
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
391 else:
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
392 # keep scheme
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
393 return url
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
394
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 # no current page URL
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
401 if force:
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
402 # use https for force
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
403 return urlunparse(('https',)+thatUrl[1:])
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
404
6012fe93f78c better scheme-less URL code.
casties
parents: 622
diff changeset
405 return url