sist2/scripts/mime.py
2019-10-25 20:25:01 -04:00

103 lines
2.6 KiB
Python

mimes = {}
noparse = set()
ext_in_hash = set()
major_mime = {
"model": 1,
"example": 2,
"message": 3,
"multipart": 4,
"font": 5,
"video": 6,
"audio": 7,
"image": 8,
"text": 9,
"application": 10
}
pdf = (
"application/pdf",
"application/x-cbr",
"application/x-cbz",
"application/vnd.ms-xpsdocument",
)
font = (
"application/vnd.ms-opentype",
"application/x-font-sfn",
"application/x-font-ttf",
"font/otf",
"font/sfnt",
"font/woff",
"font/woff2"
)
cnt = 1
def mime_id(mime):
global cnt
major = mime.split("/")[0]
mime_id = str((major_mime[major] << 16) + cnt)
cnt += 1
if mime in noparse:
mime_id += " | 0x80000000"
elif mime in pdf:
mime_id += " | 0x40000000"
elif mime in font:
mime_id += " | 0x20000000"
elif mime == "application/x-empty":
return "1"
return mime_id
def clean(t):
return t.replace("/", "_").replace(".", "_").replace("+", "_").replace("-", "_")
with open("mime.csv") as f:
for l in f:
mime, ext_list = l.split(",")
if l.startswith("!"):
mime = mime[1:]
noparse.add(mime)
ext = [x.strip() for x in ext_list.split("|")]
mimes[mime] = ext
print("// **Generated by mime.py**")
print("#ifndef MIME_GENERATED_C")
print("#define MIME_GENERATED_C")
print("#include <glib-2.0/glib.h>\n")
print("#include <stdlib.h>\n")
# Enum
print("enum mime {")
for mime, ext in mimes.items():
print(" " + clean(mime) + "=" + mime_id(mime) + ",")
print("};")
# Enum -> string
print("char *mime_get_mime_text(unsigned int mime_id) {"
"switch (mime_id) {")
for mime, ext in mimes.items():
print("case " + clean(mime) + ": return \"" + mime + "\";")
print("default: return NULL;}}")
# Ext -> Enum
print("GHashTable *mime_get_ext_table() {"
"GHashTable *ext_table = g_hash_table_new(g_str_hash, g_str_equal);")
for mime, ext in mimes.items():
for e in [e for e in ext if e]:
print("g_hash_table_insert(ext_table, \"" + e + "\", (gpointer)" + clean(mime) + ");")
if e in ext_in_hash:
raise Exception("extension already in hash: " + e)
ext_in_hash.add(e)
print("return ext_table;}")
# string -> Enum
print("GHashTable *mime_get_mime_table() {"
"GHashTable *mime_table = g_hash_table_new(g_str_hash, g_str_equal);")
for mime, ext in mimes.items():
print("g_hash_table_insert(mime_table, \"" + mime + "\", (gpointer)" + clean(mime) + ");")
print("return mime_table;}")
print("#endif")