Skip to content

Commit 48aaf00

Browse files
committed
Some more yet unused implementation
1 parent 3842f5f commit 48aaf00

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
@@ -2,19 +2,40 @@
22

33
from pip._vendor.six import add_metaclass
44

5+
from pip._internal.utils.misc import stdlib_pkgs # TODO: Move definition here.
56
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
67

78
if MYPY_CHECK_RUNNING:
8-
from typing import List, Optional
9+
from typing import Container, Iterator, List, Optional
910

1011

1112
@add_metaclass(abc.ABCMeta)
1213
class BaseDistribution(object):
14+
@property
15+
def canonical_name(self):
16+
# type: () -> str
17+
raise NotImplementedError()
18+
1319
@property
1420
def installer(self):
1521
# type: () -> str
1622
raise NotImplementedError()
1723

24+
@property
25+
def editable(self):
26+
# type: () -> bool
27+
raise NotImplementedError()
28+
29+
@property
30+
def local(self):
31+
# type: () -> bool
32+
raise NotImplementedError()
33+
34+
@property
35+
def in_usersite(self):
36+
# type: () -> bool
37+
raise NotImplementedError()
38+
1839

1940
@add_metaclass(abc.ABCMeta)
2041
class BaseEnvironment(object):
@@ -33,3 +54,27 @@ def from_paths(cls, paths):
3354
def get_distribution(self, name):
3455
# type: (str) -> Optional[BaseDistribution]
3556
raise NotImplementedError()
57+
58+
def iter_distributions(self):
59+
# type: () -> Iterator[BaseDistribution]
60+
raise NotImplementedError()
61+
62+
def iter_installed_distributions(
63+
self,
64+
local_only=True, # type: bool
65+
skip=stdlib_pkgs, # type: Container[str]
66+
include_editables=True, # type: bool
67+
editables_only=False, # type: bool
68+
user_only=False, # type: bool
69+
):
70+
# type: (...) -> Iterator[BaseDistribution]
71+
it = self.iter_distributions()
72+
if local_only:
73+
it = (d for d in it if d.local)
74+
if not include_editables:
75+
it = (d for d in it if not d.editable)
76+
if editables_only:
77+
it = (d for d in it if d.editable)
78+
if user_only:
79+
it = (d for d in it if d.in_usersite)
80+
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)