grim/hggravatar

13a2db7b58db
Parents 7d4789338ec6
Children bfae328cdd61
add support for setting themes, sizes, and ratings
  • +51 -0
    hggravatar.py
  • --- a/hggravatar.py Thu Dec 20 00:03:27 2018 -0600
    +++ b/hggravatar.py Thu Dec 20 01:21:31 2018 -0600
    @@ -9,6 +9,7 @@
    from mercurial.i18n import _
    from mercurial import (
    cmdutil,
    + error,
    registrar,
    scmutil,
    util,
    @@ -16,6 +17,7 @@
    from mercurial.utils import stringutil
    urlreq = util.urlreq
    +urlencode = urlreq.urlencode
    cmdtable = {}
    command = registrar.command(cmdtable)
    @@ -61,14 +63,37 @@
    url = opts.get('url')
    + params = {}
    +
    + rating = opts.get('rating')
    + if rating != '':
    + params['r'] = rating
    +
    + size = opts.get('size')
    + if size != '':
    + params['s'] = size
    +
    + theme = opts.get('theme')
    + if theme != '':
    + params['d'] = theme
    +
    + qs = urlencode(params)
    +
    for author, key in authors.items():
    ui.write('downloading image for %s ... ' % author)
    filename = '%s.png' % key
    uri = os.path.join(url, filename)
    + if qs != '':
    + uri += '?' + qs
    img = urlreq.urlopen(uri)
    + if img.getcode() != 200:
    + img.close()
    + ui.write('failed.\n')
    + continue
    +
    with open(os.path.join(opts.get('output'), filename), 'w') as ofp:
    ofp.write(img.read())
    img.close()
    @@ -76,16 +101,42 @@
    ui.write('done.\n')
    +def validate_arguments(**opts):
    + rating = opts.get('rating')
    + valid = ['g', 'pg', 'r', '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 != '':
    + try:
    + sz = int(size)
    + if sz < 1 or sz > 2048:
    + raise error.Abort(_('invalid size %d, it much be between 1 and 2048' % sz))
    + except ValueError:
    + 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']
    + if theme != '' and not theme in valid:
    + raise error.Abort(_('invalid theme %s, accepted values are %s' % (theme, ', '.join(valid))))
    +
    +
    @command(
    'gravatar',
    [
    ('o', 'output', '.hg/cache/gravatar/', _('output directory'), _('DIR')),
    + ('r', 'rating', '', _('rating'), _('RATING')),
    + ('s', 'size', '', _('size'), _('SIZE')),
    + ('t', 'theme', '', _('theme'), _('THEME')),
    ('u', 'url', 'https://www.gravatar.com/avatar', _('gravatar url'), _('URL')),
    ],
    _("hg gravatar [-u URL] [-o DIR]"),
    inferrepo=True,
    )
    def gravatar(ui, repo, *pats, **opts):
    + validate_arguments(**opts)
    +
    authors = find_authors(ui, repo, *pats, **opts)
    save_avatars(ui, repo, authors, *pats, **opts)