-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathconvert_mimetypes.py
More file actions
44 lines (33 loc) · 1.31 KB
/
convert_mimetypes.py
File metadata and controls
44 lines (33 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
import sys
import argparse
# Source mime.types: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
def main():
parser = argparse.ArgumentParser(description="Convert Apache's mime.types file to our MimeTypes.inc")
parser.add_argument('-s', '--source', required=True, type=str, help='Path to source mime.types file')
parser.add_argument('-o', '--output', required=True, type=str, help='Path to target MimeTypes.inc file to overwrite')
args = parser.parse_args()
if not os.path.isfile(args.source):
print("Source mime.types file must exist")
parser.print_help()
return 1
mapping = {}
with open(args.source, "r") as fh:
for line in fh.readlines():
if line.startswith("#"):
continue
line = line.strip()
parts = line.split()
mimetype = parts[0].strip()
exts = parts[1:]
if not exts:
print(f"No extensions for {mimetype}, skipping")
continue
for ext in exts:
mapping.update({ext: mimetype})
with open(args.output, "w") as fh:
for ext, mimetype in mapping.items():
fh.write(f'{{"{ext}", "{mimetype}"}},\n')
return 0
if __name__ == '__main__':
sys.exit(main())