Coverage for sources / detextive / mimetypes.py: 100%
12 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-17 06:15 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-17 06:15 +0000
1# vim: set filetype=python fileencoding=utf-8:
2# -*- coding: utf-8 -*-
4#============================================================================#
5# #
6# Licensed under the Apache License, Version 2.0 (the "License"); #
7# you may not use this file except in compliance with the License. #
8# You may obtain a copy of the License at #
9# #
10# http://www.apache.org/licenses/LICENSE-2.0 #
11# #
12# Unless required by applicable law or agreed to in writing, software #
13# distributed under the License is distributed on an "AS IS" BASIS, #
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15# See the License for the specific language governing permissions and #
16# limitations under the License. #
17# #
18#============================================================================#
21''' Determination of MIME types and textuality thereof. '''
24from . import __
25from . import nomina as _nomina
28TEXTUAL_MIMETYPE_SUFFIXES = ( '+json', '+toml', '+xml', '+yaml' )
29TEXTUAL_MIMETYPES = frozenset( (
30 'application/ecmascript',
31 'application/graphql',
32 'application/javascript',
33 'application/json',
34 'application/ld+json',
35 'application/x-httpd-php',
36 'application/x-javascript',
37 'application/x-latex',
38 'application/x-perl',
39 'application/x-php',
40 'application/x-python',
41 'application/x-ruby',
42 'application/x-shell',
43 'application/x-tex',
44 'application/x-yaml',
45 'application/xhtml+xml',
46 'application/xml',
47 'application/yaml',
48 'image/svg+xml',
49) )
52def is_textual_mimetype( mimetype: str ) -> bool:
53 ''' Checks if MIME type represents textual content. '''
54 if mimetype.startswith( ( 'text/', 'text/x-' ) ): return True
55 if mimetype in TEXTUAL_MIMETYPES: return True
56 return mimetype.endswith( TEXTUAL_MIMETYPE_SUFFIXES )
59def mimetype_from_location(
60 location: _nomina.Location
61) -> __.Absential[ str ]:
62 ''' Determines MIME type from file location. '''
63 # TODO: Python 3.13: Use __.mimetypes.guess_file_type for fs paths.
64 mimetype, _ = __.mimetypes.guess_type( location )
65 if mimetype: return mimetype
66 return __.absent