Skip to content

Commit

Permalink
replace pkg_resources with importlib.metadata in pshell script
Browse files Browse the repository at this point in the history
  • Loading branch information
mmerickel committed Feb 5, 2024
1 parent 510dd7a commit 302b853
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/pyramid/scripts/pshell.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import argparse
from code import interact
from contextlib import contextmanager
import importlib.metadata
import os
import pkg_resources
import sys
import textwrap

Expand Down Expand Up @@ -41,7 +41,7 @@ class PShellCommand:
script_name = 'pshell'
bootstrap = staticmethod(bootstrap) # for testing
get_config_loader = staticmethod(get_config_loader) # for testing
pkg_resources = pkg_resources # for testing
importlib_metadata = importlib.metadata # for testing

parser = argparse.ArgumentParser(
description=textwrap.dedent(description),
Expand Down Expand Up @@ -228,10 +228,16 @@ def show_shells(self):
return 0

def find_all_shells(self):
pkg_resources = self.pkg_resources
importlib_metadata = self.importlib_metadata

shells = {}
for ep in pkg_resources.iter_entry_points('pyramid.pshell_runner'):
eps = importlib_metadata.entry_points()
if hasattr(eps, 'select'):
eps = eps.select(group='pyramid.pshell_runner')
else: # pragma: no cover
# fallback for py38 and py39
eps = eps.get('pyramid.pshell_runner')
for ep in eps:
name = ep.name
shell_factory = ep.load()
shells[name] = shell_factory
Expand Down
38 changes: 37 additions & 1 deletion tests/test_scripts/test_pshell.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import unittest

from . import dummy
Expand Down Expand Up @@ -50,7 +51,10 @@ class Options:
return cmd

def _makeEntryPoints(self, command, shells):
command.pkg_resources = dummy.DummyPkgResources(shells)
entry_points = [
DummyEntryPoint(name, value) for name, value in shells.items()
]
command.importlib_metadata = DummyImportlibMetadata(entry_points)

def test_command_loads_default_shell(self):
command = self._makeOne()
Expand Down Expand Up @@ -430,3 +434,35 @@ def _callFUT(self, argv):
def test_it(self):
result = self._callFUT(['pshell'])
self.assertEqual(result, 2)


class DummyImportlibMetadata:
def __init__(self, entry_points):
self._entry_points = entry_points

def entry_points(self):
return DummyEntryPoints(self._entry_points)


class DummyEntryPoints:
def __init__(self, entry_points):
self._entry_points = entry_points

if sys.version_info >= (3, 10):

def select(self, **kw):
return iter(self._entry_points)

else: # pragma no cover

def get(self, key):
return list(self._entry_points)


class DummyEntryPoint:
def __init__(self, name, value):
self.name = name
self.value = value

def load(self):
return self.value

0 comments on commit 302b853

Please sign in to comment.