55import sys
66import zipp
77import email
8- import string
98import pathlib
109import operator
1110import textwrap
1615import contextlib
1716import collections
1817
18+ from . import _adapters , _meta
1919from ._collections import FreezableDefaultDict , Pair
2020from ._compat import (
2121 NullFinder ,
22- Protocol ,
2322 PyPy_repr ,
2423 install ,
2524)
3029from importlib import import_module
3130from importlib .abc import MetaPathFinder
3231from itertools import starmap
33- from typing import Any , List , Mapping , Optional , TypeVar , Union
32+ from typing import List , Mapping , Optional , Union
3433
3534
3635__all__ = [
@@ -394,25 +393,6 @@ def __repr__(self):
394393 return '<FileHash mode: {} value: {}>' .format (self .mode , self .value )
395394
396395
397- _T = TypeVar ("_T" )
398-
399-
400- class PackageMetadata (Protocol ):
401- def __len__ (self ) -> int :
402- ... # pragma: no cover
403-
404- def __contains__ (self , item : str ) -> bool :
405- ... # pragma: no cover
406-
407- def __getitem__ (self , key : str ) -> str :
408- ... # pragma: no cover
409-
410- def get_all (self , name : str , failobj : _T = ...) -> Union [List [Any ], _T ]:
411- """
412- Return all values associated with a possibly multi-valued key.
413- """
414-
415-
416396class Distribution :
417397 """A Python distribution package."""
418398
@@ -497,7 +477,7 @@ def _local(cls, root='.'):
497477 return PathDistribution (zipp .Path (meta .build_as_zip (builder )))
498478
499479 @property
500- def metadata (self ) -> PackageMetadata :
480+ def metadata (self ) -> _meta . PackageMetadata :
501481 """Return the parsed metadata for this Distribution.
502482
503483 The returned object will have keys that name the various bits of
@@ -511,7 +491,7 @@ def metadata(self) -> PackageMetadata:
511491 # (which points to the egg-info file) attribute unchanged.
512492 or self .read_text ('' )
513493 )
514- return email .message_from_string (text )
494+ return _adapters . JSONMeta ( email .message_from_string (text ) )
515495
516496 @property
517497 def name (self ):
@@ -843,7 +823,7 @@ def distributions(**kwargs):
843823 return Distribution .discover (** kwargs )
844824
845825
846- def metadata (distribution_name ) -> PackageMetadata :
826+ def metadata (distribution_name ) -> _meta . PackageMetadata :
847827 """Get the metadata for the named package.
848828
849829 :param distribution_name: The name of the distribution package to query.
@@ -920,43 +900,3 @@ def packages_distributions() -> Mapping[str, List[str]]:
920900 for pkg in (dist .read_text ('top_level.txt' ) or '' ).split ():
921901 pkg_to_dist [pkg ].append (dist .metadata ['Name' ])
922902 return dict (pkg_to_dist )
923-
924-
925- def as_json (metadata : PackageMetadata ):
926- """
927- Convert PackageMetadata to a JSON-compatible format
928- per PEP 0566.
929- """
930- # TODO: Need to match case-insensitive
931- multiple_use = {
932- 'Classifier' ,
933- 'Obsoletes-Dist' ,
934- 'Platform' ,
935- 'Project-URL' ,
936- 'Provides-Dist' ,
937- 'Provides-Extra' ,
938- 'Requires-Dist' ,
939- 'Requires-External' ,
940- 'Supported-Platform' ,
941- }
942-
943- def redent (value ):
944- "Correct for RFC822 indentation"
945- if not value or '\n ' not in value :
946- return value
947- return textwrap .dedent (' ' * 8 + value )
948-
949- def transform (key ):
950- value = (
951- metadata .get_all (key ) if key in multiple_use else redent (metadata .get (key ))
952- )
953- if key == 'Keywords' :
954- value = value .split (string .whitespace )
955- if not value and key == 'Description' :
956- value = metadata .get_payload ()
957- tk = key .lower ().replace ('-' , '_' )
958- return tk , value
959-
960- desc = ['Description' ] if metadata .get_payload () else [] # type: ignore
961- keys = itertools .chain (metadata , desc ) # type: ignore
962- return dict (map (transform , keys )) # type: ignore
0 commit comments