Skip to content

Commit

Permalink
Entry points are non filtrable in 3.8-
Browse files Browse the repository at this point in the history
  • Loading branch information
Hook25 committed Jan 22, 2025
1 parent 54fecd6 commit b649575
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 34 deletions.
23 changes: 16 additions & 7 deletions checkbox-ng/plainbox/impl/secure/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,28 @@
testing in isolation from whatever entry points may exist in the system.
"""

import os
import abc
import time
import logging
import collections
import contextlib
import logging
import os
import time
from contextlib import suppress

from plainbox.i18n import gettext as _

try:
from importlib import metadata
from importlib.metadata import entry_points
except ImportError:
import importlib_metadata as metadata
from importlib_metadata import entry_points

Check warning on line 56 in checkbox-ng/plainbox/impl/secure/plugins.py

View check run for this annotation

Codecov / codecov/patch

checkbox-ng/plainbox/impl/secure/plugins.py#L55-L56

Added lines #L55 - L56 were not covered by tests

from plainbox.i18n import gettext as _

def get_entry_points(**kwargs):
with suppress(TypeError):
return entry_points(**kwargs)
import pkg_resources

return pkg_resources.iter_entry_points(**kwargs)


logger = logging.getLogger("plainbox.secure.plugins")
Expand Down Expand Up @@ -540,7 +549,7 @@ def _get_entry_points(self):
This is the method you want to mock if you are writing unit tests
"""

return metadata.entry_points(group=self._namespace)
return get_entry_points(group=self._namespace)


class FsPlugInCollection(PlugInCollectionBase):
Expand Down
16 changes: 8 additions & 8 deletions checkbox-ng/plainbox/impl/secure/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ def test_default_wrapper(self):
# Ensure that the wrapper is :class:`PlugIn`
self.assertEqual(self.col._wrapper, PlugIn)

@mock.patch("plainbox.impl.secure.plugins.metadata")
def test_load(self, mock_metadata):
@mock.patch("plainbox.impl.secure.plugins.get_entry_points")
def test_load(self, mock_get_entry_points):
# Create a mocked entry point
mock_ep1 = mock.Mock()
mock_ep1.name = "zzz"
Expand All @@ -356,18 +356,18 @@ def test_load(self, mock_metadata):
mock_ep2.name = "aaa"
mock_ep2.load.return_value = "one"
# Make the collection load both mocked entry points
mock_metadata.entry_points.return_value = [mock_ep1, mock_ep2]
mock_get_entry_points.return_value = [mock_ep1, mock_ep2]
# Load plugins
self.col.load()
# Ensure that pkg_resources were interrogated
mock_metadata.entry_points.assert_called_with(group=self._NAMESPACE)
mock_get_entry_points.assert_called_with(group=self._NAMESPACE)
# Ensure that both entry points were loaded
mock_ep1.load.assert_called_with()
mock_ep2.load.assert_called_with()

@mock.patch("plainbox.impl.secure.plugins.logger")
@mock.patch("plainbox.impl.secure.plugins.metadata")
def test_load_failing(self, mock_metadata, mock_logger):
@mock.patch("plainbox.impl.secure.plugins.get_entry_points")
def test_load_failing(self, mock_get_entry_points, mock_logger):
# Create a mocked entry point
mock_ep1 = mock.Mock()
mock_ep1.name = "zzz"
Expand All @@ -377,11 +377,11 @@ def test_load_failing(self, mock_metadata, mock_logger):
mock_ep2.name = "aaa"
mock_ep2.load.side_effect = ImportError("boom")
# Make the collection load both mocked entry points
mock_metadata.entry_points.return_value = [mock_ep1, mock_ep2]
mock_get_entry_points.return_value = [mock_ep1, mock_ep2]
# Load plugins
self.col.load()
# Ensure that pkg_resources were interrogated
mock_metadata.entry_points.assert_called_with(group=self._NAMESPACE)
mock_get_entry_points.assert_called_with(group=self._NAMESPACE)
# Ensure that both entry points were loaded
mock_ep1.load.assert_called_with()
mock_ep2.load.assert_called_with()
Expand Down
23 changes: 16 additions & 7 deletions checkbox-ng/plainbox/impl/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,31 @@
"""

import sys
import requests

from io import TextIOWrapper
from logging import getLogger
from shutil import copyfileobj
from contextlib import suppress
from collections import OrderedDict

try:
from importlib import metadata
except ImportError:
import importlib_metadata as metadata

from plainbox.abc import ISessionStateTransport
from plainbox.i18n import gettext as _
from plainbox.impl.exporter import ByteStringStreamTranslator

import requests
try:
from importlib.metadata import entry_points
except ImportError:
from importlib_metadata import entry_points

Check warning on line 47 in checkbox-ng/plainbox/impl/transport.py

View check run for this annotation

Codecov / codecov/patch

checkbox-ng/plainbox/impl/transport.py#L46-L47

Added lines #L46 - L47 were not covered by tests


def get_entry_points(**kwargs):
with suppress(TypeError):
return entry_points(**kwargs)
import pkg_resources

return pkg_resources.iter_entry_points(**kwargs)


# OAuth is not always available on all platforms.
_oauth_available = True
Expand Down Expand Up @@ -258,7 +267,7 @@ def get_all_transports():
Returns a map of transports (mapping from name to transport class)
"""
transport_map = OrderedDict()
iterator = metadata.entry_points(group="plainbox.transport")
iterator = get_entry_points(group="plainbox.transport")
for entry_point in sorted(iterator, key=lambda ep: ep.name):
try:
transport_cls = entry_point.load()
Expand Down
35 changes: 23 additions & 12 deletions checkbox-ng/plainbox/impl/unit/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,35 @@
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.

"""Exporter Entry Unit."""
import re
import json
import logging
import os.path
import re

try:
from importlib import metadata
except ImportError:
import importlib_metadata as metadata
from contextlib import suppress

from plainbox.i18n import gettext as _
from plainbox.impl.symbol import SymbolDef
from plainbox.impl.unit import concrete_validators
from plainbox.impl.unit.unit_with_id import UnitWithId
from plainbox.impl.unit.validators import CorrectFieldValueValidator
from plainbox.impl.unit.validators import PresentFieldValidator
from plainbox.impl.validation import Problem
from plainbox.impl.validation import Severity
from plainbox.impl.unit.validators import (
CorrectFieldValueValidator,
PresentFieldValidator,
)
from plainbox.impl.validation import Problem, Severity

try:
from importlib.metadata import entry_points
except ImportError:
from importlib_metadata import entry_points

Check warning on line 40 in checkbox-ng/plainbox/impl/unit/exporter.py

View check run for this annotation

Codecov / codecov/patch

checkbox-ng/plainbox/impl/unit/exporter.py#L39-L40

Added lines #L39 - L40 were not covered by tests


def get_entry_points(**kwargs):
with suppress(TypeError):
return entry_points(**kwargs)
import pkg_resources

return pkg_resources.iter_entry_points(**kwargs)


logger = logging.getLogger("plainbox.unit.exporter")

Expand Down Expand Up @@ -127,7 +138,7 @@ class fields(SymbolDef):
CorrectFieldValueValidator(
lambda entry_point: next(
iter(
metadata.entry_points(
get_entry_points(
group="plainbox.exporter", name=entry_point
)
)
Expand Down Expand Up @@ -227,7 +238,7 @@ def _get_exporter_cls(self, exporter):
"""Return the exporter class."""
return next(
iter(
metadata.entry_points(
get_entry_points(
group="plainbox.exporter", name=exporter.entry_point
)
)
Expand Down

0 comments on commit b649575

Please sign in to comment.