mime-enum 0.0.1a1__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.
mime_enum/__init__.py ADDED
@@ -0,0 +1,4 @@
1
+ from mime_enum.core import from_extension, from_path, parse, try_parse
2
+ from mime_enum.mimetype import MimeType
3
+
4
+ __all__ = ("MimeType", "from_extension", "from_path", "parse", "try_parse")
mime_enum/core.py ADDED
@@ -0,0 +1,164 @@
1
+ # src/mime_enum/api.py (or core.py)
2
+ from __future__ import annotations
3
+
4
+ import re
5
+ from pathlib import Path
6
+
7
+ # Import the generated enum + maps
8
+ from .mimetype import _ALIASES, _EXT_TO_MIME, MimeType
9
+
10
+ _PARAM_RE = re.compile(r"\s*;.*$")
11
+
12
+
13
+ def _strip_params(value: str) -> str:
14
+ return _PARAM_RE.sub("", value).strip().lower()
15
+
16
+
17
+ def parse(value: str) -> MimeType:
18
+ """Parse a MIME type string to a MimeType enum.
19
+
20
+ Performs strict parsing with the following behavior:
21
+
22
+ - Strips parameters (e.g., 'application/json; charset=utf-8' becomes 'application/json')
23
+ - Normalizes known aliases to their canonical form
24
+ - Case-insensitive matching
25
+ - Raises ValueError for unknown MIME types
26
+
27
+ Args:
28
+ value: The MIME type string to parse (e.g., 'application/json')
29
+
30
+ Returns:
31
+ The corresponding MimeType enum value
32
+
33
+ Raises:
34
+ ValueError: If the MIME type is empty, malformed, or unknown
35
+
36
+ Examples:
37
+ >>> parse('application/json')
38
+ MimeType.APPLICATION_JSON
39
+ >>> parse('application/json; charset=utf-8')
40
+ MimeType.APPLICATION_JSON
41
+ >>> parse('text/json') # alias normalization
42
+ MimeType.APPLICATION_JSON
43
+ """
44
+ if not value:
45
+ raise ValueError("Empty MIME string") # noqa: TRY003
46
+ core = _strip_params(value)
47
+ if core in _ALIASES:
48
+ return _ALIASES[core]
49
+ try:
50
+ return MimeType(core)
51
+ except ValueError as exc:
52
+ raise ValueError(f"Unknown MIME type: {value!r}") from exc # noqa: TRY003
53
+
54
+
55
+ def try_parse(value: str) -> MimeType | None:
56
+ """Parse a MIME type string, returning None for unknown types.
57
+
58
+ Similar to parse() but returns None instead of raising ValueError
59
+ for unknown or empty MIME type strings.
60
+
61
+ Args:
62
+ value: The MIME type string to parse (e.g., 'application/json')
63
+
64
+ Returns:
65
+ The corresponding MimeType enum value, or None if unknown/empty
66
+
67
+ Examples:
68
+ >>> try_parse('application/json')
69
+ MimeType.APPLICATION_JSON
70
+ >>> try_parse('unknown/type')
71
+ None
72
+ >>> try_parse('')
73
+ None
74
+ """
75
+ if not value:
76
+ return None
77
+ core = _strip_params(value)
78
+ if core in _ALIASES:
79
+ return _ALIASES[core]
80
+ try:
81
+ return MimeType(core)
82
+ except ValueError:
83
+ return None
84
+
85
+
86
+ def from_extension(ext: str) -> MimeType | None:
87
+ """Get MIME type from a file extension.
88
+
89
+ Performs case-insensitive lookup of MIME types by file extension.
90
+ Handles extensions with or without leading dot.
91
+
92
+ Args:
93
+ ext: File extension (e.g., 'json', '.json', 'PDF', '.PDF')
94
+
95
+ Returns:
96
+ The corresponding MimeType enum value, or None if extension is unknown
97
+
98
+ Note:
99
+ This function only looks at the file extension and does NOT examine
100
+ actual file content. Files can have incorrect or missing extensions,
101
+ making this method unreliable for security-critical applications.
102
+
103
+ For content-based MIME type detection, consider using packages like:
104
+
105
+ - `python-magic` (libmagic wrapper)
106
+ - `filetype` (pure Python file type detection)
107
+
108
+ Examples:
109
+ >>> from_extension('json')
110
+ MimeType.APPLICATION_JSON
111
+ >>> from_extension('.pdf')
112
+ MimeType.APPLICATION_PDF
113
+ >>> from_extension('unknown')
114
+ None
115
+ """
116
+ if not ext:
117
+ return None
118
+
119
+ token = ext.lstrip(".").lower()
120
+ return _EXT_TO_MIME.get(token)
121
+
122
+
123
+ def from_path(path: str | Path) -> MimeType | None:
124
+ """Get MIME type from a file path or filename.
125
+
126
+ Extracts the file extension from the path and looks up the
127
+ corresponding MIME type. Uses the last extension for compound
128
+ extensions (e.g., 'file.tar.gz' uses '.gz').
129
+
130
+ Args:
131
+ path: File path or filename (str or Path object)
132
+
133
+ Returns:
134
+ The corresponding MimeType enum value, or None if no extension
135
+ or unknown extension
136
+
137
+ Warning:
138
+ This function is purely extension-based and does NOT read or examine
139
+ the actual file content. This can be unreliable because:
140
+
141
+ - Files may have incorrect extensions (e.g., .txt file containing JSON)
142
+ - Files may be renamed with wrong extensions
143
+ - Files without extensions will return None
144
+ - Malicious files can masquerade with fake extensions
145
+
146
+ For accurate MIME type detection based on file signatures/magic bytes,
147
+ use content-based detection libraries like `python-magic` or `filetype`.
148
+
149
+ Examples:
150
+ >>> from_path('/tmp/document.pdf')
151
+ MimeType.APPLICATION_PDF
152
+ >>> from_path('data.json')
153
+ MimeType.APPLICATION_JSON
154
+ >>> from_path('file_without_extension')
155
+ None
156
+ """
157
+ if not path:
158
+ return None
159
+
160
+ p = Path(path)
161
+ if not p.suffix:
162
+ return None
163
+
164
+ return from_extension(p.suffix)