Project

General

Profile

Feature #2942 » create-mime.assign.py

Alternative implementation of create-mime.assign.pl in python - geusebi, 2019-03-14 02:15

 
1
#!/usr/bin/env python
2

    
3
import re
4

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

    
13
print("mimetype.assign = (")
14

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

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

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

    
25
    match = rule.match(line)
26
    if not match:
27
        continue
28
    
29
    mime, exts = match.groups()
30

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

    
36
print(")")
(1-1/2)