Coverage for gws-app/gws/lib/mime/__init__.py: 49%
68 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
1"""Mime types."""
3from typing import Optional
5import mimetypes
7BIN = 'application/octet-stream'
8CSS = 'text/css'
9CSV = 'text/csv'
10GEOJSON = 'application/geo+json'
11GIF = 'image/gif'
12GML = 'application/gml+xml'
13GML2 = 'application/gml+xml;version=2'
14GML3 = 'application/gml+xml;version=3'
15GZIP = 'application/gzip'
16HTML = 'text/html'
17JPEG = 'image/jpeg'
18JS = 'application/javascript'
19JSON = 'application/json'
20PDF = 'application/pdf'
21PNG = 'image/png'
22SVG = 'image/svg+xml'
23TTF = 'application/x-font-ttf'
24TXT = 'text/plain'
25XML = 'application/xml'
26ZIP = 'application/zip'
27DOC = 'application/msword'
28XLS = 'application/vnd.ms-excel'
29PPT = 'application/vnd.ms-powerpoint'
30WEBP = 'image/webp'
33_common = {
34 BIN,
35 CSS,
36 CSV,
37 GEOJSON,
38 GIF,
39 GML,
40 GML2,
41 GML3,
42 GZIP,
43 HTML,
44 JPEG,
45 JS,
46 JSON,
47 PDF,
48 PNG,
49 SVG,
50 TTF,
51 TXT,
52 XML,
53 ZIP,
54 DOC,
55 XLS,
56 PPT,
57}
59_common_extensions = {
60 'css': CSS,
61 'csv': CSV,
62 'gif': GIF,
63 'gml': GML,
64 'gml3': GML3,
65 'html': HTML,
66 'jpeg': JPEG,
67 'jpg': JPEG,
68 'js': JS,
69 'json': JSON,
70 'pdf': PDF,
71 'png': PNG,
72 'svg': SVG,
73 'ttf': TTF,
74 'txt': TXT,
75 'xml': XML,
76 'zip': ZIP,
78 'doc': DOC,
79 'xls': XLS,
80 'ppt': PPT,
81}
83_aliases = {
85 'application/vnd.ogc.gml': GML,
86 'application/vnd.ogc.gml/3.1.1': GML3,
87 'application/gml:3': GML3,
88 'application/xml;subtype=gml/2': GML2,
89 'application/xml;subtype=gml/3': GML3,
91 'application/html': HTML,
92 'application/x-gzip': GZIP,
93 'application/x-pdf': PDF,
94 'image/jpg': JPEG,
95 'text/xhtml': HTML,
96 'text/xml': XML,
98 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': DOC,
99 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': XLS,
100 'application/vnd.openxmlformats-officedocument.presentationml.presentation': PPT,
101}
104def get(mt: str) -> Optional[str]:
105 """Return the normalized mime type.
107 Args:
108 mt: Mime type or content type.
110 Returns:
111 The normalized mime type.
112 """
114 if not mt:
115 return None
117 mt = mt.strip().replace(' ', '').lower()
119 s = _get_quick(mt)
120 if s:
121 return s
123 for s, m in _aliases.items():
124 if mt.startswith(s):
125 return m
127 if ';' in mt:
128 p = mt.partition(';')
129 s = _get_quick(p[0].strip())
130 if s:
131 return s
133 if '/' in mt and mimetypes.guess_extension(mt):
134 return mt
136 t, _ = mimetypes.guess_type('x.' + mt)
137 return t
140def _get_quick(mt):
141 if mt in _common:
142 return mt
143 if mt in _common_extensions:
144 return _common_extensions[mt]
145 if mt in _aliases:
146 return _aliases[mt]
149def for_path(path: str) -> str:
150 """Returns the mime type for a given path.
152 Args:
153 path: Path to mime type.
155 Returns:
156 The mime type or ``BIN`` if type is unknown.
157 """
158 _, _, e = path.rpartition('.')
159 if e in _common_extensions:
160 return _common_extensions[e]
161 t, _ = mimetypes.guess_type(path)
162 return t or BIN
165def extension_for(mt: str) -> Optional[str]:
166 """Returns the extension of a given mime type.
168 Args:
169 mt: Mime type.
171 Returns:
172 The mime type extension.
173 """
175 for ext, rt in _common_extensions.items():
176 if rt == mt:
177 return ext
178 s = mimetypes.guess_extension(mt)
179 if s:
180 return s[1:]