OLD | NEW |
1 # coding: utf-8 | 1 # coding: utf-8 |
2 | 2 |
3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
4 # Copyright (C) 2006-2013 Eyeo GmbH | 4 # Copyright (C) 2006-2013 Eyeo GmbH |
5 # | 5 # |
6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify |
7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as |
8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
9 # | 9 # |
10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 # GNU General Public License for more details. | 13 # GNU General Public License for more details. |
14 # | 14 # |
15 # You should have received a copy of the GNU General Public License | 15 # You should have received a copy of the GNU General Public License |
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 |
18 import re, email.header, urllib, time, json | 18 import re, email.header, urllib, time, json |
19 from datetime import date | 19 from datetime import date |
20 from jinja2.utils import Markup | 20 from jinja2.utils import Markup |
21 from urlparse import urlparse | 21 from urlparse import urlparse |
22 | 22 |
23 def formattime(value): | 23 def formattime(value): |
24 try: | 24 try: |
25 return time.strftime('%Y-%m-%d %H:%M GMT', time.gmtime(int(value))) | 25 return time.strftime('%Y-%m-%d %H:%M UTC', time.gmtime(int(value))) |
26 except Exception, e: | 26 except Exception, e: |
27 return 'unknown' | 27 return 'unknown' |
28 | 28 |
29 def formatrelativetime(value, baseTime): | 29 def formatrelativetime(value, baseTime): |
30 try: | 30 try: |
31 value = float(value) | 31 value = float(value) |
32 params = {'title': formattime(baseTime + value), 'number': value, 'prefix':
'in ', 'suffix': '', 'unit': 'second(s)'} | 32 params = {'title': formattime(baseTime + value), 'number': value, 'prefix':
'in ', 'suffix': '', 'unit': 'second(s)'} |
33 if params['number'] < 0: | 33 if params['number'] < 0: |
34 params['prefix'] = '' | 34 params['prefix'] = '' |
35 params['suffix'] = ' ago' | 35 params['suffix'] = ' ago' |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 | 133 |
134 def rjust(value, width=80): | 134 def rjust(value, width=80): |
135 return unicode(value).rjust(width) | 135 return unicode(value).rjust(width) |
136 | 136 |
137 def ltruncate(value, length=255, end='...'): | 137 def ltruncate(value, length=255, end='...'): |
138 value = unicode(value) | 138 value = unicode(value) |
139 if len(value) <= length: | 139 if len(value) <= length: |
140 return value | 140 return value |
141 return end + value[len(value) - length:len(value)] | 141 return end + value[len(value) - length:len(value)] |
142 | 142 |
143 def formatmonthname(value, format='%b %Y'): | |
144 return date(int(value[0:4]), int(value[4:]), 1).strftime(format) | |
145 | |
146 def formatweekday(value): | 143 def formatweekday(value): |
147 return time.strftime('%a', (0, 0, 0, 0, 0, 0, value, 0, 0)) | 144 return time.strftime('%a', (0, 0, 0, 0, 0, 0, value, 0, 0)) |
148 | 145 |
149 def formatbytes(value): | 146 def formatbytes(value): |
150 if value == 0: | 147 if value == 0: |
151 return '0' | 148 return '0' |
152 | 149 |
153 value = float(value) | 150 value = float(value) |
154 unit = 'Bytes' | 151 unit = 'Bytes' |
155 if value > 1024: | 152 if value > 1024: |
156 value /= 1024 | 153 value /= 1024 |
157 unit = 'KB' | 154 unit = 'KB' |
158 if value > 1024: | 155 if value > 1024: |
159 value /= 1024 | 156 value /= 1024 |
160 unit = 'MB' | 157 unit = 'MB' |
161 if value > 1024: | 158 if value > 1024: |
162 value /= 1024 | 159 value /= 1024 |
163 unit = 'GB' | 160 unit = 'GB' |
164 return '%.2f %s' % (value, unit) | 161 return '%.2f %s' % (value, unit) |
165 | 162 |
166 def getsum(iterable, attribute=None): | |
167 if attribute == None: | |
168 return sum(iterable) | |
169 else: | |
170 return sum(item[attribute] for item in iterable) | |
171 | |
172 def getmax(iterable, attribute=None): | |
173 if len(iterable) == 0: | |
174 return 0 | |
175 elif attribute == None: | |
176 return max(iterable) | |
177 else: | |
178 return max(iterable, key=lambda item: item[attribute])[attribute] | |
179 | |
180 def ensuremin(value, minvalue=1): | |
181 return max(value, minvalue) | |
182 | |
183 def toJSON(value, **args): | 163 def toJSON(value, **args): |
184 return re.sub(r'</script>', r'<\/script>', json.dumps(value, **args)) | 164 return re.sub(r'</script>', r'<\/script>', json.dumps(value, **args)) |
185 | 165 |
186 filters = { | 166 filters = { |
187 'formattime': formattime, | 167 'formattime': formattime, |
188 'timerelative': formatrelativetime, | 168 'timerelative': formatrelativetime, |
189 'url': formaturl, | 169 'url': formaturl, |
190 'keepnewlines': formatnewlines, | 170 'keepnewlines': formatnewlines, |
191 'filtercount': formatfiltercount, | 171 'filtercount': formatfiltercount, |
192 'buglinks': formatBugLinks, | 172 'buglinks': formatBugLinks, |
193 'urlencode': urlencode, | 173 'urlencode': urlencode, |
194 'subscriptionSort': subscriptionSort, | 174 'subscriptionSort': subscriptionSort, |
195 'mime': formatmime, | 175 'mime': formatmime, |
196 'ljust': ljust, | 176 'ljust': ljust, |
197 'rjust': rjust, | 177 'rjust': rjust, |
198 'ltruncate': ltruncate, | 178 'ltruncate': ltruncate, |
199 'weekday': formatweekday, | 179 'weekday': formatweekday, |
200 'monthname': formatmonthname, | |
201 'bytes': formatbytes, | 180 'bytes': formatbytes, |
202 'sum': getsum, | |
203 'max': getmax, | |
204 'ensuremin': ensuremin, | |
205 'json': toJSON, | 181 'json': toJSON, |
206 } | 182 } |
OLD | NEW |