Commit fbd8591e authored by Jérome Perrin's avatar Jérome Perrin

util: reimplement parseContentType with something python3 compatible

parent f0be8d52
...@@ -248,7 +248,7 @@ class Handler: ...@@ -248,7 +248,7 @@ class Handler:
# XXX please never guess extension from mimetype # XXX please never guess extension from mimetype
output_set = set() output_set = set()
if "/" in source_mimetype: if "/" in source_mimetype:
parsed_mimetype_type = parseContentType(source_mimetype).gettype() parsed_mimetype_type = parseContentType(source_mimetype).get_content_type()
# here `guess_all_extensions` never handles mimetype parameters # here `guess_all_extensions` never handles mimetype parameters
# (even for `text/plain;charset=UTF-8` which is standard) # (even for `text/plain;charset=UTF-8` which is standard)
extension_list = mimetypes.guess_all_extensions(parsed_mimetype_type) # XXX never guess extension_list = mimetypes.guess_all_extensions(parsed_mimetype_type) # XXX never guess
......
...@@ -127,7 +127,7 @@ class Handler: ...@@ -127,7 +127,7 @@ class Handler:
... ...
] ]
""" """
source_mimetype = parseContentType(source_mimetype).gettype() source_mimetype = parseContentType(source_mimetype).get_content_type()
if source_mimetype in ("application/pdf", "pdf"): if source_mimetype in ("application/pdf", "pdf"):
return [("text/plain", "Plain Text")] return [("text/plain", "Plain Text")]
return [] return []
...@@ -109,7 +109,7 @@ class Handler: ...@@ -109,7 +109,7 @@ class Handler:
... ...
] ]
""" """
source_mimetype = parseContentType(source_mimetype).gettype() source_mimetype = parseContentType(source_mimetype).get_content_type()
if source_mimetype in ("text/html", "htm", "html"): if source_mimetype in ("text/html", "htm", "html"):
return [("application/pdf", "PDF - Portable Document Format")] return [("application/pdf", "PDF - Portable Document Format")]
return [] return []
......
...@@ -338,7 +338,7 @@ class Handler: ...@@ -338,7 +338,7 @@ class Handler:
... ...
] ]
""" """
source_mimetype = parseContentType(source_mimetype).gettype() source_mimetype = parseContentType(source_mimetype).get_content_type()
if source_mimetype in ("docy", "application/x-asc-text"): if source_mimetype in ("docy", "application/x-asc-text"):
return [ return [
("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Word 2007 Document"), ("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Word 2007 Document"),
......
...@@ -93,7 +93,7 @@ def BBB_guess_extension(mimetype, title=None): ...@@ -93,7 +93,7 @@ def BBB_guess_extension(mimetype, title=None):
"text/html": ".html", "text/html": ".html",
"text/plain": ".txt", "text/plain": ".txt",
"image/jpeg": ".jpg", "image/jpeg": ".jpg",
}.get(parseContentType(mimetype).gettype(), None) or guess_extension(mimetype) }.get(parseContentType(mimetype).get_content_type(), None) or guess_extension(mimetype)
@implementer(IManager, IERP5Compatibility, ITableGranulator, IImageGranulator, @implementer(IManager, IERP5Compatibility, ITableGranulator, IImageGranulator,
ITextGranulator) ITextGranulator)
......
...@@ -28,12 +28,12 @@ ...@@ -28,12 +28,12 @@
# #
############################################################################## ##############################################################################
import email
import email.message
import logging import logging
import mimetypes import mimetypes
import pkg_resources import pkg_resources
import os import os
import mimetools
import cStringIO
from zipfile import ZipFile, ZIP_DEFLATED from zipfile import ZipFile, ZIP_DEFLATED
logger = logging.getLogger('Cloudooo') logger = logging.getLogger('Cloudooo')
...@@ -126,17 +126,13 @@ def unzip(source, destination): ...@@ -126,17 +126,13 @@ def unzip(source, destination):
with ZipFile(source) as zipfile: with ZipFile(source) as zipfile:
zipfile.extractall(destination) zipfile.extractall(destination)
def parseContentType(content_type): def parseContentType(content_type:str) -> email.message.EmailMessage:
"""Parses `text/plain;charset="utf-8"` to a mimetools.Message object. """Parses `text/plain;charset="utf-8"` to an email object.
Note: Content type or MIME type are built like `maintype/subtype[;params]`. Note: Content type or MIME type are built like `maintype/subtype[;params]`.
parsed_content_type = parseContentType('text/plain;charset="utf-8"') parsed_content_type = parseContentType('text/plain;charset="utf-8"')
parsed_content_type.gettype() -> 'text/plain' parsed_content_type.get_content_type() -> 'text/plain'
parsed_content_type.getmaintype() -> 'text' parsed_content_type.get_charset() -> 'utf-8'
parsed_content_type.getsubtype() -> 'plain'
parsed_content_type.getplist() -> 'charset="utf-8"'
parsed_content_type.getparam('charset') -> 'utf-8'
parsed_content_type.typeheader -> 'text/plain;charset="utf-8"'
""" """
return mimetools.Message(cStringIO.StringIO("Content-Type:" + content_type.replace("\r\n", "\r\n\t"))) return email.message_from_string("Content-Type:" + content_type.replace("\r\n", "\r\n\t"))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment