~jelmer/loggerhead/breezy

« back to all changes in this revision

Viewing changes to loggerhead/util.py

  • Committer: Robey Pointer
  • Date: 2006-12-11 06:44:19 UTC
  • Revision ID: robey@lag.net-20061211064419-8ssa7mlsiflpmy0c
initial checkin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright (C) 2006  Robey Pointer <robey@lag.net>
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
#
 
18
 
 
19
import re
 
20
import sha
 
21
 
 
22
 
 
23
def timespan(delta):
 
24
    if delta.days >= 3:
 
25
        return '%d days' % delta.days
 
26
    seg = []
 
27
    if delta.days > 0:
 
28
        if delta.days == 1:
 
29
            seg.append('1 day')
 
30
        else:
 
31
            seg.append('%d days' % delta.days)
 
32
    hrs = delta.seconds // 3600
 
33
    mins = (delta.seconds % 3600) // 60
 
34
    if hrs > 0:
 
35
        if hrs == 1:
 
36
            seg.append('1 hour')
 
37
        else:
 
38
            seg.append('%d hours' % hrs)
 
39
    if delta.days == 0:
 
40
        if mins > 0:
 
41
            if mins == 1:
 
42
                seg.append('1 minute')
 
43
            else:
 
44
                seg.append('%d minutes' % mins)
 
45
        elif hrs == 0:
 
46
            seg.append('less than a minute')
 
47
    return ', '.join(seg)
 
48
 
 
49
 
 
50
class Container (object):
 
51
    """
 
52
    Convert a dict into an object with attributes.
 
53
    """
 
54
    def __init__(self, _dict=None, **kw):
 
55
        if _dict is not None:
 
56
            for key, value in _dict.iteritems():
 
57
                setattr(self, key, value)
 
58
        for key, value in kw.iteritems():
 
59
            setattr(self, key, value)
 
60
 
 
61
 
 
62
def clean_revid(revid):
 
63
    if revid == 'missing':
 
64
        return revid
 
65
    return sha.new(revid).hexdigest()
 
66
 
 
67
 
 
68
def obfuscate(text):
 
69
    return ''.join([ '&#%d;' % ord(c) for c in text ])
 
70
 
 
71
 
 
72
STANDARD_PATTERN = re.compile(r'^(.*?)\s*<(.*?)>\s*$')
 
73
EMAIL_PATTERN = re.compile(r'[-\w\d\+_!%\.]+@[-\w\d\+_!%\.]+')
 
74
 
 
75
def hide_email(email):
 
76
    """
 
77
    try to obsure any email address in a bazaar committer's name.
 
78
    """
 
79
    m = STANDARD_PATTERN.search(email)
 
80
    if m is not None:
 
81
        name = m.group(1)
 
82
        email = m.group(2)
 
83
        return name
 
84
    m = EMAIL_PATTERN.search(email)
 
85
    if m is None:
 
86
        # can't find an email address in here
 
87
        return email
 
88
    username, domain = m.group(0).split('@')
 
89
    domains = domain.split('.')
 
90
    if len(domains) >= 2:
 
91
        return '%s at %s' % (username, domains[-2])
 
92
    return '%s at %s' % (username, domains[0])
 
93
 
 
94
    
 
95
def triple_factors():
 
96
    factors = (1, 3)
 
97
    index = 0
 
98
    n = 1
 
99
    while True:
 
100
        yield n * factors[index]
 
101
        index += 1
 
102
        if index >= len(factors):
 
103
            index = 0
 
104
            n *= 10
 
105
 
 
106
 
 
107
def scan_range(pos, max):
 
108
    """
 
109
    given a position in a maximum range, return a list of negative and positive
 
110
    jump factors for an hgweb-style triple-factor geometric scan.
 
111
    
 
112
    for example, with pos=20 and max=500, the range would be:
 
113
    [ -10, -3, -1, 1, 3, 10, 30, 100, 300 ]
 
114
    
 
115
    i admit this is a very strange way of jumping through revisions.  i didn't
 
116
    invent it. :)
 
117
    """
 
118
    out = []
 
119
    for n in triple_factors():
 
120
        if n > max:
 
121
            return out
 
122
        if pos + n < max:
 
123
            out.append(n)
 
124
        if pos - n >= 0:
 
125
            out.insert(0, -n)
 
126