Skip to content

Commit

Permalink
Rely on email.message to parse a header. (#969)
Browse files Browse the repository at this point in the history
* Rely on email.message to parse a header.

* πŸ‘Ή Feed the hobgoblins (delint).

* πŸ§Žβ€β™€οΈ Genuflect to the types.

* πŸ§Žβ€β™€οΈ Genuflect to the types.

* Indicate private utility method

* Replace comment with docstring

Co-authored-by: Brian Rutledge <brian@bhrutledge.com>
  • Loading branch information
jaraco and bhrutledge committed Jan 6, 2023
1 parent a33d984 commit 05cefc0
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions twine/commands/check.py
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]]:
"""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

0 comments on commit 05cefc0

Please sign in to comment.