From ef58c20961072e4f60c26e94564180c304af7144 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Sun, 15 May 2022 23:11:21 -0500 Subject: replace pkg_resources uses with importlib_metadata --- src/paste/deploy/loadwsgi.py | 54 +++++++++++++++----------------------------- src/paste/deploy/util.py | 6 ----- 2 files changed, 18 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/paste/deploy/loadwsgi.py b/src/paste/deploy/loadwsgi.py index 482b339..bde8a08 100644 --- a/src/paste/deploy/loadwsgi.py +++ b/src/paste/deploy/loadwsgi.py @@ -6,9 +6,7 @@ import re import sys from urllib.parse import unquote -import pkg_resources - -from paste.deploy.util import fix_call, lookup_object +from paste.deploy.util import fix_call, importlib_metadata, lookup_object __all__ = ['loadapp', 'loadserver', 'loadfilter', 'appconfig'] @@ -18,14 +16,10 @@ __all__ = ['loadapp', 'loadserver', 'loadfilter', 'appconfig'] ############################################################ -def import_string(s): - ep = pkg_resources.EntryPoint.parse("x=" + s) - if hasattr(ep, 'resolve'): - # this is available on setuptools >= 10.2 - return ep.resolve() - else: - # this causes a DeprecationWarning on setuptools >= 11.3 - return ep.load(False) +def find_entry_point(dist, group, name): + for entry in dist.entry_points: + if entry.name == name and entry.group == group: + return entry def _aslist(obj): @@ -443,7 +437,7 @@ class ConfigLoader(_Loader): filter_with = None if 'require' in local_conf: for spec in local_conf['require'].split(): - pkg_resources.require(spec) + importlib_metadata.distribution(spec) del local_conf['require'] if section.startswith('filter-app:'): context = self._filter_app_context( @@ -532,7 +526,9 @@ class ConfigLoader(_Loader): raise LookupError("No loader given in section %r" % section) found_protocol, found_expr = possible[0] del local_conf[found_protocol] - value = import_string(found_expr) + value = importlib_metadata.EntryPoint( + name=None, group=None, value=found_expr + ).load() context = LoaderContext( value, object_type, found_protocol, global_conf, local_conf, self ) @@ -649,51 +645,37 @@ class EggLoader(_Loader): global_conf or {}, {}, self, - distribution=pkg_resources.get_distribution(self.spec), + distribution=importlib_metadata.distribution(self.spec), entry_point_name=ep_name, ) def find_egg_entry_point(self, object_type, name=None): """ - Returns the (entry_point, protocol) for the with the given - ``name``. + Returns the (entry_point, protocol) for with the given ``name``. """ if name is None: name = 'main' + dist = importlib_metadata.distribution(self.spec) possible = [] for protocol_options in object_type.egg_protocols: for protocol in protocol_options: - pkg_resources.require(self.spec) - entry = pkg_resources.get_entry_info(self.spec, protocol, name) + entry = find_entry_point(dist, protocol, name) if entry is not None: possible.append((entry.load(), protocol, entry.name)) break if not possible: # Better exception - dist = pkg_resources.get_distribution(self.spec) raise LookupError( - "Entry point %r not found in egg %r (dir: %s; protocols: %s; " - "entry_points: %s)" + "Entry point %r not found in egg %r (protocols: %s; entry_points: %s)" % ( name, self.spec, - dist.location, ', '.join(_flatten(object_type.egg_protocols)), ', '.join( - _flatten( - [ - list( - ( - pkg_resources.get_entry_info( - self.spec, prot, name - ) - or {} - ).keys() - ) - for prot in protocol_options - ] - or '(no entry points)' - ) + str(entry) + for prot in protocol_options + for entry in [find_entry_point(dist, prot, name)] + if entry ), ) ) diff --git a/src/paste/deploy/util.py b/src/paste/deploy/util.py index 1e9c08f..f8ce2aa 100644 --- a/src/paste/deploy/util.py +++ b/src/paste/deploy/util.py @@ -10,12 +10,6 @@ except ImportError: import importlib_metadata -def find_entry_point(dist, group, name): - for entry in dist.entry_points: - if entry.name == name and entry.group == group: - return entry - - def fix_type_error(exc_info, callable, varargs, kwargs): """ Given an exception, this will test if the exception was due to a -- cgit v1.2.1