Skip to content

Commit 0491b79

Browse files
committed
Some more yet unused implementation
1 parent 179b020 commit 0491b79

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

src/pip/_internal/metadata/base.py

+46-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,38 @@
11
import abc
22

3+
from pip._internal.utils.misc import stdlib_pkgs # TODO: Move definition here.
34
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
45

56
if MYPY_CHECK_RUNNING:
6-
from typing import List, Optional
7+
from typing import Container, Iterator, List, Optional
78

89

910
class BaseDistribution(metadata=abc.ABCMeta):
11+
@property
12+
def canonical_name(self):
13+
# type: () -> str
14+
raise NotImplementedError()
15+
1016
@property
1117
def installer(self):
1218
# type: () -> str
1319
raise NotImplementedError()
1420

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

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

src/pip/_internal/metadata/pkg_resources.py

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

4+
from pip._internal.utils.misc import (
5+
dist_in_usersite,
6+
dist_is_editable,
7+
dist_is_local,
8+
)
39
from pip._internal.utils.packaging import get_installer
410
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
511

612
from .base import BaseDistribution, BaseEnvironment
713

814
if MYPY_CHECK_RUNNING:
9-
from typing import List, Optional
15+
from typing import Iterator, List, Optional
1016

1117

1218
class Distribution(BaseDistribution):
1319
def __init__(self, dist):
1420
# type: (pkg_resources.Distribution) -> None
1521
self._dist = dist
1622

23+
@property
24+
def canonical_name(self):
25+
# type: () -> str
26+
return canonicalize_name(self._dist.project_name)
27+
1728
@property
1829
def installer(self):
1930
# type: () -> str
20-
# TODO: Move get_installer() implementation here.
2131
return get_installer(self._dist)
2232

33+
@property
34+
def editable(self):
35+
# type: () -> bool
36+
return dist_is_editable(self._dist)
37+
38+
@property
39+
def local(self):
40+
# type: () -> bool
41+
return dist_is_local(self._dist)
42+
43+
@property
44+
def in_usersite(self):
45+
# type: () -> bool
46+
return dist_in_usersite(self._dist)
47+
2348

2449
class Environment(BaseEnvironment):
2550
def __init__(self, ws):
@@ -44,3 +69,8 @@ def get_distribution(self, name):
4469
except pkg_resources.DistributionNotFound:
4570
return None
4671
return Distribution(dist)
72+
73+
def iter_distributions(self):
74+
# type: () -> Iterator[BaseDistribution]
75+
for dist in self._ws:
76+
yield Distribution(dist)

0 commit comments

Comments
 (0)