Coverage for gws-app/gws/lib/mime/__init__.py: 96%

70 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-24 12:46 +0200

1"""Mime types.""" 

2 

3from typing import Optional 

4 

5import mimetypes 

6 

7BIN = 'application/octet-stream' 

8CSS = 'text/css' 

9CSV = 'text/csv' 

10DOC = 'application/msword' 

11GEOJSON = 'application/geo+json' 

12GIF = 'image/gif' 

13GML = 'application/gml+xml' 

14GML2 = 'application/gml+xml;version=2' 

15GML3 = 'application/gml+xml;version=3' 

16GZIP = 'application/gzip' 

17HTML = 'text/html' 

18JPEG = 'image/jpeg' 

19JS = 'application/javascript' 

20JSON = 'application/json' 

21KML = 'application/vnd.google-earth.kml+xml' 

22PDF = 'application/pdf' 

23PNG = 'image/png' 

24PPT = 'application/vnd.ms-powerpoint' 

25SVG = 'image/svg+xml' 

26TTF = 'application/x-font-ttf' 

27TXT = 'text/plain' 

28WEBP = 'image/webp' 

29XLS = 'application/vnd.ms-excel' 

30XML = 'text/xml' 

31ZIP = 'application/zip' 

32 

33 

34_common = { 

35 BIN, 

36 CSS, 

37 CSV, 

38 DOC, 

39 GEOJSON, 

40 GIF, 

41 GML, 

42 GML2, 

43 GML3, 

44 GZIP, 

45 HTML, 

46 JPEG, 

47 JS, 

48 JSON, 

49 KML, 

50 PDF, 

51 PNG, 

52 PPT, 

53 SVG, 

54 TTF, 

55 TXT, 

56 XLS, 

57 XML, 

58 ZIP, 

59} 

60 

61_common_extensions = { 

62 'css': CSS, 

63 'csv': CSV, 

64 'doc': DOC, 

65 'gif': GIF, 

66 'gml': GML, 

67 'gml2': GML2, 

68 'html': HTML, 

69 'jpeg': JPEG, 

70 'jpg': JPEG, 

71 'js': JS, 

72 'json': JSON, 

73 'kml': KML, 

74 'pdf': PDF, 

75 'png': PNG, 

76 'ppt': PPT, 

77 'svg': SVG, 

78 'ttf': TTF, 

79 'txt': TXT, 

80 'xls': XLS, 

81 'xml': XML, 

82 'zip': ZIP, 

83} 

84 

85_aliases = { 

86 'application/vnd.ogc.gml': GML2, 

87 'application/vnd.ogc.gml/3.1.1': GML, 

88 'application/gml:3': GML, 

89 'application/xml;subtype=gml/2': GML2, 

90 'application/xml;subtype=gml/3': GML, 

91 'application/html': HTML, 

92 'application/x-gzip': GZIP, 

93 'application/x-pdf': PDF, 

94 'image/jpg': JPEG, 

95 'text/xhtml': HTML, 

96 'application/xml': XML, 

97 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': DOC, 

98 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': XLS, 

99 'application/vnd.openxmlformats-officedocument.presentationml.presentation': PPT, 

100} 

101 

102 

103def get(mt: str) -> Optional[str]: 

104 """Return the normalized mime type. 

105 

106 Args: 

107 mt: Mime type or content type. 

108 

109 Returns: 

110 The normalized mime type. 

111 """ 

112 

113 if not mt: 

114 return None 

115 

116 mt = mt.strip().replace(' ', '').lower() 

117 

118 s = _get_quick(mt) 

119 if s: 

120 return s 

121 

122 for s, m in _aliases.items(): 

123 if mt.startswith(s): 

124 return m 

125 

126 if ';' in mt: 

127 p = mt.partition(';') 

128 s = _get_quick(p[0].strip()) 

129 if s: 

130 return s 

131 

132 if '/' in mt and mimetypes.guess_extension(mt): 

133 return mt 

134 

135 t, _ = mimetypes.guess_type('x.' + mt) 

136 return t 

137 

138 

139def _get_quick(mt): 

140 if mt in _common: 

141 return mt 

142 if mt in _common_extensions: 

143 return _common_extensions[mt] 

144 if mt in _aliases: 

145 return _aliases[mt] 

146 

147 

148def for_path(path: str) -> str: 

149 """Returns the mime type for a given path. 

150 

151 Args: 

152 path: Path to mime type. 

153 

154 Returns: 

155 The mime type or ``BIN`` if type is unknown. 

156 """ 

157 _, _, e = path.rpartition('.') 

158 if e in _common_extensions: 

159 return _common_extensions[e] 

160 t, _ = mimetypes.guess_type(path) 

161 return t or BIN 

162 

163 

164def extension_for(mt: str, default: str = 'bin') -> str: 

165 """Returns the extension of a given mime type. 

166 

167 Args: 

168 mt: Mime type. 

169 default: Default extension to return if mime type is unknown. 

170 

171 Returns: 

172 The mime type extension. 

173 """ 

174 

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:] 

181 return default