Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rely on email.message to parse a header. #969

Merged
merged 7 commits into from
Jan 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions twine/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import cgi
import email.message
import io
import logging
import re
from typing import List, Optional, Tuple, cast
from typing import Dict, List, Optional, Tuple, cast

import readme_renderer.rst
from rich import print
Expand Down Expand Up @@ -63,6 +63,16 @@ def __str__(self) -> str:
return self.getvalue().strip()


def _parse_content_type(value: str) -> Tuple[str, Dict[str, str]]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I tweaked this to be aligned with the other utility methods/classes in this module.

"""Implement logic of deprecated cgi.parse_header().

From https://docs.python.org/3.11/library/cgi.html#cgi.parse_header.
"""
msg = email.message.EmailMessage()
msg["content-type"] = value
return msg.get_content_type(), msg["content-type"].params


def _check_file(
filename: str, render_warning_stream: _WarningStream
) -> Tuple[List[str], bool]:
Expand All @@ -82,7 +92,7 @@ def _check_file(
)
description_content_type = "text/x-rst"

content_type, params = cgi.parse_header(description_content_type)
content_type, params = _parse_content_type(description_content_type)
renderer = _RENDERERS.get(content_type, _RENDERERS[None])

if description is None or description.rstrip() == "UNKNOWN":
Expand Down