From 0799ee51135cb36c9bfae22c5e17bacf28a5489a Mon Sep 17 00:00:00 2001 From: Ales Erjavec Date: Fri, 3 Nov 2023 15:02:35 +0100 Subject: [PATCH] discovery: Ensure back-compatibility Allow clients to pass pkg_resources.Distribution since it was part of public interface. --- orangecanvas/registry/discovery.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/orangecanvas/registry/discovery.py b/orangecanvas/registry/discovery.py index a04ca9d0..40885988 100644 --- a/orangecanvas/registry/discovery.py +++ b/orangecanvas/registry/discovery.py @@ -10,7 +10,6 @@ import abc import os import sys -import stat import logging import types import pkgutil @@ -27,7 +26,7 @@ from . import VERSION_HEX from . import cache, WidgetRegistry from . import utils -from ..utils.pkgmeta import entry_points +from ..utils.pkgmeta import entry_points, Distribution log = logging.getLogger(__name__) @@ -72,6 +71,13 @@ def default_category_for_module(module): ) +def _dist_name(dist: Distribution) -> str: + try: + return dist.name + except AttributeError: # pkg_resources.Distribution + return dist.project_name + + class WidgetDiscovery(object): """ Base widget discovery runner. @@ -213,7 +219,7 @@ def process_category_package(self, category, name=None, distribution=None): cat_desc.name = default_category_name_for_module(category) if distribution is not None: - cat_desc.project_name = distribution.project_name + cat_desc.project_name = _dist_name(distribution) self.handle_category(cat_desc) @@ -352,7 +358,7 @@ def widget_description(self, module, widget_name=None, desc.category = category_name if distribution is not None: - desc.project_name = distribution.project_name + desc.project_name = _dist_name(distribution) return desc @@ -372,7 +378,7 @@ def cache_insert(self, module, mtime, description, distribution=None, project_name = project_version = None if distribution is not None: - project_name = distribution.project_name + project_name = _dist_name(distribution) project_version = distribution.version exc_type = exc_val = None @@ -415,7 +421,7 @@ def cache_has_valid_entry(self, mod_path, distribution=None): return False if distribution is not None: - if entry.project_name != distribution.project_name or \ + if entry.project_name != _dist_name(distribution) or \ entry.project_version != distribution.version: return False