Skip to content

Commit

Permalink
squash! Extend resource_files for entry-points
Browse files Browse the repository at this point in the history
Signed-off-by: Cristian Le <[email protected]>
  • Loading branch information
LecrisUT committed May 20, 2024
1 parent 5c79195 commit 29e25f4
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions tmt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7058,7 +7058,8 @@ def is_key_origin(node: fmf.Tree, key: str) -> bool:

def resource_files(
path: str,
package: Union[str, ModuleType] = "tmt"
package: Union[str, ModuleType] = "tmt",
logger: Optional[tmt.log.Logger] = None,
) -> importlib.abc.Traversable:
"""
Helper function to get path of package file or directory.
Expand All @@ -7075,6 +7076,7 @@ def resource_files(
:param path: file or directory path to retrieve, relative to the ``package``
or entry-point's root.
:param package: primary package in which to search for the file/directory.
:param logger: logger to report plugin import failures
:returns: a traversable path to the requested file or directory.
"""
def accumulate_path(paths: list[pathlib.Path],
Expand All @@ -7090,6 +7092,10 @@ def accumulate_path(paths: list[pathlib.Path],
# This should not happen
raise TypeError(f"Unexpected type improtlib.resources for package: {package}")

if not logger:
# Make sure there is a logger to report entry-point failures
logger = tmt.log.Logger.get_bootstrap_logger()

# Accumulate the base path of the package and entry-points
base_paths: list[pathlib.Path] = []

Expand All @@ -7101,12 +7107,13 @@ def accumulate_path(paths: list[pathlib.Path],

entry_point_group = entry_points().select(group=entry_point_name)

for found in entry_point_group:
ep_module = found.load()
with suppress(Exception):
# TODO: Log errors if entry-point was defined that failed to retrieve base path
for ep in entry_point_group:
try:
ep_module = ep.load()
ep_path = importlib.resources.files(ep_module)
accumulate_path(base_paths, ep_path)
except Exception:
logger.warn(f"Failed to load plugin resources: {ep}")

# Extract the Path if only one was found
if len(base_paths) == 1:
Expand Down

0 comments on commit 29e25f4

Please sign in to comment.