grim/hggravatar

1edb76f6931b
Parents bfae328cdd61
Children
Make this work with modern hg and some other stuff I hadn't commited yet
  • +40 -34
    hggravatar.py
  • --- a/hggravatar.py Thu Dec 20 01:44:41 2018 -0600
    +++ b/hggravatar.py Wed Oct 07 03:15:45 2020 -0500
    @@ -7,6 +7,8 @@
    import os.path
    import shutil
    +from collections import namedtuple
    +
    from mercurial.i18n import _
    from mercurial import (
    cmdutil,
    @@ -17,7 +19,7 @@
    )
    from mercurial.utils import stringutil
    -_CACHE_DIR = '.hg/cache/gravatar'
    +_CACHE_DIR = b'.hg/cache/gravatar'
    urlreq = util.urlreq
    urlencode = urlreq.urlencode
    @@ -25,25 +27,29 @@
    cmdtable = {}
    command = registrar.command(cmdtable)
    +
    def find_authors(ui, repo, *pats, **opts):
    - ui.write('caching authors ... ')
    + ui.write(b'caching authors ... ')
    # now walk the dag
    m = scmutil.match(repo[None], pats, opts)
    - authors = {}
    + authors = {}
    def prep(ctx, fns):
    - """callback for cmdutil.walkchangerevs that just adds unique email
    - address to the authors dict."""
    - email = stringutil.email(ctx.user())
    - normalized = email.lower().strip()
    - if not email in authors:
    - authors[email] = hashlib.md5(normalized).hexdigest()
    + """callback for cmdutil.walkchangerevs that just adds unique email
    + address to the authors dict."""
    + email = stringutil.email(ctx.user()).lower().strip()
    + if not email in authors:
    + c = namedtuple('Contributor', 'username email gravatar_id')
    + c.email = email
    + c.username = stringutil.person(ctx.user())
    + c.gravatar_id = hashlib.md5(email).hexdigest().encode('UTF-8')
    + authors[email] = c
    for ctx in cmdutil.walkchangerevs(repo, m, opts, prep):
    - # we handle everything in the callback closure so nothing to do here
    - continue
    + # we handle everything in the callback closure so nothing to do here
    + continue
    path = os.path.join(repo.root, _CACHE_DIR)
    @@ -53,11 +59,11 @@
    if err.errno != errno.EEXIST:
    raise
    - map_file = os.path.join(path, 'author_map')
    - with open(map_file, 'w') as ofp:
    - json.dump(authors, ofp, indent=2)
    +# map_file = os.path.join(path, 'author_map')
    +# with open(map_file, 'w') as ofp:
    +# json.dump(authors, ofp, indent=2)
    - ui.write('done.\n')
    + ui.write(b'done.\n')
    return authors
    @@ -84,36 +90,36 @@
    qs = urlencode(params)
    - for author, key in authors.items():
    - ui.write('downloading image for %s ... ' % author)
    + for _, author in authors.items():
    + ui.write(b'downloading image for %s ... ' % author.username)
    - filename = '%s.png' % key
    + filename = b'%s.png' % author.username
    - uri = os.path.join(url, filename)
    - if qs != '':
    - uri += '?' + qs
    + uri = os.path.join(url, b'%s.png' % author.gravatar_id)
    + if qs != b'':
    + uri += b'?' + qs
    - img = urlreq.urlopen(uri)
    + img = urlreq.urlopen(uri.decode('UTF-8'))
    if img.getcode() != 200:
    img.close()
    - ui.write('failed.\n')
    + ui.write(b'failed.\n')
    continue
    - with open(os.path.join(repo.root, _CACHE_DIR, filename), 'w') as ofp:
    + with open(os.path.join(repo.root, _CACHE_DIR, filename), 'wb') as ofp:
    ofp.write(img.read())
    img.close()
    - ui.write('done.\n')
    + ui.write(b'done.\n')
    def validate_arguments(**opts):
    rating = opts.get('rating')
    - valid = ['g', 'pg', 'r', 'x']
    + valid = [b'g', b'pg', b'r', b'x']
    if rating != '' and not rating in valid:
    raise error.Abort(_('invalid rating %s, accepted values are %s' % (rating, ', '.join(valid))))
    size = opts.get('size')
    - if size != '':
    + if size != b'':
    try:
    sz = int(size)
    if sz < 1 or sz > 2048:
    @@ -122,18 +128,18 @@
    raise error.Abort(_('invalid size %s, it is not an integer between 1 and 2048' % size))
    theme = opts.get('theme')
    - valid = ['404', 'mp', 'identicon', 'monsterid', 'wavatar', 'retro', 'robohash', 'blank']
    + valid = [b'404', b'mp', b'identicon', b'monsterid', b'wavatar', b'retro', b'robohash', b'blank']
    if theme != '' and not theme in valid:
    raise error.Abort(_('invalid theme %s, accepted values are %s' % (theme, ', '.join(valid))))
    @command(
    - 'gravatar',
    - [
    - ('r', 'rating', '', _('rating'), _('RATING')),
    - ('s', 'size', '', _('size'), _('SIZE')),
    - ('t', 'theme', '', _('theme'), _('THEME')),
    - ('u', 'url', 'https://www.gravatar.com/avatar', _('gravatar url'), _('URL')),
    + b'gravatar', [
    + (b'c', b'clean', b'', _('clean the cache'), None),
    + (b'r', b'rating', b'pg', _('minimum rating'), _('RATING')),
    + (b's', b'size', b'128', _('size of the images'), _('SIZE')),
    + (b't', b'theme', b'identicon', _('the theme to use'), _('THEME')),
    + (b'u', b'url', b'https://www.gravatar.com/avatar', _('the gravatar url'), _('URL')),
    ],
    _("hg gravatar [-u URL] [-o DIR]"),
    inferrepo=True,