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

Class Lookup

Lib/importlib/metadata/__init__.py:876–924  ·  view source on GitHub ↗

A micro-optimized class for searching a (fast) path for metadata.

Source from the content-addressed store, hash-verified

874
875
876class Lookup:
877 """
878 A micro-optimized class for searching a (fast) path for metadata.
879 """
880
881 def __init__(self, path: FastPath):
882 """
883 Calculate all of the children representing metadata.
884
885 From the children in the path, calculate early all of the
886 children that appear to represent metadata (infos) or legacy
887 metadata (eggs).
888 """
889
890 base = os.path.basename(path.root).lower()
891 base_is_egg = base.endswith(".egg")
892 self.infos = FreezableDefaultDict(list)
893 self.eggs = FreezableDefaultDict(list)
894
895 for child in path.children():
896 low = child.lower()
897 if low.endswith((".dist-info", ".egg-info")):
898 # rpartition is faster than splitext and suitable for this purpose.
899 name = low.rpartition(".")[0].partition("-")[0]
900 normalized = Prepared.normalize(name)
901 self.infos[normalized].append(path.joinpath(child))
902 elif base_is_egg and low == "egg-info":
903 name = base.rpartition(".")[0].partition("-")[0]
904 legacy_normalized = Prepared.legacy_normalize(name)
905 self.eggs[legacy_normalized].append(path.joinpath(child))
906
907 self.infos.freeze()
908 self.eggs.freeze()
909
910 def search(self, prepared: Prepared):
911 """
912 Yield all infos and eggs matching the Prepared query.
913 """
914 infos = (
915 self.infos[prepared.normalized]
916 if prepared
917 else itertools.chain.from_iterable(self.infos.values())
918 )
919 eggs = (
920 self.eggs[prepared.legacy_normalized]
921 if prepared
922 else itertools.chain.from_iterable(self.eggs.values())
923 )
924 return itertools.chain(infos, eggs)
925
926
927class Prepared:

Callers 1

lookupMethod · 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…