Created
January 16, 2019 02:47
-
-
Save chenyahui/900ece1f2a73004be0edf8fdf7dd0b81 to your computer and use it in GitHub Desktop.
mime_type_gen.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
def load_from_url(): | |
with open('db.json') as f: | |
return json.load(f) | |
def mime_type_to_var(mime_type): | |
return mime_type.upper().replace("+", "_").replace("/","_").replace("-","_").replace(".","_") | |
def print_lines(file, lines): | |
for l in lines: | |
file.write(l) | |
file.write("\n") | |
def write_file(file, lines): | |
with open(file, 'w') as f: | |
for l in lines: | |
f.write(l) | |
f.write("\n") | |
def main(): | |
content_type_struct = [] | |
content_type_def = [] | |
ext_to_content_type_map = [] | |
mime_dict = load_from_url() | |
for mime_type in mime_dict: | |
var_mime_type = mime_type_to_var(mime_type) | |
content_type_struct.append('const static std::string {};'.format(var_mime_type, mime_type)) | |
content_type_def.append('const std::string ContentType::{} = "{}";'.format(var_mime_type, mime_type)) | |
if "extensions" in mime_dict[mime_type]: | |
for ext in mime_dict[mime_type]["extensions"]: | |
ext_to_content_type_map.append('{"%s", ContentType::%s},' % (ext,var_mime_type)) | |
write_file("content_type_struct.cpp", content_type_struct) | |
write_file("content_type_def.cpp", content_type_def) | |
write_file("ext_to_content_type_map.cpp", ext_to_content_type_map) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment