Package: pluginlib
OverviewPluginlib makes creating plugins for your project simple.FeaturesPlugins are validated when they are importedPlugins can be loaded through different mechanisms (modules, filesystem paths,entry points)Multipleversionsof the same plugin are supported (The newest one is used by default)Plugins can beblacklistedby type, name, or versionMultipleplugin groupsare supported so one program can use multiple sets of plugins that won’t conflictPlugins supportconditional loading(examples: os, version, installed software, etc)Once loaded, plugins can beaccessedthrough dictionary or dot notationInstallationPIP$pipinstallpluginlibFedora and EL (RHEL/CentOS/Rocky/Alma)(EPELrepositories must beconfiguredfor EL8)$dnfinstallpython3-pluginlibEL7 (RHEL/CentOS)(EPELrepositories must beconfigured)$yuminstallpython2-pluginlib$yuminstallpython36-pluginlibUsageStep 1: Define plugin parent classesAll plugins are subclasses of parent classes. To create a parent class, use the@Parentdecorator.The@Parentdecorator can take a plugin type for accessing child plugins
of the parent. If a plugin type isn’t given, the class name will be used.The@Parentdecorator can also take agroupkeyword which
restricts plugins to a specific plugin group.groupshould be specified if plugins for
different projects could be accessed in an single program, such as with libraries and frameworks.
For more information, see thePlugin Groupssection.Methods required in child plugins should be labeled as abstract methods.
Plugins without these methods or with parameters
that don’t match, will not be loaded.
For more information, see theAbstract Methodssection."""
sample.py
"""importpluginlib@pluginlib.Parent('parser')classParser(object):@pluginlib.abstractmethoddefparse(self,string):passStep 2: Define plugin classesTo create a plugin, subclass a parent class and include any required methods.Plugins can be customized through optional class attributes:_alias_Changes the name of the plugin which defaults to the class name._version_Sets the version of the plugin. Defaults to the module__version__orNoneIf multiple plugins with the same type and name are loaded, the plugin with
the highest version is used. For more information, see theVersionssection._skipload_Specifies the plugin should not be loaded. This is useful when a plugin is a parent class
for additional plugins or when a plugin should only be loaded under certain conditions.
For more information see theConditional Loadingsection."""
sample_plugins.py
"""importjsonimportsampleclassJSON(sample.Parser):_alias_='json'defparse(self,string):returnjson.loads(string)Step 3: Load pluginsPlugins are loaded when the module they are in is imported.PluginLoaderwill load modules from specified locations and provides access to them.PluginLoadercan load plugins from several locations.A program’s standard libraryEntry pointsA list of modulesA list of filesystem pathsPlugins can also be filtered through blacklists and type filters.
See theBlacklistsandType Filterssections for more information.Plugins are accessible through thePluginLoader.pluginsproperty,
a nested dictionary accessible through dot notation. For other ways to access plugins,
see theAccessing Pluginssection.importpluginlibimportsampleloader=pluginlib.PluginLoader(modules=['sample_plugins'])plugins=loader.pluginsparser=plugins.parser.json()print(parser.parse('{"json": "test"}'))