Plugin framework

- Registers some very simple barcode plugins
This commit is contained in:
Oliver Walters 2020-04-14 01:17:44 +10:00
parent 9ff5032020
commit b286a5e30c
6 changed files with 93 additions and 0 deletions

View File

View File

View File

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
class BarcodePlugin:
"""
The BarcodePlugin class is the base class for any barcode plugin.
"""
# Override this for each actual plugin
PLUGIN_NAME = ''
def validate_barcode(self, barcode_data):
"""
Default implementation returns False
"""
return False
def __init__(self):
pass

View File

@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
from . import barcode
class DigikeyBarcodePlugin(barcode.BarcodePlugin):
PLUGIN_NAME = "DigikeyBarcodePlugin"

View File

@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
from . import barcode
class InvenTreeBarcodePlugin(barcode.BarcodePlugin):
PLUGIN_NAME = "InvenTreeBarcodePlugin"
def validate_barcode(self, barcode_data):
print("testing")
return True

View File

@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
import inspect
import importlib
import pkgutil
# Barcode plugins
import plugins.barcode as barcode
from plugins.barcode.barcode import BarcodePlugin
def iter_namespace(pkg):
return pkgutil.iter_modules(pkg.__path__, pkg.__name__ + ".")
def get_modules(pkg):
# Return all modules in a given package
return [importlib.import_module(name) for finder, name, ispkg in iter_namespace(barcode)]
def get_classes(module):
# Return all classes in a given module
return inspect.getmembers(module, inspect.isclass)
def get_plugins(pkg, baseclass):
"""
Return a list of all modules under a given package.
- Modules must be a subclass of the provided 'baseclass'
- Modules must have a non-empty PLUGIN_NAME parameter
"""
plugins = []
modules = get_modules(pkg)
# Iterate through each module in the package
for mod in modules:
# Iterate through each class in the module
for item in get_classes(mod):
plugin = item[1]
if plugin.__class__ is type(baseclass) and plugin.PLUGIN_NAME:
plugins.append(plugin)
return plugins
def load_barcode_plugins():
return get_plugins(barcode, BarcodePlugin)