Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin system for validations #222

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions punx/plugin_validations/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from ._core import ValidationPluginBase
from .root import *
76 changes: 76 additions & 0 deletions punx/plugin_validations/_core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""
Python plugin loading system.

# from: https://gist.github.com/will-hart/5899567
# a simple Python plugin loading system
# see: http://stackoverflow.com/questions/14510286/plugin-architecture-plugin-manager-vs-inspecting-from-plugins-import
"""

# import imp
# import pathlib


# def install_user_plugin(plugin_file):
# """
# Install plugin(s) from a Python file.
# Potentially dangerous since this is an import of a user-created file.
# """
# plugin = pathlib.Path(plugin_file).absolute()
# if not plugin.exists():
# raise FileExistsError(plugin_file)

# module_name = plugin.stem

# fp, path, desc = imp.find_module(module_name, [str(plugin.parent)])
# imp.load_module(module_name, fp, path, desc)


class PluginMounter(type):
"""
Register and initiate all plugins subclassed from PluginBase.
Acts as a metaclass which creates anything inheriting from PluginBase.
A plugin mount point derived from:
http://martyalchin.com/2008/jan/10/simple-plugin-framework/
"""

def __init__(cls, name, bases, attrs):
"""Called when a PluginBase derived class is imported."""

if not hasattr(cls, 'plugins'):
# Called when the metaclass is first instantiated
cls.plugins = []
else:
# Called when a plugin class is imported
cls._enroll(cls)

def _enroll(cls, plugin):
"""Add the plugin to the plugin list and perform any registration logic"""

# create a plugin instance and store it
instance = plugin()

# save the plugin reference
cls.plugins.append(instance)

# apply plugin logic as defined by the plugin
instance.register()

def register(self):
raise NotImplementedError(
"MUST define 'register()' method in subclass."
)


class ValidationPluginBase(metaclass=PluginMounter):
"""A plugin which must provide a `register()` method."""

def __str__(self):
return f"Validator: {self.__class__.__name__}"

def action(self):
"""
The `action()` method describes the plugin's action(s).
"""
raise NotImplementedError(
"MUST define 'action()' method in subclass."
)
9 changes: 9 additions & 0 deletions punx/plugin_validations/root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from ._core import ValidationPluginBase


class FileRoot(ValidationPluginBase):
def register(self):
print(f"register: {__file__}::{self.__class__.__name__}")

def action(self):
print(f"action: {__file__}::{self.__class__.__name__}")