2020-10-31 18:22:34 +00:00
|
|
|
#!/usr/bin/env python3
|
2020-10-31 17:04:49 +00:00
|
|
|
|
2020-10-07 09:17:37 +00:00
|
|
|
#get mime.types file from the nginx repository at nginx/conf/mime.types
|
2020-10-12 22:01:47 +00:00
|
|
|
#typical output filename: mime_types.h
|
|
|
|
import sys
|
2021-12-03 03:41:30 +00:00
|
|
|
from datetime import date
|
2020-10-12 22:01:47 +00:00
|
|
|
|
2021-12-03 03:41:30 +00:00
|
|
|
if (len(sys.argv) != 3) and (len(sys.argv) != 2):
|
|
|
|
print("Usage (local file): {} <NGINX_MIME_TYPE_FILE_PATH> <CROW_OUTPUT_HEADER_PATH>".format(sys.argv[0]))
|
|
|
|
print("(downloads file) : {} <CROW_OUTPUT_HEADER_PATH>".format(sys.argv[0]))
|
2020-10-12 22:01:47 +00:00
|
|
|
sys.exit(1)
|
2021-12-03 03:41:30 +00:00
|
|
|
if len(sys.argv) == 3:
|
|
|
|
file_path = sys.argv[1]
|
|
|
|
output_path = sys.argv[2]
|
|
|
|
elif len(sys.argv) == 2:
|
|
|
|
import requests
|
|
|
|
open("mime.types", "wb").write(requests.get("https://hg.nginx.org/nginx/raw-file/tip/conf/mime.types").content)
|
|
|
|
file_path = "mime.types"
|
|
|
|
output_path = sys.argv[1]
|
2020-10-12 22:01:47 +00:00
|
|
|
|
2020-10-07 09:17:37 +00:00
|
|
|
tabspace = " "
|
2021-12-03 03:41:30 +00:00
|
|
|
tabandahalfspace = " "
|
2020-10-04 16:09:30 +00:00
|
|
|
def main():
|
2020-10-07 09:17:37 +00:00
|
|
|
outLines = []
|
2021-12-03 03:41:30 +00:00
|
|
|
outLines.append("// This file is generated from nginx/conf/mime.types using nginx_mime2cpp.py on " + date.today().strftime('%Y-%m-%d') + ".")
|
2020-10-07 09:17:37 +00:00
|
|
|
outLines.extend([
|
|
|
|
"#include <unordered_map>",
|
|
|
|
"#include <string>",
|
|
|
|
"",
|
2021-12-03 03:41:30 +00:00
|
|
|
"namespace crow",
|
|
|
|
"{",
|
|
|
|
tabspace + "const std::unordered_map<std::string, std::string> mime_types{"])
|
2020-10-07 09:17:37 +00:00
|
|
|
|
2020-10-12 22:01:47 +00:00
|
|
|
with open(file_path, "r") as mtfile:
|
2020-10-07 09:17:37 +00:00
|
|
|
incomplete = ""
|
|
|
|
incompleteExists = False
|
|
|
|
for line in mtfile:
|
|
|
|
if ('{' not in line and '}' not in line):
|
|
|
|
splitLine = line.split()
|
|
|
|
if (';' not in line):
|
|
|
|
incomplete += line
|
|
|
|
incompleteExists = True
|
|
|
|
continue
|
|
|
|
elif (incompleteExists):
|
|
|
|
splitLine = incomplete.split()
|
|
|
|
splitLine.extend(line.split())
|
|
|
|
incomplete = ""
|
|
|
|
incompleteExists = False
|
|
|
|
outLines.extend(mime_line_to_cpp(splitLine))
|
|
|
|
|
2021-12-03 03:41:30 +00:00
|
|
|
outLines[-1] = outLines[-1][:-1] + "};"
|
|
|
|
outLines.append("}")
|
2020-10-07 09:17:37 +00:00
|
|
|
|
2020-10-12 22:01:47 +00:00
|
|
|
with open(output_path, "w") as mtcppfile:
|
2020-10-07 09:17:37 +00:00
|
|
|
mtcppfile.writelines(x + '\n' for x in outLines)
|
2020-10-04 16:09:30 +00:00
|
|
|
|
|
|
|
def mime_line_to_cpp(mlist):
|
2020-10-07 09:17:37 +00:00
|
|
|
#print("recieved: " + str(mlist))
|
|
|
|
stringReturn = []
|
2020-10-04 16:09:30 +00:00
|
|
|
for i in range (len(mlist)):
|
|
|
|
if (mlist[i].endswith(";")):
|
|
|
|
mlist[i] = mlist[i][:-1]
|
|
|
|
for i in range (len(mlist)-1, 0, -1):
|
2021-12-03 03:41:30 +00:00
|
|
|
stringReturn.append(tabandahalfspace + "{\"" + mlist[i] + "\", \"" + mlist[0] + "\"},")
|
2020-10-07 09:17:37 +00:00
|
|
|
#print("created: " + stringReturn)
|
2020-10-04 16:09:30 +00:00
|
|
|
return stringReturn
|
|
|
|
|
|
|
|
main()
|