MCPcopy Index your code
hub / github.com/python/cpython / Prepared

Class Prepared

Lib/importlib/metadata/__init__.py:927–980  ·  view source on GitHub ↗

A prepared search query for metadata on a possibly-named package. Pre-calculates the normalization to prevent repeated operations. >>> none = Prepared(None) >>> none.normalized >>> none.legacy_normalized >>> bool(none) False >>> sample = Prepared('Sample__Pkg-name.

Source from the content-addressed store, hash-verified

925
926
927class Prepared:
928 """
929 A prepared search query for metadata on a possibly-named package.
930
931 Pre-calculates the normalization to prevent repeated operations.
932
933 >>> none = Prepared(None)
934 >>> none.normalized
935 >>> none.legacy_normalized
936 >>> bool(none)
937 False
938 >>> sample = Prepared('Sample__Pkg-name.foo')
939 >>> sample.normalized
940 'sample_pkg_name_foo'
941 >>> sample.legacy_normalized
942 'sample__pkg_name.foo'
943 >>> bool(sample)
944 True
945 """
946
947 normalized = None
948 legacy_normalized = None
949
950 def __init__(self, name: str | None):
951 self.name = name
952 if name is None:
953 return
954 self.normalized = self.normalize(name)
955 self.legacy_normalized = self.legacy_normalize(name)
956
957 @staticmethod
958 def normalize(name):
959 """
960 PEP 503 normalization plus dashes as underscores.
961
962 Specifically avoids ``re.sub`` as prescribed for performance
963 benefits (see python/cpython#143658).
964 """
965 value = name.lower().replace("-", "_").replace(".", "_")
966 # Condense repeats
967 while "__" in value:
968 value = value.replace("__", "_")
969 return value
970
971 @staticmethod
972 def legacy_normalize(name):
973 """
974 Normalize the package name as found in the convention in
975 older packaging tools versions and specs.
976 """
977 return name.lower().replace('-', '_')
978
979 def __bool__(self):
980 return bool(self.name)
981
982
983class MetadataPathFinder(DistributionFinder):

Callers 1

_search_pathsMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…