Skip to content

Commit 56de9dd

Browse files
committed
init commit
1 parent 139730c commit 56de9dd

File tree

11 files changed

+1836
-1717
lines changed

11 files changed

+1836
-1717
lines changed

.github/workflows/on-release-main.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,3 @@ jobs:
6464

6565
- name: Deploy documentation
6666
run: uv run mkdocs gh-deploy --force
67-

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def set_content_type(mime: MimeType) -> str:
4545
return f"Content-Type: {mime}"
4646
```
4747

48-
### Flexible String Parsing
48+
### Flexible String Parsing
4949

5050
Parse real-world MIME type strings with automatic parameter stripping and alias normalization:
5151

@@ -69,7 +69,7 @@ print(unknown) # None
6969

7070
Detect MIME types from file extensions and paths:
7171

72-
```python
72+
```python
7373
from mime_enum import from_extension, from_path
7474

7575
# Look up by extension (with or without dot, case-insensitive)

docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ This page contains the complete API documentation for the `mime-enum` package, a
1111

1212
## MimeType Enum
1313

14-
::: mime_enum.mimetype.MimeType
14+
::: mime_enum.mimetype.MimeType

docs/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def set_content_type(mime: MimeType) -> str:
4545
return f"Content-Type: {mime}"
4646
```
4747

48-
### Flexible String Parsing
48+
### Flexible String Parsing
4949

5050
Parse real-world MIME type strings with automatic parameter stripping and alias normalization:
5151

@@ -69,7 +69,7 @@ print(unknown) # None
6969

7070
Detect MIME types from file extensions and paths:
7171

72-
```python
72+
```python
7373
from mime_enum import from_extension, from_path
7474

7575
# Look up by extension (with or without dot, case-insensitive)
@@ -84,4 +84,4 @@ mime_type = from_path("/path/to/document.pdf") # MimeType.APPLICATION_PDF
8484
8585
For detailed usage examples, see the [Usage Guide](usage.md).
8686

87-
For complete API documentation, see the [API Reference](api.md).
87+
For complete API documentation, see the [API Reference](api.md).

mimeData.json

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@
109109
"communityContributed": false,
110110
"popularUsage": null
111111
}
112-
},{
112+
},
113+
{
113114
"name": "image/avif-sequence",
114115
"description": "AVIF (AV1 Image File Format) is an image and animation file format, an extension of <a href=\"/image/heif\">HEIF</a> allowing images encoded with AV1. Most files will only have AV1-encoded images, but a mixture of different encodings is allowed. Early draft versions were named AV1 Still Image File Format.",
115116
"furtherReading": [
@@ -356,7 +357,7 @@
356357
},
357358
{
358359
"name": "image/x-icns",
359-
"description": "ICNS (often written in lowercase as icns) is the native icon format of Mac OS and OS X.<br /><br />It goes by many other names, including Apple Icon, Macintosh Icon, Mac OS Icon, Macintosh OS X Icon, Mac OS X Icon Resource, Mac OS icns Resource, and IconFamily Resource.<br /><br />There are usually multiple images in a file. Supported image sizes are 16×12, and 16, 32, 48, 128, 256, 512, and 1024 pixels square.",
360+
"description": "ICNS (often written in lowercase as icns) is the native icon format of Mac OS and OS X.<br /><br />It goes by many other names, including Apple Icon, Macintosh Icon, Mac OS Icon, Macintosh OS X Icon, Mac OS X Icon Resource, Mac OS icns Resource, and IconFamily Resource.<br /><br />There are usually multiple images in a file. Supported image sizes are 16\u00d712, and 16, 32, 48, 128, 256, 512, and 1024 pixels square.",
360361
"furtherReading": [
361362
{
362363
"title": "Let's Solve the File Format Problem!",
@@ -5406,8 +5407,7 @@
54065407
}
54075408
],
54085409
"links": {
5409-
"deprecates": [
5410-
],
5410+
"deprecates": [],
54115411
"relatedTo": [],
54125412
"parentOf": [
54135413
"audio/x-aiff"
@@ -6619,7 +6619,7 @@
66196619
},
66206620
{
66216621
"name": "application/yaml",
6622-
"description": "YAML (YAML Ain’t Markup Language) is a serialization format. It is a strict superset of JSON, with the addition of syntactically significant newlines and indentation, like Python. Unlike Python, however, YAML doesn’t allow literal tab characters for indentation. It is designed to be human-friendly and work well with modern programming languages for common everyday tasks. It is broadly useful for programming needs ranging from configuration files to Internet messaging to object persistence to data auditing. Together with the Unicode standard for characters, it is also well-suited for internationalization.",
6622+
"description": "YAML (YAML Ain\u2019t Markup Language) is a serialization format. It is a strict superset of JSON, with the addition of syntactically significant newlines and indentation, like Python. Unlike Python, however, YAML doesn\u2019t allow literal tab characters for indentation. It is designed to be human-friendly and work well with modern programming languages for common everyday tasks. It is broadly useful for programming needs ranging from configuration files to Internet messaging to object persistence to data auditing. Together with the Unicode standard for characters, it is also well-suited for internationalization.",
66236623
"furtherReading": [
66246624
{
66256625
"title": "Internet Assigned Numbers Authority (IANA)",
@@ -7570,7 +7570,7 @@
75707570
},
75717571
{
75727572
"name": "video/x-matroska",
7573-
"description": "Matroska (sometimes spelled Matroška) is an open standard multimedia container format. It is based on the EBML metaformat and can be used with a variety of compressed video formats.",
7573+
"description": "Matroska (sometimes spelled Matro\u0161ka) is an open standard multimedia container format. It is based on the EBML metaformat and can be used with a variety of compressed video formats.",
75747574
"furtherReading": [
75757575
{
75767576
"title": "Let's Solve the File Format Problem!",
@@ -8127,7 +8127,7 @@
81278127
},
81288128
{
81298129
"name": "text/x-pascal",
8130-
"description": "Pascal is an influential imperative and procedural programming language, designed in 1968–1969 and published in 1970. Borland's Turbo Pascal, and the later Borland Pascal, were popular in the 1980s and early 1990s on the PC/MS-DOS platform (CP/M versions were also released).",
8130+
"description": "Pascal is an influential imperative and procedural programming language, designed in 1968\u20131969 and published in 1970. Borland's Turbo Pascal, and the later Borland Pascal, were popular in the 1980s and early 1990s on the PC/MS-DOS platform (CP/M versions were also released).",
81318131
"furtherReading": [
81328132
{
81338133
"title": "Let's Solve the File Format Problem!",
@@ -8985,7 +8985,7 @@
89858985
},
89868986
{
89878987
"name": "application/prql",
8988-
"description": "PRQL is a modern language for transforming data a simple, powerful, pipelined SQL replacement.",
8988+
"description": "PRQL is a modern language for transforming data \u2014 a simple, powerful, pipelined SQL replacement.",
89898989
"furtherReading": [
89908990
{
89918991
"title": "Let's Solve the File Format Problem!",
@@ -9147,16 +9147,25 @@
91479147
"deprecates": [],
91489148
"relatedTo": [],
91499149
"parentOf": [],
9150-
"alternativeTo": ["text/x-python-code", "text/x-python-script", "application/x-python-code", "application/x-python-script"]
9150+
"alternativeTo": [
9151+
"text/x-python-code",
9152+
"text/x-python-script",
9153+
"application/x-python-code",
9154+
"application/x-python-script"
9155+
]
91519156
},
91529157
"fileTypes": [
9153-
".py", ".pyc", ".pyo", ".pyd", ".whl"
9158+
".py",
9159+
".pyc",
9160+
".pyo",
9161+
".pyd",
9162+
".whl"
91549163
],
91559164
"furtherReading": [
9156-
{
9157-
"title": "Let's Solve the File Format Problem!",
9158-
"url": "http://fileformats.archiveteam.org/wiki/Python"
9159-
}
9165+
{
9166+
"title": "Let's Solve the File Format Problem!",
9167+
"url": "http://fileformats.archiveteam.org/wiki/Python"
9168+
}
91609169
],
91619170
"notices": {
91629171
"hasNoOfficial": true,

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,3 @@ skip_empty = true
109109
[tool.coverage.run]
110110
branch = true
111111
source = ["src"]
112-

src/mime_enum/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
from mime_enum.core import parse, try_parse, from_extension, from_path
1+
from mime_enum.core import from_extension, from_path, parse, try_parse
22
from mime_enum.mimetype import MimeType
33

4-
__all__ = (
5-
"MimeType", "parse", "try_parse", "from_extension", "from_path"
6-
)
4+
__all__ = ("MimeType", "from_extension", "from_path", "parse", "try_parse")

src/mime_enum/core.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pathlib import Path
66

77
# Import the generated enum + maps
8-
from .mimetype import MimeType, _ALIASES, _EXT_TO_MIME
8+
from .mimetype import _ALIASES, _EXT_TO_MIME, MimeType
99

1010
_PARAM_RE = re.compile(r"\s*;.*$")
1111

@@ -16,23 +16,23 @@ def _strip_params(value: str) -> str:
1616

1717
def parse(value: str) -> MimeType:
1818
"""Parse a MIME type string to a MimeType enum.
19-
19+
2020
Performs strict parsing with the following behavior:
21-
21+
2222
- Strips parameters (e.g., 'application/json; charset=utf-8' becomes 'application/json')
2323
- Normalizes known aliases to their canonical form
2424
- Case-insensitive matching
2525
- Raises ValueError for unknown MIME types
26-
26+
2727
Args:
2828
value: The MIME type string to parse (e.g., 'application/json')
29-
29+
3030
Returns:
3131
The corresponding MimeType enum value
32-
32+
3333
Raises:
3434
ValueError: If the MIME type is empty, malformed, or unknown
35-
35+
3636
Examples:
3737
>>> parse('application/json')
3838
MimeType.APPLICATION_JSON
@@ -42,28 +42,28 @@ def parse(value: str) -> MimeType:
4242
MimeType.APPLICATION_JSON
4343
"""
4444
if not value:
45-
raise ValueError("Empty MIME string")
45+
raise ValueError("Empty MIME string") # noqa: TRY003
4646
core = _strip_params(value)
4747
if core in _ALIASES:
4848
return _ALIASES[core]
4949
try:
5050
return MimeType(core)
5151
except ValueError as exc:
52-
raise ValueError(f"Unknown MIME type: {value!r}") from exc
52+
raise ValueError(f"Unknown MIME type: {value!r}") from exc # noqa: TRY003
5353

5454

5555
def try_parse(value: str) -> MimeType | None:
5656
"""Parse a MIME type string, returning None for unknown types.
57-
57+
5858
Similar to parse() but returns None instead of raising ValueError
5959
for unknown or empty MIME type strings.
60-
60+
6161
Args:
6262
value: The MIME type string to parse (e.g., 'application/json')
63-
63+
6464
Returns:
6565
The corresponding MimeType enum value, or None if unknown/empty
66-
66+
6767
Examples:
6868
>>> try_parse('application/json')
6969
MimeType.APPLICATION_JSON
@@ -85,26 +85,26 @@ def try_parse(value: str) -> MimeType | None:
8585

8686
def from_extension(ext: str) -> MimeType | None:
8787
"""Get MIME type from a file extension.
88-
88+
8989
Performs case-insensitive lookup of MIME types by file extension.
9090
Handles extensions with or without leading dot.
91-
91+
9292
Args:
9393
ext: File extension (e.g., 'json', '.json', 'PDF', '.PDF')
94-
94+
9595
Returns:
9696
The corresponding MimeType enum value, or None if extension is unknown
97-
97+
9898
Note:
9999
This function only looks at the file extension and does NOT examine
100100
actual file content. Files can have incorrect or missing extensions,
101101
making this method unreliable for security-critical applications.
102-
102+
103103
For content-based MIME type detection, consider using packages like:
104-
104+
105105
- `python-magic` (libmagic wrapper)
106106
- `filetype` (pure Python file type detection)
107-
107+
108108
Examples:
109109
>>> from_extension('json')
110110
MimeType.APPLICATION_JSON
@@ -122,30 +122,30 @@ def from_extension(ext: str) -> MimeType | None:
122122

123123
def from_path(path: str | Path) -> MimeType | None:
124124
"""Get MIME type from a file path or filename.
125-
125+
126126
Extracts the file extension from the path and looks up the
127127
corresponding MIME type. Uses the last extension for compound
128128
extensions (e.g., 'file.tar.gz' uses '.gz').
129-
129+
130130
Args:
131131
path: File path or filename (str or Path object)
132-
132+
133133
Returns:
134134
The corresponding MimeType enum value, or None if no extension
135135
or unknown extension
136-
136+
137137
Warning:
138138
This function is purely extension-based and does NOT read or examine
139139
the actual file content. This can be unreliable because:
140-
140+
141141
- Files may have incorrect extensions (e.g., .txt file containing JSON)
142142
- Files may be renamed with wrong extensions
143143
- Files without extensions will return None
144144
- Malicious files can masquerade with fake extensions
145-
145+
146146
For accurate MIME type detection based on file signatures/magic bytes,
147147
use content-based detection libraries like `python-magic` or `filetype`.
148-
148+
149149
Examples:
150150
>>> from_path('/tmp/document.pdf')
151151
MimeType.APPLICATION_PDF

0 commit comments

Comments
 (0)