'Generate a string that is guaranteed to be unique among all
albums in the library who share the same set of keys. A fields
from "disam" is used in the string if one is sufficient to
disambiguate the albums. Otherwise, a fallback opaque value is
used. Both "keys" and "disam" should be given as
whitespace-separated lists of field names, while "bracket" is a
pair of characters to be used as brackets surrounding the
disambiguator or empty to have no brackets.'

def tmpl_aunique(self, keys=None, disam=None, bracket=None):

    if ((not self.item) or (not self.lib)):
        return u''
    if (self.item.album_id is None):
        return u''
    memokey = ('aunique', keys, disam, self.item.album_id)
    memoval = self.lib._memotable.get(memokey)
    if (memoval is not None):
        return memoval
    keys = (keys or 'albumartist    album')
    disam = (disam or 'albumtype    year    label    catalognum    albumdisambig')
    if (bracket is None):
        bracket = '[]'
    keys = keys.split()
    disam = disam.split()
    if (len(bracket) == 2):
        bracket_l = bracket[0]
        bracket_r = bracket[1]
    else:
        bracket_l = u''
        bracket_r = u''
    album = self.lib.get_album(self.item)
    if (not album):
        self.lib._memotable[memokey] = u''
        return u''
    subqueries = []
    for key in keys:
        value = album.get(key, '')
        subqueries.append(dbcore.MatchQuery(key, value))
    albums = self.lib.albums(dbcore.AndQuery(subqueries))
    if (len(albums) == 1):
        self.lib._memotable[memokey] = u''
        return u''
    for disambiguator in disam:
        disam_values = set([a.get(disambiguator, '') for a in albums])
        if (len(disam_values) == len(albums)):
            break
    else:
        res = u'    {1}{0}{2}'.format(album.id, bracket_l, bracket_r)
        self.lib._memotable[memokey] = res
        return res
    disam_value = album.formatted(True).get(disambiguator)
    if disam_value:
        res = u'    {1}{0}{2}'.format(disam_value, bracket_l, bracket_r)
    else:
        res = u''
    self.lib._memotable[memokey] = res
    return res
