more docstrings!

This commit is contained in:
Matthias 2021-09-25 01:22:48 +02:00
parent bbbf9be883
commit 1735514364
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076
8 changed files with 35 additions and 4 deletions

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""Class for ActionPlugin"""
import logging import logging

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""sample implementation for ActionPlugin"""
from plugins.action.action import ActionPlugin from plugins.action.action import ActionPlugin

View File

@ -1,3 +1,4 @@
"""sample implementation for IntegrationPlugin"""
from plugins.integration.integration import IntegrationPlugin, UrlsMixin from plugins.integration.integration import IntegrationPlugin, UrlsMixin

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""class for IntegrationPlugin and Mixins for it"""
import logging import logging
import os import os
@ -18,12 +19,14 @@ class MixinBase:
"""general base for mixins""" """general base for mixins"""
def add_mixin(self, key: str, fnc_enabled=True, cls=None): def add_mixin(self, key: str, fnc_enabled=True, cls=None):
"""add a mixin to the plugins registry"""
if not hasattr(self, '_mixins'): if not hasattr(self, '_mixins'):
self._mixins = {} self._mixins = {}
self._mixins[key] = fnc_enabled self._mixins[key] = fnc_enabled
self.setup_mixin(key, cls=cls) self.setup_mixin(key, cls=cls)
def setup_mixin(self, key, cls=None): def setup_mixin(self, key, cls=None):
"""define mixin details for the current mixin -> provides meta details for all active mixins"""
if not hasattr(self, '_mixinreg'): if not hasattr(self, '_mixinreg'):
self._mixinreg = {} self._mixinreg = {}
@ -38,6 +41,7 @@ class MixinBase:
@property @property
def registered_mixins(self, with_base: bool = False): def registered_mixins(self, with_base: bool = False):
"""get all registered mixins for the plugin"""
mixins = getattr(self, '_mixinreg', None) mixins = getattr(self, '_mixinreg', None)
if mixins: if mixins:
# filter out base # filter out base
@ -52,6 +56,7 @@ class MixinBase:
class SettingsMixin: class SettingsMixin:
"""Mixin that enables settings for the plugin""" """Mixin that enables settings for the plugin"""
class Meta: class Meta:
"""meta options for this mixin"""
MIXIN_NAME = 'Settings' MIXIN_NAME = 'Settings'
def __init__(self): def __init__(self):
@ -74,6 +79,9 @@ class SettingsMixin:
@property @property
def settingspatterns(self): def settingspatterns(self):
"""
get patterns for InvenTreeSetting defintion
"""
if self.has_settings: if self.has_settings:
return {f'PLUGIN_{self.plugin_name().upper()}_{key}': value for key, value in self.settings.items()} return {f'PLUGIN_{self.plugin_name().upper()}_{key}': value for key, value in self.settings.items()}
return None return None
@ -82,6 +90,7 @@ class SettingsMixin:
class UrlsMixin: class UrlsMixin:
"""Mixin that enables urls for the plugin""" """Mixin that enables urls for the plugin"""
class Meta: class Meta:
"""meta options for this mixin"""
MIXIN_NAME = 'URLs' MIXIN_NAME = 'URLs'
def __init__(self): def __init__(self):
@ -97,12 +106,15 @@ class UrlsMixin:
@property @property
def base_url(self): def base_url(self):
"""
returns base url for this plugin
"""
return f'{settings.PLUGIN_URL}/{self.plugin_name()}/' return f'{settings.PLUGIN_URL}/{self.plugin_name()}/'
@property @property
def urlpatterns(self): def urlpatterns(self):
""" """
retruns the urlpatterns for this plugin returns the urlpatterns for this plugin
""" """
if self.has_urls: if self.has_urls:
return url(f'^{self.plugin_name()}/', include((self.urls, self.plugin_name())), name=self.plugin_name()) return url(f'^{self.plugin_name()}/', include((self.urls, self.plugin_name())), name=self.plugin_name())
@ -119,6 +131,7 @@ class UrlsMixin:
class NavigationMixin: class NavigationMixin:
"""Mixin that enables adding navigation links with the plugin""" """Mixin that enables adding navigation links with the plugin"""
class Meta: class Meta:
"""meta options for this mixin"""
MIXIN_NAME = 'Navigation Links' MIXIN_NAME = 'Navigation Links'
def __init__(self): def __init__(self):
@ -148,6 +161,7 @@ class NavigationMixin:
def get_git_log(path): def get_git_log(path):
"""get dict with info of the last commit to file named in path"""
path = path.replace(os.path.dirname(settings.BASE_DIR), '')[1:] path = path.replace(os.path.dirname(settings.BASE_DIR), '')[1:]
command = ['git', 'log', '-n', '1', "--pretty=format:'%H%n%aN%n%aE%n%aI%n%f%n%G?%n%GK'", '--follow', '--', path] command = ['git', 'log', '-n', '1', "--pretty=format:'%H%n%aN%n%aE%n%aI%n%f%n%G?%n%GK'", '--follow', '--', path]
try: try:
@ -158,7 +172,9 @@ def get_git_log(path):
class GitStatus: class GitStatus:
"""class for resolving git gpg singing state"""
class Definition: class Definition:
"""definition of a git gpg sing state"""
key: str = 'N' key: str = 'N'
status: int = 2 status: int = 2
msg: str = '' msg: str = ''
@ -189,19 +205,23 @@ class IntegrationPlugin(MixinBase, plugin.InvenTreePlugin):
self.set_sign_values() self.set_sign_values()
def mixin(self, key): def mixin(self, key):
"""check if mixin is registered"""
return key in self._mixins return key in self._mixins
def mixin_enabled(self, key): def mixin_enabled(self, key):
"""check if mixin is enabled and ready"""
if self.mixin(key): if self.mixin(key):
fnc_name = self._mixins.get(key) fnc_name = self._mixins.get(key)
return getattr(self, fnc_name, True) return getattr(self, fnc_name, True)
return False return False
def get_plugin_commit(self): def get_plugin_commit(self):
"""get last git commit for plugin"""
path = inspect.getfile(self.__class__) path = inspect.getfile(self.__class__)
return get_git_log(path) return get_git_log(path)
def set_sign_values(self): def set_sign_values(self):
"""add the last commit of the plugins class file into plugins context"""
# fetch git log # fetch git log
commit = self.get_plugin_commit() commit = self.get_plugin_commit()
# resolve state # resolve state

View File

@ -1,3 +1,4 @@
"""sample implementations for IntegrationPlugin"""
from plugins.integration.integration import SettingsMixin, UrlsMixin, NavigationMixin, IntegrationPlugin from plugins.integration.integration import SettingsMixin, UrlsMixin, NavigationMixin, IntegrationPlugin
from django.http import HttpResponse from django.http import HttpResponse
@ -13,6 +14,7 @@ class SampleIntegrationPlugin(SettingsMixin, UrlsMixin, NavigationMixin, Integra
PLUGIN_NAME = "SampleIntegrationPlugin" PLUGIN_NAME = "SampleIntegrationPlugin"
def view_test(self, request): def view_test(self, request):
"""very basic view"""
return HttpResponse(f'Hi there {request.user.username} this works') return HttpResponse(f'Hi there {request.user.username} this works')
def setup_urls(self): def setup_urls(self):

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""Base Class for InvenTree plugins"""
class InvenTreePlugin(): class InvenTreePlugin():
@ -10,6 +11,7 @@ class InvenTreePlugin():
PLUGIN_NAME = '' PLUGIN_NAME = ''
def plugin_name(self): def plugin_name(self):
"""get plugin name"""
return self.PLUGIN_NAME return self.PLUGIN_NAME
def __init__(self): def __init__(self):

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""general functions for plugin handeling"""
import inspect import inspect
import importlib import importlib
@ -17,17 +18,17 @@ logger = logging.getLogger("inventree")
def iter_namespace(pkg): def iter_namespace(pkg):
"""get all modules in a package"""
return pkgutil.iter_modules(pkg.__path__, pkg.__name__ + ".") return pkgutil.iter_modules(pkg.__path__, pkg.__name__ + ".")
def get_modules(pkg): def get_modules(pkg):
# Return all modules in a given package """get all modules in a package"""
return [importlib.import_module(name) for finder, name, ispkg in iter_namespace(pkg)] return [importlib.import_module(name) for finder, name, ispkg in iter_namespace(pkg)]
def get_classes(module): def get_classes(module):
# Return all classes in a given module """get all classes in a given module"""
return inspect.getmembers(module, inspect.isclass) return inspect.getmembers(module, inspect.isclass)

View File

@ -10,15 +10,18 @@ class InvenTreePluginTests(TestCase):
self.plugin = plugins.plugin.InvenTreePlugin() self.plugin = plugins.plugin.InvenTreePlugin()
class NamedPlugin(plugins.plugin.InvenTreePlugin): class NamedPlugin(plugins.plugin.InvenTreePlugin):
"""a named plugin"""
PLUGIN_NAME = 'abc123' PLUGIN_NAME = 'abc123'
self.named_plugin = NamedPlugin() self.named_plugin = NamedPlugin()
def test_basic_plugin_init(self): def test_basic_plugin_init(self):
"""check if a basic plugin intis"""
self.assertEqual(self.plugin.PLUGIN_NAME, '') self.assertEqual(self.plugin.PLUGIN_NAME, '')
self.assertEqual(self.plugin.plugin_name(), '') self.assertEqual(self.plugin.plugin_name(), '')
def test_basic_plugin_name(self): def test_basic_plugin_name(self):
"""check if the name of a basic plugin can be set"""
self.assertEqual(self.named_plugin.PLUGIN_NAME, 'abc123') self.assertEqual(self.named_plugin.PLUGIN_NAME, 'abc123')
self.assertEqual(self.named_plugin.plugin_name(), 'abc123') self.assertEqual(self.named_plugin.plugin_name(), 'abc123')