Mercurial > hg > MPIWGWeb
annotate SrvTxtUtils.py @ 188:16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
author | casties |
---|---|
date | Fri, 14 Jun 2013 17:36:09 +0200 |
parents | 485bf377913a |
children | 938add25f81b |
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 urllib2 |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
11 import logging |
65 | 12 import time |
188
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
13 import re |
0
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
14 |
49 | 15 import xml.etree.ElementTree as ET |
16 | |
188
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
17 srvTxtUtilsVersion = "1.10.0" |
0
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
18 |
16 | 19 map_months = {'en': [u"", |
20 u"January", | |
21 u"February", | |
22 u"March", | |
23 u"April", | |
24 u"May", | |
25 u"June", | |
26 u"July", | |
27 u"August", | |
28 u"September", | |
29 u"October", | |
30 u"November", | |
31 u"December"], | |
32 'de': [u"", | |
33 u"Januar", | |
34 u"Februar", | |
35 u"M\u00e4rz", | |
36 u"April", | |
37 u"Mai", | |
38 u"Juni", | |
39 u"Juli", | |
40 u"August", | |
41 u"September", | |
42 u"Oktober", | |
43 u"November", | |
44 u"Dezember"]} | |
0
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
45 |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
46 def getInt(number, default=0): |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
47 """returns always an int (0 in case of problems)""" |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
48 try: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
49 return int(number) |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
50 except: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
51 return int(default) |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
52 |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
53 def getAt(array, idx, default=None): |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
54 """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
|
55 try: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
56 return array[idx] |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
57 except: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
58 return default |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
59 |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
60 def unicodify(s): |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
61 """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
|
62 if not s: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
63 return u"" |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
64 if isinstance(s, str): |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
65 try: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
66 return s.decode('utf-8') |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
67 except: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
68 return s.decode('latin-1') |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
69 else: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
70 return s |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
71 |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
72 def utf8ify(s): |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
73 """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
|
74 assumes string objects to be utf-8""" |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
75 if not s: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
76 return "" |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
77 if isinstance(s, str): |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
78 return s |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
79 else: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
80 return s.encode('utf-8') |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
81 |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
82 def getText(node, recursive=0): |
49 | 83 """returns all text content of a (etree) node and its subnodes""" |
0
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
84 if node is None: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
85 return '' |
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 # ElementTree: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
88 text = node.text or '' |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
89 for e in node: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
90 if recursive: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
91 text += getText(e) |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
92 else: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
93 text += e.text or '' |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
94 if e.tail: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
95 text += e.tail |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
96 |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
97 # 4Suite: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
98 #nodelist=node.childNodes |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
99 #text = "" |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
100 #for n in nodelist: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
101 # if n.nodeType == node.TEXT_NODE: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
102 # text = text + n.data |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
103 |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
104 return text |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
105 |
49 | 106 |
107 def serialize(node): | |
108 """returns a string containing an XML snippet of (etree) node""" | |
109 s = ET.tostring(node, 'UTF-8') | |
110 # snip off XML declaration | |
111 if s.startswith('<?xml'): | |
112 i = s.find('?>') | |
113 return s[i+3:] | |
114 | |
115 return s | |
116 | |
117 | |
16 | 118 def getMonthName(mon, lang): |
119 """returns the name of the month mon in the language lang""" | |
120 return map_months[lang][mon] | |
0
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
121 |
87
77f2478ae971
getDateString method to format dates in English and German.
casties
parents:
65
diff
changeset
|
122 |
137 | 123 def getDateString(date=None, lang='en', withYear=True, monthNames=True, abbrev=False): |
87
77f2478ae971
getDateString method to format dates in English and German.
casties
parents:
65
diff
changeset
|
124 """Return formatted date string.""" |
170 | 125 if date is None: |
126 return None | |
127 | |
87
77f2478ae971
getDateString method to format dates in English and German.
casties
parents:
65
diff
changeset
|
128 ds = None |
126 | 129 if callable(date.day): |
130 # callable members | |
131 day = date.day() | |
132 month = date.month() | |
133 year = date.year() | |
134 else: | |
135 # data members | |
136 day = date.day | |
137 month = date.month | |
138 year = date.year | |
139 | |
87
77f2478ae971
getDateString method to format dates in English and German.
casties
parents:
65
diff
changeset
|
140 if lang.lower() == 'en': |
137 | 141 ds = "%s %s"%(getMonthName(month, lang), day) |
142 if withYear: | |
143 ds += ", %s"%year | |
144 | |
145 elif lang.lower() == 'de': | |
146 ds = "%s. %s"%(day, getMonthName(month, lang)) | |
87
77f2478ae971
getDateString method to format dates in English and German.
casties
parents:
65
diff
changeset
|
147 if withYear: |
126 | 148 ds += " %s"%year |
137 | 149 |
150 elif lang.lower() == 'iso': | |
151 ds = date.isoformat() | |
87
77f2478ae971
getDateString method to format dates in English and German.
casties
parents:
65
diff
changeset
|
152 |
77f2478ae971
getDateString method to format dates in English and German.
casties
parents:
65
diff
changeset
|
153 return ds |
77f2478ae971
getDateString method to format dates in English and German.
casties
parents:
65
diff
changeset
|
154 |
77f2478ae971
getDateString method to format dates in English and German.
casties
parents:
65
diff
changeset
|
155 |
0
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
156 def getHttpData(url, data=None, num_tries=3, timeout=10, noExceptions=False): |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
157 """returns result from url+data HTTP request""" |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
158 # we do GET (by appending data to url) |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
159 if isinstance(data, str) or isinstance(data, unicode): |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
160 # if data is string then append |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
161 url = "%s?%s"%(url,data) |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
162 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
|
163 # urlencode |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
164 url = "%s?%s"%(url,urllib.urlencode(data)) |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
165 |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
166 response = None |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
167 errmsg = None |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
168 for cnt in range(num_tries): |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
169 try: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
170 logging.debug("getHttpData(#%s %ss) url=%s"%(cnt+1,timeout,url)) |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
171 if sys.version_info < (2, 6): |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
172 # set timeout on socket -- ugly :-( |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
173 import socket |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
174 socket.setdefaulttimeout(float(timeout)) |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
175 response = urllib2.urlopen(url) |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
176 else: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
177 # timeout as parameter |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
178 response = urllib2.urlopen(url,timeout=float(timeout)) |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
179 # check result? |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
180 break |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
181 except urllib2.HTTPError, e: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
182 logging.error("getHttpData: HTTP error(%s): %s"%(e.code,e)) |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
183 errmsg = str(e) |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
184 # stop trying |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
185 break |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
186 except urllib2.URLError, e: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
187 logging.error("getHttpData: URLLIB error(%s): %s"%(e.reason,e)) |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
188 errmsg = str(e) |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
189 # stop trying |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
190 #break |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
191 |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
192 if response is not None: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
193 data = response.read() |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
194 response.close() |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
195 return data |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
196 |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
197 if noExceptions: |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
198 return None |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
199 |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
200 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
|
201 #return None |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
202 |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
203 |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
204 def refreshingImageFileIndexHtml(self, REQUEST, RESPONSE): |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
205 """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
|
206 stat_info = os.stat(self.path) |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
207 self.size = stat_info[stat.ST_SIZE] |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
208 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
|
209 self.lmh = rfc1123_date(self.lmt) |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
210 # call original method |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
211 return ImageFile.index_html(self, REQUEST, RESPONSE) |
bca61e893fcc
first checkin of MPIWGWeb r2 branch from CVS into mercurial
casties
parents:
diff
changeset
|
212 |
65 | 213 |
214 def shortenString(s, l, ellipsis='...'): | |
215 """returns a string of length l (or l-1) by omitting characters in the middle of s, replacing with ellipsis.""" | |
216 l1 = int((l - len(ellipsis)) / 2) | |
217 return "%s%s%s"%(s[:l1],ellipsis,s[-l1:]) | |
218 | |
188
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
219 |
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
220 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
|
221 """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
|
222 if s is None: |
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
223 return "" |
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
224 |
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
225 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
|
226 # make string object |
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
227 s = str(s) |
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
228 |
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
229 # remove ' |
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
230 s = s.replace("'","") |
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
231 # all else -> "_" |
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
232 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
|
233 if lc: |
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
234 return s.lower() |
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
235 |
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
236 return s |
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
237 |
16d55695f1c8
fix updating staff entries through "Update personal homepages". (creating new entries doesn't work)
casties
parents:
170
diff
changeset
|
238 |