import re


def read_keywords(keywords_file):
    """
    Parses a Processing keywords.txt file, returning a dictionary of
    all keywords in the file, indexed by keyword type.

    Arguments:
    keywords_file -- full path to a Processing keywords.txt file

    """
    keywords = {}

    for line in open(keywords_file):

        # Skip blank lines and comments
        if re.match('^s*$', line):
            continue
        if re.match('^\s*#', line):
            continue

        match = re.match('^(\w+)\s*\t(\w+)\t?(\w*)', line)
        if match:
            (kw_name, kw_type, kw_loc) = match.groups()
            if kw_type in keywords:
                keywords[kw_type].add(kw_name)
            else:
                keywords[kw_type] = set([kw_name])

    return keywords