Skip to content

Commit

Permalink
feat: Add get providers from entry points
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci[bot] authored and loonghao committed Apr 18, 2024
1 parent 604dbcf commit 5228ee4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
27 changes: 26 additions & 1 deletion notifiers/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from abc import ABC
from abc import abstractmethod
from importlib.metadata import entry_points
import importlib.machinery
import importlib.util

import jsonschema
import requests
Expand Down Expand Up @@ -349,6 +351,29 @@ def get_notifier(provider_name: str, strict: bool = False) -> Provider:
raise NoSuchNotifierError(name=provider_name)


def load_provider_from_points(entry_points: str):
"""Load a Provider class from a given entry point string.
This function takes an entry point string in the format
'module_path:class_name', imports the module dynamically using
importlib, and returns the specified class from the module.
:param entry_points: A string specifying the module path and class name, separated by a colon.
:return: :class:`Provider` or None
:raises ImportError: If the specified module cannot be imported.
:raises AttributeError: If the specified class cannot be found in the module.
Example:
>>> entry_points = "xxx.provider:Provider"
>>> plugin_class = load_provider_from_points(entry_points)
>>> plugin_instance = plugin_class()
"""
instance_path, instance = entry_points.split(":")
module = importlib.import_module(instance_path)
provider = getattr(module, instance)
return provider


def get_providers_from_entry_points(group_name: str = "notifiers") -> dict:
"""
Get a dictionary of plugins from the entry points based on the given group name.
Expand All @@ -368,7 +393,7 @@ def get_providers_from_entry_points(group_name: str = "notifiers") -> dict:
points = entry_points()
for item in points.get(group_name, []):
if item.group == group_name:
result[item.name] = item.value
result[item.name] = load_provider_from_points(item.value)
return result


Expand Down
2 changes: 2 additions & 0 deletions source/api/core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Core API reference

.. autofunction:: notifiers.core.get_all_providers

.. autofunction:: notifiers.core.load_provider_from_points

.. autofunction:: notifiers.core.get_providers_from_entry_points

.. autofunction:: notifiers.core.notify
Expand Down
2 changes: 1 addition & 1 deletion source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = 'en'
language = "en"

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
Expand Down

0 comments on commit 5228ee4

Please sign in to comment.