Skip to content

Commit

Permalink
Some more yet unused implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
uranusjr committed Apr 23, 2020
1 parent 7c86fd6 commit d4f5bda
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
47 changes: 46 additions & 1 deletion src/pip/_internal/metadata/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,40 @@

from pip._vendor.six import add_metaclass

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

if MYPY_CHECK_RUNNING:
from typing import List, Optional
from typing import Container, Iterator, List, Optional


@add_metaclass(abc.ABCMeta)
class BaseDistribution(object):
@property
def canonical_name(self):
# type: () -> str
raise NotImplementedError()

@property
def installer(self):
# type: () -> str
raise NotImplementedError()

@property
def editable(self):
# type: () -> bool
raise NotImplementedError()

@property
def local(self):
# type: () -> bool
raise NotImplementedError()

@property
def in_usersite(self):
# type: () -> bool
raise NotImplementedError()


@add_metaclass(abc.ABCMeta)
class BaseEnvironment(object):
Expand All @@ -33,3 +54,27 @@ def from_paths(cls, paths):
def get_distribution(self, name):
# type: (str) -> Optional[BaseDistribution]
raise NotImplementedError()

def iter_distributions(self):
# type: (str) -> Iterator[BaseDistribution]
raise NotImplementedError()

def iter_installed_distributions(
self,
local_only=True, # type: bool
skip=stdlib_pkgs, # type: Container[str]
include_editables=True, # type: bool
editables_only=False, # type: bool
user_only=False, # type: bool
):
# type: (...) -> Iterator[BaseDistribution]
it = self.iter_distributions()
if local_only:
it = (d for d in it if d.local)
if not include_editables:
it = (d for d in it if not d.editable)
if editables_only:
it = (d for d in it if d.editable)
if user_only:
it = (d for d in it if d.in_usersite)
return (d for d in it if d.canonical_name not in skip)
34 changes: 32 additions & 2 deletions src/pip/_internal/metadata/pkg_resources.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
from pip._vendor import pkg_resources
from pip._vendor.packaging.utils import canonicalize_name

from pip._internal.utils.misc import (
dist_in_usersite,
dist_is_editable,
dist_is_local,
)
from pip._internal.utils.packaging import get_installer
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

from .base import BaseDistribution, BaseEnvironment

if MYPY_CHECK_RUNNING:
from typing import List, Optional
from typing import Iterator, List, Optional


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

@property
def canonical_name(self):
# type: () -> str
return canonicalize_name(self._dist.project_name)

@property
def installer(self):
# type: () -> str
# TODO: Move get_installer() implementation here.
return get_installer(self._dist)

@property
def editable(self):
# type: () -> bool
return dist_is_editable(self._dist)

@property
def local(self):
# type: () -> bool
return dist_is_local(self._dist)

@property
def in_usersite(self):
# type: () -> bool
return dist_in_usersite(self._dist)


class Environment(BaseEnvironment):
def __init__(self, ws):
Expand All @@ -44,3 +69,8 @@ def get_distribution(self, name):
except pkg_resources.DistributionNotFound:
return None
return Distribution(dist)

def iter_distributions(self):
# type: (str) -> Iterator[BaseDistribution]
for dist in self._ws:
yield Distribution(dist)

0 comments on commit d4f5bda

Please sign in to comment.