classification
Title: add filename_extension_map and/or content-types_map dict(s) to mimetypes
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Daniel Black, r.david.murray, xtreak
Priority: normal Keywords:

Created on 2019-03-30 21:18 by Daniel Black, last changed 2019-04-04 04:01 by xtreak.

Messages (4)
msg339221 - (view) Author: Daniel Black (Daniel Black) Date: 2019-03-30 21:18
In https://bugs.python.org/issue36460 (which should be probably be disregarded until AMP is in RFC) I discovered that the types_map dictionary within the mimetypes module is a key: str to key: str (1:1) relationship. The reality is that many filename extensions commonly support multiple content-types and even sub types. A more useful structure might look like:

(fne is "file name extension" aka foo)

{
  '.fne': ['type1/subtype', 'type2/subtype'],
  '.htm': ['text/html', 'text/css', 'text/javascript'],
  '.html': ['text/html', 'text/css', 'text/javascript']
} 

However this seems to compete with the functionality of the types map so another consideration is content-types_map where the content-type is the key and the pair values are lists of valid filename extensions:

{
  'audio/x-aiff': ['.aifc', '.aiff', '.au'],
  'text/html': ['.html', '.htm']
}
msg339388 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python triager) Date: 2019-04-03 12:35
> However this seems to compete with the functionality of the types map so another consideration is content-types_map where the content-type is the key and the pair values are lists of valid filename extensions:

There is mimetypes.MimeTypes().types_map_inv

> https://docs.python.org/3.8/library/mimetypes.html#mimetypes.MimeTypes.types_map_inv
> Tuple containing two dictionaries, mapping MIME types to a list of filename extensions: the first dictionary is for the non-standards types and the second one is for the standard types. They are initialized by common_types and types_map.

>>> mimetypes.MimeTypes().types_map_inv[1]['text/html']
['.htm', '.html']
msg339417 - (view) Author: Daniel Black (Daniel Black) Date: 2019-04-03 20:15
Thanks Karthikeyan. That is a bit cumbersome but gets it done. Any value seen in adding as proposed?
msg339431 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python triager) Date: 2019-04-04 04:01
The docs state the tuple contains two dictionaries depending on how common the type is. I guess you want a merged dict to be provided as an API? I will leave it to @r.david.murray on that.
History
Date User Action Args
2019-04-04 04:01:00xtreaksetmessages: + msg339431
2019-04-03 20:15:36Daniel Blacksetmessages: + msg339417
2019-04-03 12:35:10xtreaksetnosy: + xtreak
messages: + msg339388
2019-04-03 08:31:21SilentGhostsetnosy: + r.david.murray

type: enhancement
components: + Library (Lib)
versions: + Python 3.8, Python 3.9
2019-03-30 21:18:32Daniel Blackcreate