mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-30 20:55:42 +00:00 
			
		
		
		
	Plugin framework
- Registers some very simple barcode plugins
This commit is contained in:
		
							
								
								
									
										0
									
								
								InvenTree/plugins/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								InvenTree/plugins/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										0
									
								
								InvenTree/plugins/barcode/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								InvenTree/plugins/barcode/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										19
									
								
								InvenTree/plugins/barcode/barcode.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								InvenTree/plugins/barcode/barcode.py
									
									
									
									
									
										Normal 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 | ||||
							
								
								
									
										8
									
								
								InvenTree/plugins/barcode/digikey.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								InvenTree/plugins/barcode/digikey.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
| # -*- coding: utf-8 -*- | ||||
|  | ||||
| from . import barcode | ||||
|  | ||||
|  | ||||
| class DigikeyBarcodePlugin(barcode.BarcodePlugin): | ||||
|  | ||||
|     PLUGIN_NAME = "DigikeyBarcodePlugin" | ||||
							
								
								
									
										14
									
								
								InvenTree/plugins/barcode/inventree.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								InvenTree/plugins/barcode/inventree.py
									
									
									
									
									
										Normal 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 | ||||
							
								
								
									
										52
									
								
								InvenTree/plugins/plugins.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								InvenTree/plugins/plugins.py
									
									
									
									
									
										Normal 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) | ||||
		Reference in New Issue
	
	Block a user