#!/usr/bin/env python

import re

# match a simplified form of rule with
# at least one extension
rule = re.compile("""
    ([a-z0-9\/.+-]+)
    \s+
    ([a-z0-9.+-][a-z0-9. +-]*)
""", re.IGNORECASE | re.VERBOSE)

print("mimetype.assign = (")

mime_file = open("/etc/mime.types", "r")

# collapse lines ending with a slash
text = re.sub(r"\\(\n|(\r\n?))", "", mime_file.read())

types = set()
for line in map(str.strip, text.splitlines()):
    # rule matching will prevent comments or
    # empty lines to pass

    match = rule.match(line)
    if not match:
        continue
    
    mime, exts = match.groups()

    for ext in exts.split():
        if not ext in types:
            types.add(ext)
            print('".%s" => "%s",' % (ext, mime.lower()))

print(")")
