From 05cefc072772361b92984c50a556113c498aa9c5 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 6 Jan 2023 06:50:00 -0500 Subject: [PATCH] Rely on email.message to parse a header. (#969) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- twine/commands/check.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/twine/commands/check.py b/twine/commands/check.py index 5942f221..90b2e375 100644 --- a/twine/commands/check.py +++ b/twine/commands/check.py @@ -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 @@ -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]: @@ -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":