~malept/loggerhead/standalone-auth

« back to all changes in this revision

Viewing changes to loggerhead/util.py

  • Committer: Martin Albisetti
  • Date: 2008-07-22 00:40:03 UTC
  • mfrom: (182 loggerhead.search_integration)
  • mto: This revision was merged to the branch mainline in revision 187.
  • Revision ID: argentina@gmail.com-20080722004003-lx1fp0tthpp6kl92
Merged from trunk, resolved billions of conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import datetime
28
28
import logging
29
29
import re
30
 
import sha
31
30
import struct
32
 
import sys
33
31
import threading
34
32
import time
35
 
import traceback
36
 
import types
37
33
 
38
34
log = logging.getLogger("loggerhead.controllers")
39
35
 
142
138
        return out
143
139
 
144
140
 
145
 
def clean_revid(revid):
146
 
    if revid == 'missing':
147
 
        return revid
148
 
    return sha.new(revid).hexdigest()
149
 
 
150
 
 
151
 
def obfuscate(text):
152
 
    return ''.join([ '&#%d;' % ord(c) for c in text ])
153
 
 
154
 
 
155
141
def trunc(text, limit=10):
156
142
    if len(text) <= limit:
157
143
        return text
158
144
    return text[:limit] + '...'
159
145
 
160
146
 
161
 
def to_utf8(s):
162
 
    if isinstance(s, unicode):
163
 
        return s.encode('utf-8')
164
 
    return s
165
 
 
166
 
 
167
147
STANDARD_PATTERN = re.compile(r'^(.*?)\s*<(.*?)>\s*$')
168
148
EMAIL_PATTERN = re.compile(r'[-\w\d\+_!%\.]+@[-\w\d\+_!%\.]+')
169
149
 
187
167
    return '%s at %s' % (username, domains[0])
188
168
 
189
169
 
190
 
def triple_factors(min_value=1):
191
 
    factors = (1, 3)
192
 
    index = 0
193
 
    n = 1
194
 
    while True:
195
 
        if n >= min_value:
196
 
            yield n * factors[index]
197
 
        index += 1
198
 
        if index >= len(factors):
199
 
            index = 0
200
 
            n *= 10
201
 
 
202
 
 
203
 
def scan_range(pos, max, pagesize=1):
204
 
    """
205
 
    given a position in a maximum range, return a list of negative and positive
206
 
    jump factors for an hgweb-style triple-factor geometric scan.
207
 
 
208
 
    for example, with pos=20 and max=500, the range would be:
209
 
    [ -10, -3, -1, 1, 3, 10, 30, 100, 300 ]
210
 
 
211
 
    i admit this is a very strange way of jumping through revisions.  i didn't
212
 
    invent it. :)
213
 
    """
214
 
    out = []
215
 
    for n in triple_factors(pagesize + 1):
216
 
        if n > max:
217
 
            return out
218
 
        if pos + n < max:
219
 
            out.append(n)
220
 
        if pos - n >= 0:
221
 
            out.insert(0, -n)
222
 
 
223
 
 
224
170
# only do this if unicode turns out to be a problem
225
171
#_BADCHARS_RE = re.compile(ur'[\u007f-\uffff]')
226
172
 
276
222
    return s.expandtabs().replace(' ', NONBREAKING_SPACE)
277
223
 
278
224
 
279
 
def if_present(format, value):
280
 
    """
281
 
    format a value using a format string, if the value exists and is not None.
282
 
    """
283
 
    if value is None:
284
 
        return ''
285
 
    return format % value
 
225
def fake_permissions(kind, executable):
 
226
    # fake up unix-style permissions given only a "kind" and executable bit
 
227
    if kind == 'directory':
 
228
        return 'drwxr-xr-x'
 
229
    if executable:
 
230
        return '-rwxr-xr-x'
 
231
    return '-rw-r--r--'
286
232
 
287
233
 
288
234
def b64(s):
367
313
    navigation.last_in_page_revid = get_offset(navigation.pagesize - 1)
368
314
    navigation.prev_page_revid = get_offset(-1 * navigation.pagesize)
369
315
    navigation.next_page_revid = get_offset(1 * navigation.pagesize)
370
 
    prev_page_revno = navigation.branch.history.get_revno(
371
 
            navigation.prev_page_revid)
372
 
    next_page_revno = navigation.branch.history.get_revno(
373
 
            navigation.next_page_revid)
374
 
    start_revno = navigation.branch._history.get_revno(navigation.start_revid)
375
 
 
376
 
    prev_page_revno = navigation.branch._history.get_revno(
377
 
            navigation.prev_page_revid)
378
 
    next_page_revno = navigation.branch._history.get_revno(
379
 
            navigation.next_page_revid)
 
316
    prev_page_revno = navigation.history.get_revno(
 
317
            navigation.prev_page_revid)
 
318
    next_page_revno = navigation.history.get_revno(
 
319
            navigation.next_page_revid)
 
320
    start_revno = navigation.history.get_revno(navigation.start_revid)
380
321
 
381
322
    params = { 'filter_file_id': navigation.filter_file_id }
382
323
    if getattr(navigation, 'query', None) is not None:
393
334
            [navigation.scan_url, next_page_revno], **params)
394
335
 
395
336
 
396
 
def log_exception(log):
397
 
    for line in ''.join(traceback.format_exception(*sys.exc_info())).split('\n'):
398
 
        log.debug(line)
399
 
 
400
 
 
401
337
def decorator(unbound):
402
338
    def new_decorator(f):
403
339
        g = unbound(f)
427
363
    return _decorator
428
364
 
429
365
 
430
 
 
431
366
@decorator
432
367
def lsprof(f):
433
368
    def _f(*a, **kw):