~noskcaj/ubuntu/vivid/fedmsg-meta-fedora-infrastructure/disable-tests

« back to all changes in this revision

Viewing changes to fedmsg_meta_fedora_infrastructure/fasshim.py

  • Committer: Package Import Robot
  • Author(s): Nicolas Dandrimont
  • Date: 2014-05-18 18:33:16 UTC
  • Revision ID: package-import@ubuntu.com-20140518183316-9pbnt2o7lxkoaspc
Tags: upstream-0.2.12
ImportĀ upstreamĀ versionĀ 0.2.12

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import urllib
 
2
import socket
 
3
from hashlib import md5
 
4
 
 
5
_valid_gravatar_sizes = (32, 64, 140)
 
6
 
 
7
_fas_cache = {}
 
8
 
 
9
import logging
 
10
log = logging.getLogger("moksha.hub")
 
11
 
 
12
 
 
13
def gravatar_url(username, size=64, default=None):
 
14
    try:
 
15
        import fedora.client
 
16
        system = fedora.client.AccountSystem()
 
17
        return system.gravatar_url(
 
18
            username, size, default, lookup_email=False)
 
19
    except Exception:
 
20
        email = username + "@fedoraproject.org"
 
21
        return gravatar_url_from_email(email, size, default)
 
22
 
 
23
 
 
24
def gravatar_url_from_email(email, size=64, default=None):
 
25
    """
 
26
    Our own implementation since fas doesn't support this nicely yet.
 
27
    """
 
28
 
 
29
    if size not in _valid_gravatar_sizes:
 
30
        raise ValueError(b_(
 
31
            'Size %(size)i disallowed.  Must be in %(valid_sizes)r') % {
 
32
                'size': size, 'valid_sizes': _valid_gravatar_sizes})
 
33
 
 
34
    if not default:
 
35
        default = ("http://fedoraproject.org/static/images/"
 
36
                   "fedora_infinity_%ix%i.png" % (size, size))
 
37
 
 
38
    return _kernel(email, size, default, service='gravatar')
 
39
 
 
40
 
 
41
def _kernel(email, size, default, service='gravatar'):
 
42
    """ Copy-and-paste of some code from python-fedora. """
 
43
 
 
44
    if service == 'libravatar':
 
45
        import libravatar
 
46
        return libravatar.libravatar_url(
 
47
            email=email,
 
48
            size=size,
 
49
            default=default,
 
50
        )
 
51
    else:
 
52
        query_string = urllib.urlencode({
 
53
            's': size,
 
54
            'd': default,
 
55
        })
 
56
 
 
57
        hash = md5(email).hexdigest()
 
58
 
 
59
        return "http://www.gravatar.com/avatar/%s?%s" % (hash, query_string)
 
60
 
 
61
 
 
62
def make_fas_cache(**config):
 
63
    global _fas_cache
 
64
    if _fas_cache:
 
65
        return _fas_cache
 
66
 
 
67
    log.warn("No previous fas cache found.  Looking to rebuild.")
 
68
 
 
69
    try:
 
70
        import fedora.client
 
71
        import fedora.client.fas2
 
72
    except ImportError:
 
73
        log.warn("No python-fedora installed.  Not caching fas.")
 
74
        return {}
 
75
 
 
76
    if not 'fas_credentials' in config:
 
77
        log.warn("No fas_credentials found.  Not caching fas.")
 
78
        return {}
 
79
 
 
80
    creds = config['fas_credentials']
 
81
 
 
82
    fasclient = fedora.client.fas2.AccountSystem(
 
83
        username=creds['username'],
 
84
        password=creds['password'],
 
85
    )
 
86
 
 
87
    timeout = socket.getdefaulttimeout()
 
88
    socket.setdefaulttimeout(600)
 
89
    try:
 
90
        log.info("Downloading FAS cache")
 
91
        request = fasclient.send_request('/user/list',
 
92
                                         req_params={'search': '*'},
 
93
                                         auth=True)
 
94
    except fedora.client.ServerError as e:
 
95
        log.error("Failed to download fas cache %r" % e)
 
96
        return {}
 
97
    finally:
 
98
        socket.setdefaulttimeout(timeout)
 
99
 
 
100
    log.info("Caching necessary user data")
 
101
    for user in request['people']:
 
102
        nick = user['ircnick']
 
103
        if nick:
 
104
            _fas_cache[nick] = user['username']
 
105
 
 
106
        email = user['email']
 
107
        if email:
 
108
            _fas_cache[email] = user['username']
 
109
 
 
110
    del request
 
111
    del fasclient
 
112
    del fedora.client.fas2
 
113
 
 
114
    return _fas_cache
 
115
 
 
116
 
 
117
def nick2fas(nickname, **config):
 
118
    fas_cache = make_fas_cache(**config)
 
119
    return fas_cache.get(nickname, nickname)
 
120
 
 
121
 
 
122
def email2fas(email, **config):
 
123
    fas_cache = make_fas_cache(**config)
 
124
    return fas_cache.get(email, email)