From b286a5e30cbf392ec79d955ac86c0cac12010fae Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 14 Apr 2020 01:17:44 +1000 Subject: [PATCH] Plugin framework - Registers some very simple barcode plugins --- InvenTree/plugins/__init__.py | 0 InvenTree/plugins/barcode/__init__.py | 0 InvenTree/plugins/barcode/barcode.py | 19 ++++++++++ InvenTree/plugins/barcode/digikey.py | 8 ++++ InvenTree/plugins/barcode/inventree.py | 14 +++++++ InvenTree/plugins/plugins.py | 52 ++++++++++++++++++++++++++ 6 files changed, 93 insertions(+) create mode 100644 InvenTree/plugins/__init__.py create mode 100644 InvenTree/plugins/barcode/__init__.py create mode 100644 InvenTree/plugins/barcode/barcode.py create mode 100644 InvenTree/plugins/barcode/digikey.py create mode 100644 InvenTree/plugins/barcode/inventree.py create mode 100644 InvenTree/plugins/plugins.py diff --git a/InvenTree/plugins/__init__.py b/InvenTree/plugins/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/InvenTree/plugins/barcode/__init__.py b/InvenTree/plugins/barcode/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/InvenTree/plugins/barcode/barcode.py b/InvenTree/plugins/barcode/barcode.py new file mode 100644 index 0000000000..a2d3c6652e --- /dev/null +++ b/InvenTree/plugins/barcode/barcode.py @@ -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 diff --git a/InvenTree/plugins/barcode/digikey.py b/InvenTree/plugins/barcode/digikey.py new file mode 100644 index 0000000000..2542fe964a --- /dev/null +++ b/InvenTree/plugins/barcode/digikey.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- + +from . import barcode + + +class DigikeyBarcodePlugin(barcode.BarcodePlugin): + + PLUGIN_NAME = "DigikeyBarcodePlugin" diff --git a/InvenTree/plugins/barcode/inventree.py b/InvenTree/plugins/barcode/inventree.py new file mode 100644 index 0000000000..f983487a41 --- /dev/null +++ b/InvenTree/plugins/barcode/inventree.py @@ -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 diff --git a/InvenTree/plugins/plugins.py b/InvenTree/plugins/plugins.py new file mode 100644 index 0000000000..75815aded2 --- /dev/null +++ b/InvenTree/plugins/plugins.py @@ -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)