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

1"""Mime types.""" 

2 

3from typing import Optional 

4 

5import mimetypes 

6 

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' 

31 

32 

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} 

58 

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, 

77 

78 'doc': DOC, 

79 'xls': XLS, 

80 'ppt': PPT, 

81} 

82 

83_aliases = { 

84 

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, 

90 

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, 

97 

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} 

102 

103 

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

105 """Return the normalized mime type. 

106 

107 Args: 

108 mt: Mime type or content type. 

109 

110 Returns: 

111 The normalized mime type. 

112 """ 

113 

114 if not mt: 

115 return None 

116 

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

118 

119 s = _get_quick(mt) 

120 if s: 

121 return s 

122 

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

124 if mt.startswith(s): 

125 return m 

126 

127 if ';' in mt: 

128 p = mt.partition(';') 

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

130 if s: 

131 return s 

132 

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

134 return mt 

135 

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

137 return t 

138 

139 

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] 

147 

148 

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

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

151 

152 Args: 

153 path: Path to mime type. 

154 

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 

163 

164 

165def extension_for(mt: str) -> Optional[str]: 

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

167 

168 Args: 

169 mt: Mime type. 

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