content-types 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,158 @@
1
+ import sys
2
+ from pathlib import Path
3
+ from typing import Dict
4
+
5
+ __VERSION__ = '0.2.0'
6
+
7
+ # This dictionary maps file extensions (no dot) to the most specific content type.
8
+ EXTENSION_TO_CONTENT_TYPE: Dict[str, str] = {
9
+ # Text
10
+ 'txt': 'text/plain',
11
+ 'htm': 'text/html',
12
+ 'html': 'text/html',
13
+ 'css': 'text/css',
14
+ 'csv': 'text/csv',
15
+ 'tsv': 'text/tab-separated-values',
16
+ # JavaScript
17
+ 'js': 'application/javascript', # commonly "application/javascript" nowadays
18
+ # JSON
19
+ 'json': 'application/json',
20
+ 'map': 'application/json', # e.g., SourceMap
21
+ # XML
22
+ 'xml': 'application/xml', # can also be "text/xml" in some contexts
23
+ # Images
24
+ 'jpg': 'image/jpeg',
25
+ 'jpeg': 'image/jpeg',
26
+ 'png': 'image/png',
27
+ 'gif': 'image/gif',
28
+ 'bmp': 'image/bmp',
29
+ 'webp': 'image/webp',
30
+ 'avif': 'image/avif',
31
+ 'ico': 'image/x-icon', # sometimes "image/vnd.microsoft.icon"
32
+ 'svg': 'image/svg+xml',
33
+ 'tif': 'image/tiff',
34
+ 'tiff': 'image/tiff',
35
+ # Audio
36
+ 'mp3': 'audio/mpeg',
37
+ 'ogg': 'audio/ogg',
38
+ 'wav': 'audio/wav',
39
+ 'aac': 'audio/aac',
40
+ 'flac': 'audio/flac',
41
+ 'm4a': 'audio/mp4',
42
+ 'weba': 'audio/webm',
43
+ # Video
44
+ 'mp4': 'video/mp4',
45
+ 'm4v': 'video/mp4', # often container-based
46
+ 'mov': 'video/quicktime',
47
+ 'avi': 'video/x-msvideo',
48
+ 'wmv': 'video/x-ms-wmv',
49
+ 'mpg': 'video/mpeg',
50
+ 'mpeg': 'video/mpeg',
51
+ 'ogv': 'video/ogg',
52
+ 'webm': 'video/webm',
53
+ # Application / Archive
54
+ 'pdf': 'application/pdf',
55
+ 'zip': 'application/zip',
56
+ 'gz': 'application/gzip',
57
+ 'tgz': 'application/gzip', # or "application/x-tar" + "gzip"
58
+ 'tar': 'application/x-tar',
59
+ '7z': 'application/x-7z-compressed',
60
+ 'rar': 'application/vnd.rar',
61
+ # Office
62
+ 'doc': 'application/msword',
63
+ 'xls': 'application/vnd.ms-excel',
64
+ 'ppt': 'application/vnd.ms-powerpoint',
65
+ 'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
66
+ 'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
67
+ 'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
68
+ # OpenDocument
69
+ 'odt': 'application/vnd.oasis.opendocument.text',
70
+ 'ods': 'application/vnd.oasis.opendocument.spreadsheet',
71
+ 'odp': 'application/vnd.oasis.opendocument.presentation',
72
+ 'odg': 'application/vnd.oasis.opendocument.graphics',
73
+ # Fonts
74
+ 'otf': 'font/otf',
75
+ 'ttf': 'font/ttf',
76
+ 'woff': 'font/woff',
77
+ 'woff2': 'font/woff2',
78
+ # 3D model
79
+ 'gltf': 'model/gltf+json',
80
+ 'glb': 'model/gltf-binary',
81
+ 'stl': 'model/stl',
82
+ 'obj': 'model/obj', # not officially registered; widely used
83
+ # Scripts / misc
84
+ 'sh': 'application/x-sh',
85
+ 'php': 'application/x-httpd-php', # Usually not used at runtime for real responses
86
+ 'exe': 'application/x-msdownload',
87
+ # Misc
88
+ 'apk': 'application/vnd.android.package-archive',
89
+ 'deb': 'application/x-debian-package',
90
+ 'rpm': 'application/x-rpm',
91
+ }
92
+
93
+
94
+ def get_content_type(filename_or_extension: str | Path, treat_as_binary: bool = True) -> str:
95
+ """
96
+ Given a filename (or just an extension), return the most specific,
97
+ commonly accepted MIME type based on extension.
98
+
99
+ Falls back to 'application/octet-stream' if not found.
100
+
101
+ Example:
102
+ >>> get_content_type("picture.jpg")
103
+ 'image/jpeg'
104
+ >>> get_content_type(".webp")
105
+ 'image/webp'
106
+ >>> get_content_type("script.js")
107
+ 'application/javascript'
108
+ >>> get_content_type("unknown.xyz")
109
+ 'application/octet-stream'
110
+ >>> get_content_type("unknown.xyz", treat_as_binary=False)
111
+ 'application/octet-stream'
112
+ """
113
+
114
+ if filename_or_extension is None:
115
+ raise Exception('filename cannot be None.')
116
+
117
+ if isinstance(filename_or_extension, Path):
118
+ filename_or_extension = filename_or_extension.suffix
119
+
120
+ if '.' not in filename_or_extension:
121
+ filename_or_extension = f'.{filename_or_extension}'
122
+
123
+ # Split by dot, take the last part as extension
124
+ # e.g., "archive.tar.gz" => "gz"
125
+ # Also handle cases like ".webp" => "webp"
126
+ dot_parts = filename_or_extension.lower().split('.')
127
+ ext = dot_parts[-1] if len(dot_parts) > 1 else ''
128
+
129
+ if treat_as_binary:
130
+ return EXTENSION_TO_CONTENT_TYPE.get(ext, 'application/octet-stream')
131
+
132
+ return EXTENSION_TO_CONTENT_TYPE.get(ext, 'text/plain')
133
+
134
+
135
+ webp: str = get_content_type('.webp')
136
+ png: str = get_content_type('.png')
137
+ jpg: str = get_content_type('.jpg')
138
+ mp3: str = get_content_type('.mp3')
139
+ json: str = get_content_type('.json')
140
+ pdf: str = get_content_type('.pdf')
141
+ zip: str = get_content_type('.zip') # noqa == it's fine to overwrite zip() in this module only.
142
+ xml: str = get_content_type('.xml')
143
+ csv: str = get_content_type('.csv')
144
+
145
+
146
+ def cli():
147
+ """
148
+ A simple CLI to look up the MIME type for a given filename or extension.
149
+ Usage example:
150
+ contenttypes my_file.jpg
151
+ """
152
+ if len(sys.argv) < 2:
153
+ print('Usage: contenttypes [FILENAME_OR_EXTENSION]\nExample: contenttypes .jpg')
154
+ sys.exit(1)
155
+
156
+ filename = sys.argv[1]
157
+ mime_type = get_content_type(filename)
158
+ print(mime_type)
@@ -0,0 +1,61 @@
1
+ Metadata-Version: 2.4
2
+ Name: content-types
3
+ Version: 0.2.0
4
+ Summary: A library to map file extensions to content types and vice versa.
5
+ Author-email: Michael Kennedy <mikeckennedy@gmail.com>
6
+ License-Expression: MIT
7
+ License-File: LICENSE
8
+ Keywords: content-type,file extensions,mapping,mime
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Programming Language :: Python :: 3.14
17
+ Classifier: Topic :: Internet :: WWW/HTTP
18
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
+ Requires-Python: <=3.14,>=3.10
20
+ Description-Content-Type: text/markdown
21
+
22
+
23
+ # content-types 🗃️🔎
24
+
25
+ A Python library to map file extensions to MIME types.
26
+ It also provides a CLI for quick lookups right from your terminal.
27
+ If no known mapping is found, the tool returns `application/octet-stream`.
28
+
29
+ ## Installation
30
+
31
+ ```bash
32
+ uv pip install content-types
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ ```python
38
+ import content_types
39
+
40
+ # Forward lookup: filename -> MIME type
41
+ mime_type = content_types.get_content_type("example.jpg")
42
+ print(mime_type) # "image/jpeg"
43
+
44
+ # For very common files, you have shortcuts:
45
+ print(f'Content-Type for webp is {content_types.webp}') # 'image/webp'
46
+ ```
47
+
48
+ ## CLI
49
+
50
+ After installing in a virtual environment or system-wide.
51
+
52
+ ```bash
53
+ contenttypes example.jpg
54
+
55
+ # Outputs image/jpeg
56
+ ```
57
+
58
+ ## Contributing
59
+
60
+ Contributions are welcome! Check out [the GitHub repo](https://github.com/mikeckennedy/content-types)
61
+ for more details on how to get involved.
@@ -0,0 +1,6 @@
1
+ content_types/__init__.py,sha256=sTJ7PuW3cye17QlaG5VahqT53lRDGagZelEto3NCleM,5162
2
+ content_types-0.2.0.dist-info/METADATA,sha256=vkEI1TI3SVta6sfqcJMj7GmW0uRG667ejhzg_KtAub8,1740
3
+ content_types-0.2.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
4
+ content_types-0.2.0.dist-info/entry_points.txt,sha256=wtAV2fpmMEOZYMpxrnCkV2KzbVGNpvHbAw-STWGKwHQ,52
5
+ content_types-0.2.0.dist-info/licenses/LICENSE,sha256=qpKwczyRa1AguxfvezgALGP411SLV1FMa7ECQAvUGFQ,1072
6
+ content_types-0.2.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.27.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ content-types = content_types:cli
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Michael Kennedy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.