Skip to content

Commit 7e36104

Browse files
committed
Some more yet unused implementation
1 parent a08c4be commit 7e36104

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

src/pip/_internal/metadata/base.py

+46-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1+
from pip._internal.utils.misc import stdlib_pkgs # TODO: Move definition here.
12
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
23

34
if MYPY_CHECK_RUNNING:
4-
from typing import List, Optional
5+
from typing import Container, Iterator, List, Optional
56

67

78
class BaseDistribution:
9+
@property
10+
def canonical_name(self):
11+
# type: () -> str
12+
raise NotImplementedError()
13+
814
@property
915
def installer(self):
1016
# type: () -> str
1117
raise NotImplementedError()
1218

19+
@property
20+
def editable(self):
21+
# type: () -> bool
22+
raise NotImplementedError()
23+
24+
@property
25+
def local(self):
26+
# type: () -> bool
27+
raise NotImplementedError()
28+
29+
@property
30+
def in_usersite(self):
31+
# type: () -> bool
32+
raise NotImplementedError()
33+
1334

1435
class BaseEnvironment:
1536
"""An environment containing distributions to introspect."""
@@ -27,3 +48,27 @@ def from_paths(cls, paths):
2748
def get_distribution(self, name):
2849
# type: (str) -> Optional[BaseDistribution]
2950
raise NotImplementedError()
51+
52+
def iter_distributions(self):
53+
# type: () -> Iterator[BaseDistribution]
54+
raise NotImplementedError()
55+
56+
def iter_installed_distributions(
57+
self,
58+
local_only=True, # type: bool
59+
skip=stdlib_pkgs, # type: Container[str]
60+
include_editables=True, # type: bool
61+
editables_only=False, # type: bool
62+
user_only=False, # type: bool
63+
):
64+
# type: (...) -> Iterator[BaseDistribution]
65+
it = self.iter_distributions()
66+
if local_only:
67+
it = (d for d in it if d.local)
68+
if not include_editables:
69+
it = (d for d in it if not d.editable)
70+
if editables_only:
71+
it = (d for d in it if d.editable)
72+
if user_only:
73+
it = (d for d in it if d.in_usersite)
74+
return (d for d in it if d.canonical_name not in skip)

src/pip/_internal/metadata/pkg_resources.py

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,46 @@
11
from pip._vendor import pkg_resources
2+
from pip._vendor.packaging.utils import canonicalize_name
23

4+
from pip._internal.utils.misc import dist_in_usersite, dist_is_editable, dist_is_local
35
from pip._internal.utils.packaging import get_installer
46
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
57

68
from .base import BaseDistribution, BaseEnvironment
79

810
if MYPY_CHECK_RUNNING:
9-
from typing import List, Optional
11+
from typing import Iterator, List, Optional
1012

1113

1214
class Distribution(BaseDistribution):
1315
def __init__(self, dist):
1416
# type: (pkg_resources.Distribution) -> None
1517
self._dist = dist
1618

19+
@property
20+
def canonical_name(self):
21+
# type: () -> str
22+
return canonicalize_name(self._dist.project_name)
23+
1724
@property
1825
def installer(self):
1926
# type: () -> str
20-
# TODO: Move get_installer() implementation here.
2127
return get_installer(self._dist)
2228

29+
@property
30+
def editable(self):
31+
# type: () -> bool
32+
return dist_is_editable(self._dist)
33+
34+
@property
35+
def local(self):
36+
# type: () -> bool
37+
return dist_is_local(self._dist)
38+
39+
@property
40+
def in_usersite(self):
41+
# type: () -> bool
42+
return dist_in_usersite(self._dist)
43+
2344

2445
class Environment(BaseEnvironment):
2546
def __init__(self, ws):
@@ -44,3 +65,8 @@ def get_distribution(self, name):
4465
except pkg_resources.DistributionNotFound:
4566
return None
4667
return Distribution(dist)
68+
69+
def iter_distributions(self):
70+
# type: () -> Iterator[BaseDistribution]
71+
for dist in self._ws:
72+
yield Distribution(dist)

0 commit comments

Comments
 (0)