This commit is contained in:
J. Nick Koston 2023-10-04 14:24:16 -05:00
parent 49159c314c
commit 6e62169ee7
No known key found for this signature in database
2 changed files with 8 additions and 4 deletions

View File

@ -102,6 +102,7 @@ class Distribution(BaseDistribution):
self._dist = dist self._dist = dist
self._info_location = info_location self._info_location = info_location
self._installed_location = installed_location self._installed_location = installed_location
self._cached_canonical_name: Optional[NormalizedName] = None
@classmethod @classmethod
def from_directory(cls, directory: str) -> BaseDistribution: def from_directory(cls, directory: str) -> BaseDistribution:
@ -169,8 +170,10 @@ class Distribution(BaseDistribution):
@property @property
def canonical_name(self) -> NormalizedName: def canonical_name(self) -> NormalizedName:
name = self._get_dist_name_from_location() or get_dist_name(self._dist) if self._cached_canonical_name is None:
return canonicalize_name(name) name = self._get_dist_name_from_location() or get_dist_name(self._dist)
self._cached_canonical_name = canonicalize_name(name)
return self._cached_canonical_name
@property @property
def version(self) -> DistributionVersion: def version(self) -> DistributionVersion:

View File

@ -7,7 +7,7 @@ import re
from typing import FrozenSet, NewType, Tuple, Union, cast from typing import FrozenSet, NewType, Tuple, Union, cast
from .tags import Tag, parse_tag from .tags import Tag, parse_tag
from .version import InvalidVersion, Version from .version import parse_version, InvalidVersion, Version
BuildTag = Union[Tuple[()], Tuple[int, str]] BuildTag = Union[Tuple[()], Tuple[int, str]]
NormalizedName = NewType("NormalizedName", str) NormalizedName = NewType("NormalizedName", str)
@ -30,6 +30,7 @@ _canonicalize_regex = re.compile(r"[-_.]+")
_build_tag_regex = re.compile(r"(\d+)(.*)") _build_tag_regex = re.compile(r"(\d+)(.*)")
@functools.lru_cache(maxsize=4096)
def canonicalize_name(name: str) -> NormalizedName: def canonicalize_name(name: str) -> NormalizedName:
# This is taken from PEP 503. # This is taken from PEP 503.
value = _canonicalize_regex.sub("-", name).lower() value = _canonicalize_regex.sub("-", name).lower()
@ -44,7 +45,7 @@ def canonicalize_version(version: Union[Version, str]) -> str:
""" """
if isinstance(version, str): if isinstance(version, str):
try: try:
parsed = Version(version) parsed = parse_version(version)
except InvalidVersion: except InvalidVersion:
# Legacy versions cannot be normalized # Legacy versions cannot be normalized
return version return version