-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add metadata package to resolve distribution information (#6)
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import inspect | ||
from functools import lru_cache | ||
from importlib import metadata | ||
from typing import Mapping, Optional | ||
|
||
from .core import PluginSpec | ||
|
||
|
||
@lru_cache() | ||
def packages_distributions() -> Mapping[str, list[str]]: | ||
""" | ||
Cache wrapper around metadata.packages_distributions, which returns a mapping of top-level packages to | ||
their distributions. | ||
:return: package to distribution mapping | ||
""" | ||
return metadata.packages_distributions() | ||
|
||
|
||
def resolve_distribution_information(plugin_spec: PluginSpec) -> Optional[metadata.Distribution]: | ||
""" | ||
Resolves for a PluginSpec the python distribution package it comes from. Currently, this raises an | ||
error for plugins that come from a namespace package (i.e., when a package is part of multiple | ||
distributions). | ||
:param plugin_spec: the plugin spec to resolve | ||
:return: the Distribution metadata if it exists | ||
""" | ||
package = inspect.getmodule(plugin_spec.factory).__name__ | ||
root_package = package.split(".")[0] | ||
distributions = packages_distributions().get(root_package) | ||
if not distributions: | ||
return None | ||
if len(distributions) > 1: | ||
raise ValueError("cannot deal with plugins that are part of namespace packages") | ||
|
||
return metadata.distribution(distributions[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from plugin import PluginSpec | ||
from plugin.metadata import resolve_distribution_information | ||
|
||
|
||
def test_resolve_distribution_information(): | ||
import pytest | ||
|
||
# fake a plugin spec and use pytest as test object | ||
fake_plugin_spec = PluginSpec("foo", "bar", pytest.fixture) | ||
dist = resolve_distribution_information(fake_plugin_spec) | ||
assert dist.name == "pytest" | ||
assert dist.metadata["License"] == "MIT" |