import urllib
from hashlib import md5

_valid_gravatar_sizes = (32, 64, 140)


def gravatar_url(username, size=64, default=None):
    try:
        import fedora.client
        system = fedora.client.AccountSystem()
        return system.gravatar_url(
            username, size, default, lookup_email=False)
    except Exception:
        return _gravatar_url(username, size, default)


def _gravatar_url(username, size=64, default=None):
    """
    Our own implementation since fas doesn't support this nicely yet.
    """

    if size not in _valid_gravatar_sizes:
        raise ValueError(b_('Size %(size)i disallowed.  Must be in'
            ' %(valid_sizes)r') % { 'size': size,
                'valid_sizes': _valid_gravatar_sizes})

    if not default:
        default = "http://fedoraproject.org/static/images/" + \
                "fedora_infinity_%ix%i.png" % (size, size)

    query_string = urllib.urlencode({
        's': size,
        'd': default,
    })

    email = "%s@fedoraproject.org" % username
    hash = md5(email).hexdigest()

    return "http://www.gravatar.com/avatar/%s?%s" % (hash, query_string)

@@ -323,16 +323,64 @@
 eption:%0A
+        email = username + %22@fedoraproject.org%22%0A
         
@@ -386,17 +386,16 @@
  return 
-_
 gravatar
@@ -394,33 +394,41 @@
 gravatar_url
-(username
+_from_email(email
 , size, defa
@@ -438,17 +438,16 @@
 )%0A%0A%0Adef 
-_
 gravatar
@@ -446,33 +446,41 @@
 gravatar_url
-(username
+_from_email(email
 , size=64, d
@@ -650,16 +650,29 @@
 rror(b_(
+%0A            
 'Size %25(
@@ -705,23 +705,8 @@
 e in
-'%0A            '
  %25(v
@@ -727,23 +727,11 @@
  %25 %7B
- 'size': size,%0A
+%0A  
     
@@ -740,17 +740,29 @@
         
- 
+'size': size,
  'valid_
@@ -878,16 +878,18 @@
 s/%22 + %5C%0A
+  
         
@@ -940,16 +940,396 @@
  size)%0A%0A
+    return _kernel(email, size, default, service='gravatar')%0A%0A%0Adef _kernel(email, size, default, service='gravatar'):%0A    %22%22%22 Copy-and-paste of some code from python-fedora. %22%22%22%0A%0A    if service == 'libravatar':%0A        import libravatar%0A        return libravatar.libravatar_url(%0A            email=email,%0A            size=size,%0A            default=default,%0A        )%0A    else:%0A    
     quer
@@ -1358,16 +1358,20 @@
 ncode(%7B%0A
+    
         
@@ -1389,16 +1389,20 @@
         
+    
 'd': def
@@ -1403,24 +1403,28 @@
 ': default,%0A
+    
     %7D)%0A%0A    
@@ -1427,50 +1427,8 @@
     
-email = %22%25s@fedoraproject.org%22 %25 username%0A
     
@@ -1458,16 +1458,20 @@
 gest()%0A%0A
+    
     retu

Reorganize fas shim to: