Mercurial > hg > MPIWGWeb
annotate SrvTxtUtils.py @ 265:9adba5461dfa new_pro_struct
167: Title of projects' info blocks can't be changed
Task-Url: https://it-dev.mpiwg-berlin.mpg.de/tracs/webpage/ticket/167
| author | casties |
|---|---|
| date | Mon, 25 Aug 2014 17:45:51 +0200 |
| parents | 649f74b167e6 |
| children |
| rev | line source |
|---|---|
|
0
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
1 """Utility methods for handling XML, reading HTTP, etc""" |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
2 |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
3 from App.ImageFile import ImageFile |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
4 from App.Common import rfc1123_date |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
5 |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
6 import sys |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
7 import os |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
8 import stat |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
9 import urllib |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
10 import logging |
| 65 | 11 import time |
|
188
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
12 import re |
| 216 | 13 import datetime |
| 14 try: | |
| 15 import httplib2 | |
| 16 httplib = 'httplib2' | |
| 17 except: | |
| 18 logging.warn("Unable to import httplib2! Falling back to urllib2!") | |
| 19 import urllib2 | |
| 20 httplib = 'urllib2' | |
|
0
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
21 |
| 49 | 22 import xml.etree.ElementTree as ET |
| 23 | |
|
231
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
24 srvTxtUtilsVersion = "1.12.3" |
|
0
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
25 |
| 16 | 26 map_months = {'en': [u"", |
|
231
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
27 u"January", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
28 u"February", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
29 u"March", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
30 u"April", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
31 u"May", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
32 u"June", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
33 u"July", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
34 u"August", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
35 u"September", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
36 u"October", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
37 u"November", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
38 u"December"], |
| 16 | 39 'de': [u"", |
|
231
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
40 u"Januar", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
41 u"Februar", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
42 u"M\u00e4rz", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
43 u"April", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
44 u"Mai", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
45 u"Juni", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
46 u"Juli", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
47 u"August", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
48 u"September", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
49 u"Oktober", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
50 u"November", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
51 u"Dezember"]} |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
52 |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
53 map_weekdays_short = {'en': [ |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
54 u"Mo", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
55 u"Tu", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
56 u"We", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
57 u"Th", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
58 u"Fr", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
59 u"Sa", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
60 u"Su", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
61 ], |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
62 'de': [ |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
63 u"Mo", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
64 u"Di", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
65 u"Mi", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
66 u"Do", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
67 u"Fr", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
68 u"Sa", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
69 u"So", |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
70 ]} |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
71 |
|
0
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
72 |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
73 def getInt(number, default=0): |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
74 """returns always an int (0 in case of problems)""" |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
75 try: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
76 return int(number) |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
77 except: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
78 return int(default) |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
79 |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
80 def getAt(array, idx, default=None): |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
81 """returns element idx from array or default (in case of problems)""" |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
82 try: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
83 return array[idx] |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
84 except: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
85 return default |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
86 |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
87 def unicodify(s): |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
88 """decode str (utf-8 or latin-1 representation) into unicode object""" |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
89 if not s: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
90 return u"" |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
91 if isinstance(s, str): |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
92 try: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
93 return s.decode('utf-8') |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
94 except: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
95 return s.decode('latin-1') |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
96 else: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
97 return s |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
98 |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
99 def utf8ify(s): |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
100 """encode unicode object or string into byte string in utf-8 representation. |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
101 assumes string objects to be utf-8""" |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
102 if not s: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
103 return "" |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
104 if isinstance(s, str): |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
105 return s |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
106 else: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
107 return s.encode('utf-8') |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
108 |
|
231
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
109 |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
110 def getTextFromNode(node, recursive=False, length=0): |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
111 """Return all text content of a (etree) node. |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
112 |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
113 :param recursive: descend subnodes |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
114 |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
115 :returns: text string |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
116 """ |
|
0
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
117 if node is None: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
118 return '' |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
119 |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
120 # ElementTree: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
121 text = node.text or '' |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
122 for e in node: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
123 if recursive: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
124 text += getText(e) |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
125 else: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
126 text += e.text or '' |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
127 if e.tail: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
128 text += e.tail |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
129 |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
130 # 4Suite: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
131 #nodelist=node.childNodes |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
132 #text = "" |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
133 #for n in nodelist: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
134 # if n.nodeType == node.TEXT_NODE: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
135 # text = text + n.data |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
136 |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
137 return text |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
138 |
|
231
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
139 getText = getTextFromNode |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
140 |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
141 def getPlaintext(text, length=0, wordwrap=False, ignoretags=[]): |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
142 """Return plain text content by filtering out XML tags. |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
143 |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
144 :param text: string or etree node |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
145 :param length: length of text to return (0=all) |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
146 :param wordwrap: try not to break the last word (may return shorter string) |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
147 :returns: text string |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
148 """ |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
149 if text is None: |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
150 return '' |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
151 |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
152 try: |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
153 if isinstance(text, basestring): |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
154 xmltext = utf8ify("<div>%s</div>"%text) |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
155 dom = ET.fromstring(xmltext) |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
156 else: |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
157 dom = text |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
158 |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
159 plaintext = '' |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
160 for elem in dom.iter(): |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
161 if elem.tag in ignoretags: |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
162 # ignore tag |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
163 continue |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
164 |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
165 if elem.text: |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
166 plaintext += elem.text |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
167 if elem.tail: |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
168 plaintext += elem.tail |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
169 |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
170 if length > 0 and len(plaintext) > length: |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
171 break |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
172 |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
173 text = plaintext |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
174 |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
175 except Exception, e: |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
176 logging.warn("getPlaintext: error parsing text! Returning everything. %s"%e) |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
177 |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
178 if length > 0 and len(text) > length: |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
179 # try to not break words |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
180 if wordwrap and text[length] not in [' ', '.', '?', '!']: |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
181 # search the last blank |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
182 length = text.rfind(' ', 0, length) |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
183 |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
184 return text[:length] + '...' |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
185 |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
186 return text |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
187 |
| 49 | 188 |
| 189 def serialize(node): | |
| 190 """returns a string containing an XML snippet of (etree) node""" | |
| 191 s = ET.tostring(node, 'UTF-8') | |
| 192 # snip off XML declaration | |
| 193 if s.startswith('<?xml'): | |
| 194 i = s.find('?>') | |
| 195 return s[i+3:] | |
| 196 | |
| 197 return s | |
| 198 | |
| 199 | |
| 16 | 200 def getMonthName(mon, lang): |
| 201 """returns the name of the month mon in the language lang""" | |
|
231
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
202 return map_months[lang.lower()][mon] |
|
0
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
203 |
|
87
77f2478ae971
getDateString method to format dates in English and German.
casties
parents:
65
diff
changeset
|
204 |
|
231
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
205 def getWeekdayName(day, lang, short=True): |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
206 """returns the name of the weekday day in the language lang""" |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
207 return map_weekdays_short[lang.lower()][day] |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
208 |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
209 |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
210 def getDateString(date=None, lang='en', short=False, withYear=True, monthNames=True, abbrev=False): |
|
87
77f2478ae971
getDateString method to format dates in English and German.
casties
parents:
65
diff
changeset
|
211 """Return formatted date string.""" |
| 170 | 212 if date is None: |
| 213 return None | |
| 214 | |
|
87
77f2478ae971
getDateString method to format dates in English and German.
casties
parents:
65
diff
changeset
|
215 ds = None |
| 126 | 216 if callable(date.day): |
| 217 # callable members | |
| 218 day = date.day() | |
| 219 month = date.month() | |
| 220 year = date.year() | |
| 221 else: | |
| 222 # data members | |
| 223 day = date.day | |
| 224 month = date.month | |
| 225 year = date.year | |
| 226 | |
|
87
77f2478ae971
getDateString method to format dates in English and German.
casties
parents:
65
diff
changeset
|
227 if lang.lower() == 'en': |
|
231
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
228 if short: |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
229 ds = "%s/%s/%s"%(year,month,day) |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
230 else: |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
231 ds = "%s %s"%(getMonthName(month, lang), day) |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
232 if withYear: |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
233 ds += ", %s"%year |
| 137 | 234 |
| 235 elif lang.lower() == 'de': | |
|
231
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
236 if short: |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
237 ds = "%s.%s.%s"%(day,month,year) |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
238 else: |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
239 ds = "%s. %s"%(day, getMonthName(month, lang)) |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
240 if withYear: |
|
649f74b167e6
extract getPlaintext to SrvTxtUtils. add character count to project description editor.
casties
parents:
216
diff
changeset
|
241 ds += " %s"%year |
| 137 | 242 |
| 243 elif lang.lower() == 'iso': | |
| 244 ds = date.isoformat() | |
|
87
77f2478ae971
getDateString method to format dates in English and German.
casties
parents:
65
diff
changeset
|
245 |
|
77f2478ae971
getDateString method to format dates in English and German.
casties
parents:
65
diff
changeset
|
246 return ds |
|
77f2478ae971
getDateString method to format dates in English and German.
casties
parents:
65
diff
changeset
|
247 |
|
77f2478ae971
getDateString method to format dates in English and German.
casties
parents:
65
diff
changeset
|
248 |
| 216 | 249 def getDate(date): |
| 250 """return date object from date or datetime date.""" | |
| 251 if isinstance(date, datetime.datetime): | |
| 252 # strip time | |
| 253 return date.date() | |
| 254 | |
| 255 return date | |
| 256 | |
| 257 | |
| 258 def getDatetime(date): | |
| 259 """return datetime object from date or datetime date.""" | |
| 260 if isinstance(date, datetime.date): | |
| 261 # add time 0:00 | |
| 262 return datetime.datetime.combine(date, datetime.time()) | |
| 263 | |
| 264 return date | |
| 265 | |
| 266 | |
| 267 def getHttpData(url, data=None, num_tries=3, timeout=10, username=None, password=None, cache=None, insecure=False, noExceptions=False): | |
|
0
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
268 """returns result from url+data HTTP request""" |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
269 # we do GET (by appending data to url) |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
270 if isinstance(data, str) or isinstance(data, unicode): |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
271 # if data is string then append |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
272 url = "%s?%s"%(url,data) |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
273 elif isinstance(data, dict) or isinstance(data, list) or isinstance(data, tuple): |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
274 # urlencode |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
275 url = "%s?%s"%(url,urllib.urlencode(data)) |
| 216 | 276 |
|
0
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
277 errmsg = None |
| 216 | 278 if httplib == 'httplib2': |
| 279 # use httplib2 | |
| 280 for cnt in range(num_tries): | |
| 281 try: | |
| 282 logging.debug("getHttp(lib2)Data(#%s %ss) url=%s"%(cnt+1,timeout,url)) | |
| 283 h = httplib2.Http(cache=cache, timeout=float(timeout), disable_ssl_certificate_validation=insecure) | |
| 284 if username: | |
| 285 h.add_credentials(username, password) | |
| 286 | |
| 287 resp, data = h.request(url) | |
| 288 return data | |
| 289 | |
| 290 except httplib2.HttpLib2Error, e: | |
| 291 logging.error("getHttp(lib2)Data: HTTP error(%s): %s"%(e.code,e)) | |
| 292 errmsg = str(e) | |
| 293 # stop trying | |
| 294 break | |
| 295 | |
| 296 else: | |
| 297 # use urllib2 | |
| 298 response = None | |
| 299 for cnt in range(num_tries): | |
| 300 try: | |
| 301 logging.debug("getHttpData(#%s %ss) url=%s"%(cnt+1,timeout,url)) | |
| 302 if sys.version_info < (2, 6): | |
| 303 # set timeout on socket -- ugly :-( | |
| 304 import socket | |
| 305 socket.setdefaulttimeout(float(timeout)) | |
| 306 response = urllib2.urlopen(url) | |
| 307 else: | |
| 308 # timeout as parameter | |
| 309 response = urllib2.urlopen(url,timeout=float(timeout)) | |
| 310 # check result? | |
| 311 data = response.read() | |
| 312 response.close() | |
| 313 return data | |
|
0
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
314 |
| 216 | 315 except urllib2.HTTPError, e: |
| 316 logging.error("getHttpData: HTTP error(%s): %s"%(e.code,e)) | |
| 317 errmsg = str(e) | |
| 318 # stop trying | |
| 319 break | |
| 320 except urllib2.URLError, e: | |
| 321 logging.error("getHttpData: URLLIB error(%s): %s"%(e.reason,e)) | |
| 322 errmsg = str(e) | |
| 323 # stop trying | |
| 324 #break | |
|
0
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
325 |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
326 if noExceptions: |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
327 return None |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
328 |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
329 raise IOError("ERROR fetching HTTP data from %s: %s"%(url,errmsg)) |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
330 #return None |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
331 |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
332 |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
333 def refreshingImageFileIndexHtml(self, REQUEST, RESPONSE): |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
334 """index_html method for App.ImageFile that updates the file info for each request.""" |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
335 stat_info = os.stat(self.path) |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
336 self.size = stat_info[stat.ST_SIZE] |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
337 self.lmt = float(stat_info[stat.ST_MTIME]) or time.time() |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
338 self.lmh = rfc1123_date(self.lmt) |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
339 # call original method |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
340 return ImageFile.index_html(self, REQUEST, RESPONSE) |
|
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
341 |
| 65 | 342 |
| 343 def shortenString(s, l, ellipsis='...'): | |
| 344 """returns a string of length l (or l-1) by omitting characters in the middle of s, replacing with ellipsis.""" | |
| 207 | 345 if len(s) <= l: |
| 346 return s | |
| 347 | |
| 65 | 348 l1 = int((l - len(ellipsis)) / 2) |
| 349 return "%s%s%s"%(s[:l1],ellipsis,s[-l1:]) | |
| 350 | |
|
188
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
351 |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
352 def sqlName(s, lc=True, more=''): |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
353 """returns restricted ASCII-only version of string""" |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
354 if s is None: |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
355 return "" |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
356 |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
357 if not isinstance(s, basestring): |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
358 # make string object |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
359 s = str(s) |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
360 |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
361 # remove ' |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
362 s = s.replace("'","") |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
363 # all else -> "_" |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
364 s = re.sub('[^A-Za-z0-9_'+more+']','_',s) |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
365 if lc: |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
366 return s.lower() |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
367 |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
368 return s |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
369 |
|
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
370 |
