content-types 0.2.0__tar.gz

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,172 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+
110
+ # pdm
111
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
112
+ #pdm.lock
113
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
114
+ # in version control.
115
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
116
+ .pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
121
+ __pypackages__/
122
+
123
+ # Celery stuff
124
+ celerybeat-schedule
125
+ celerybeat.pid
126
+
127
+ # SageMath parsed files
128
+ *.sage.py
129
+
130
+ # Environments
131
+ .env
132
+ .venv
133
+ env/
134
+ venv/
135
+ ENV/
136
+ env.bak/
137
+ venv.bak/
138
+
139
+ # Spyder project settings
140
+ .spyderproject
141
+ .spyproject
142
+
143
+ # Rope project settings
144
+ .ropeproject
145
+
146
+ # mkdocs documentation
147
+ /site
148
+
149
+ # mypy
150
+ .mypy_cache/
151
+ .dmypy.json
152
+ dmypy.json
153
+
154
+ # Pyre type checker
155
+ .pyre/
156
+
157
+ # pytype static type analyzer
158
+ .pytype/
159
+
160
+ # Cython debug symbols
161
+ cython_debug/
162
+
163
+ # PyCharm
164
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
165
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
166
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
167
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
168
+ #.idea/
169
+
170
+ # PyPI configuration file
171
+ .pypirc
172
+ /.idea/
@@ -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.
@@ -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,40 @@
1
+
2
+ # content-types 🗃️🔎
3
+
4
+ A Python library to map file extensions to MIME types.
5
+ It also provides a CLI for quick lookups right from your terminal.
6
+ If no known mapping is found, the tool returns `application/octet-stream`.
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ uv pip install content-types
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ```python
17
+ import content_types
18
+
19
+ # Forward lookup: filename -> MIME type
20
+ mime_type = content_types.get_content_type("example.jpg")
21
+ print(mime_type) # "image/jpeg"
22
+
23
+ # For very common files, you have shortcuts:
24
+ print(f'Content-Type for webp is {content_types.webp}') # 'image/webp'
25
+ ```
26
+
27
+ ## CLI
28
+
29
+ After installing in a virtual environment or system-wide.
30
+
31
+ ```bash
32
+ contenttypes example.jpg
33
+
34
+ # Outputs image/jpeg
35
+ ```
36
+
37
+ ## Contributing
38
+
39
+ Contributions are welcome! Check out [the GitHub repo](https://github.com/mikeckennedy/content-types)
40
+ for more details on how to get involved.
@@ -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,35 @@
1
+ [build-system]
2
+ requires = ["hatchling>=1.0"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "content-types"
7
+ version = "0.2.0" # You can manage version bumps manually or use a plugin for dynamic versioning
8
+ description = "A library to map file extensions to content types and vice versa."
9
+ readme = "README.md"
10
+ license = "MIT"
11
+ authors = [
12
+ { name = "Michael Kennedy", email = "mikeckennedy@gmail.com" }
13
+ ]
14
+ requires-python = ">=3.10, <=3.14"
15
+ keywords = ["mime", "content-type", "mapping", "file extensions"]
16
+ homepage = "https://github.com/mikeckennedy/content-types"
17
+
18
+ # If you have no runtime dependencies, this can be an empty array or omitted
19
+ dependencies = []
20
+
21
+ classifiers = [
22
+ "License :: OSI Approved :: MIT License",
23
+ "Intended Audience :: Developers",
24
+ "Operating System :: OS Independent",
25
+ "Programming Language :: Python :: 3.10",
26
+ "Programming Language :: Python :: 3.11",
27
+ "Programming Language :: Python :: 3.12",
28
+ "Programming Language :: Python :: 3.13",
29
+ "Programming Language :: Python :: 3.14",
30
+ "Topic :: Internet :: WWW/HTTP",
31
+ "Topic :: Software Development :: Libraries :: Python Modules"
32
+ ]
33
+
34
+ [project.scripts]
35
+ content-types = "content_types:cli"
@@ -0,0 +1,43 @@
1
+ # [ruff]
2
+ line-length = 120
3
+ format.quote-style = "single"
4
+
5
+ # Enable Pyflakes `E` and `F` codes by default.
6
+ lint.select = ["E", "F"]
7
+ lint.ignore = []
8
+
9
+ # Exclude a variety of commonly ignored directories.
10
+ exclude = [
11
+ ".bzr",
12
+ ".direnv",
13
+ ".eggs",
14
+ ".git",
15
+ ".hg",
16
+ ".mypy_cache",
17
+ ".nox",
18
+ ".pants.d",
19
+ ".ruff_cache",
20
+ ".svn",
21
+ ".tox",
22
+ "__pypackages__",
23
+ "_build",
24
+ "buck-out",
25
+ "build",
26
+ "dist",
27
+ "node_modules",
28
+ ".env",
29
+ ".venv",
30
+ "venv",
31
+ ]
32
+ lint.per-file-ignores = {}
33
+
34
+ # Allow unused variables when underscore-prefixed.
35
+ # dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
36
+
37
+ # Assume Python 3.13.
38
+ target-version = "py313"
39
+
40
+ #[tool.ruff.mccabe]
41
+ ## Unlike Flake8, default to a complexity level of 10.
42
+ lint.mccabe.max-complexity = 10
43
+