Skip to content

egse.plugin

cgse-common

This code is part of the cgse-common package.

egse.plugin

This module provides function to load plugins and settings from entry-points.

Classes:

Name Description
HierarchicalEntryPoints

Functions:

Name Description
entry_points

Returns a set with all entry-points for the given group name.

get_file_infos

Returns a dictionary with location and filename of all the entries found for

load_plugins_ep

Returns a dictionary with plugins loaded. The keys are the names of the entry-points,

load_plugins_fn

Returns a dictionary with plugins loaded for the filenames that match the given pattern.

HierarchicalEntryPoints

HierarchicalEntryPoints(base_group)

Methods:

Name Description
get_all_entry_points

Get all entry points as a flat list.

get_all_groups

Get all discovered group names.

get_by_subgroup

Get entry points from a specific subgroup.

get_by_type

Get entry points by type (assuming type is the subgroup name).

get_all_entry_points

get_all_entry_points()

Get all entry points as a flat list.

get_all_groups

get_all_groups()

Get all discovered group names.

get_by_subgroup

get_by_subgroup(subgroup=None)

Get entry points from a specific subgroup.

get_by_type

get_by_type(entry_type)

Get entry points by type (assuming type is the subgroup name).

entry_points

entry_points(group)

Returns a set with all entry-points for the given group name.

When the name is not known as an entry-point group, an empty set will be returned.

get_file_infos

get_file_infos(entry_point)

Returns a dictionary with location and filename of all the entries found for the given entry-point name.

The entry-points are interpreted as follows: <name> = "<module>:<filename>" where

  • <name> is the name of the entry-point given in the pyproject.toml file
  • <module> is a valid module name that can be imported and from which the location can be determined.
  • <filename> is the name of the target file, e.g. a YAML file

As an example, for the cgse-common settings, the following entry in the pyproject.toml:

[project.entry-points."cgse.settings"]
cgse-common = "cgse_common:settings.yaml"

Note that the module name for this entry point has an underscore instead of a dash.

Return

A dictionary with the entry point name as the key and a tuple (location, filename) as the value.

load_plugins_ep

load_plugins_ep(entry_point)

Returns a dictionary with plugins loaded. The keys are the names of the entry-points, the values are the loaded modules or objects.

Note

When an entry point cannot be loaded, an error is logged and the value for that entry point in the returned dictionary will be None.

load_plugins_fn cached

load_plugins_fn(pattern, package_name=None)

Returns a dictionary with plugins loaded for the filenames that match the given pattern. The keys are the names of the modules, the values are the loaded modules.

If no package_name is provided, the pattern is relative to the location of this module (which is in the top-level module egse).

If the pattern results in two or more modules with the same name, a warning will be logged and only the last imported module will be returned in the dictionary.

Plugins are usually located in the egse.plugins module. Remember that egse.plugins is a namespace and external packages can also deliver plugin modules in that location.

Note

When a plugin cannot be loaded, an error is logged.

This function uses an LRU cache to avoid reloading modules. If you need to reload, use load_plugins_fn.cache_clear() to reset the cache.

# Loading the InfluxDB plugin for time series metrics
>>> influxdb = load_plugins_fn("influxdb.py", "egse.plugins.metrics")

# Loading the HDF5 storage plugin for PLATO
>>> hdf5 = load_plugins_fn("hdf5.py", "egse.plugins.storage")

# Loading all plugins
>>> x = load_plugins_fn("**/*.py", "egse.plugins")