Estimated reading time: 3 minutes

Populating sup contacts from abook

A colleague from work, Adam Robertson, is switching to Sup from mutt and wondering how to easily convert his contacts from abook. Given that it is my constant pimping of sup that has convinced him to switch I feel obliged to help with the conversion.

The first choice I may recommend is just to follow the wiki and use abook as a source for lbdb. I use a method similar to this quite successfully.

The second choice is to prime the contact list from abook with a little Python script. The sup contacts list uses a very simple format and mangling the abook addressbook is possible in only a few lines of code:

#! /usr/bin/python3 -tt
"""Generate a sup contacts list from abook"""

from os import path
from sys import argv, exit

from configobj import ConfigObj

def parse(fname=None):
    if not fname:
        fname = path.expanduser('~/.abook/addressbook')
    conf = ConfigObj(fname, list_values=False)
    for chunk in filter(lambda d: 'nick' in d and 'email' in d, conf.values()):
        print('{0[nick]}: {0[name]}'.format(chunk),
              '<{}>'.format(chunk['email'].split(',')[0]))

if __name__ == '__main__':
    if len(argv) > 1:
        if argv[1] in ('-h', '--help'):
            print('{} [addressbook]'.format(argv[0]))
            exit(255)
        addressbook = argv[1]
    else:
        addressbook = None
    parse(addressbook)

The script requires the excellent configobj module. You could also use the configparser module from the Python standard library, but configobj allows my laziness to shine through with its simple dictionary-based access to the parsed data.

The script opens either the named or default abook addressbook and fetches any entries that contain both a nick and email section. Those entries are then output in format that sup accepts. It includes only the primary email address for the contact, as I tend to order contacts with multiple email addresses in address preference order.

As I’ve mentioned before in Making a nice home all these tasks should be automated, and this one is no different. To regenerate the contacts list when the addressbook has been updated we can use make:

.sup/contacts.txt: .abook/addressbook
    python3 sup_contacts.py $< >$@

Using this method allows us to continue using abook while having simple access to our contacts from within sup. This is incredibly useful as it means we can continue to use abook for other things too, see “Kick me” birthday reminders.


Authenticate this page by pasting this signature into Keybase.

Have a suggestion or see a typo? Edit this page