def fetch2count(data, hash_func):
    """Nested count of the hash values returned from all the elements in data

    Args:
        data: data from a fetch()
        hash_func: Hashing function
            One argument: classad dictionary
            Returns: hash value
                if None, will not be counted
                if a list, all elements will be used

    Returns:
        dict: dictionary of hash values
            Elements are counts (or more dictionaries if hash returns lists)

    """
    count = {}
    for k in list(data.keys()):
        el = data[k]

        hid = hash_func(el)
        if hid is None:
            # hash tells us it does not want to count this
            continue

        # cel will point to the real counter
        cel = count

        # check if it is a list
        if isinstance(hid, list):
            # have to create structure inside count
            for h in hid[:-1]:
                if h not in cel:
                    cel[h] = {}
                cel = cel[h]
            hid = hid[-1]

        if hid in cel:
            count_el = cel[hid] + 1
        else:
            count_el = 1
        cel[hid] = count_el

    return count