mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
more docstrings!
This commit is contained in:
parent
bbbf9be883
commit
1735514364
@ -1,4 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Class for ActionPlugin"""
|
||||
|
||||
import logging
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""sample implementation for ActionPlugin"""
|
||||
from plugins.action.action import ActionPlugin
|
||||
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
"""sample implementation for IntegrationPlugin"""
|
||||
from plugins.integration.integration import IntegrationPlugin, UrlsMixin
|
||||
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""class for IntegrationPlugin and Mixins for it"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
@ -18,12 +19,14 @@ class MixinBase:
|
||||
"""general base for mixins"""
|
||||
|
||||
def add_mixin(self, key: str, fnc_enabled=True, cls=None):
|
||||
"""add a mixin to the plugins registry"""
|
||||
if not hasattr(self, '_mixins'):
|
||||
self._mixins = {}
|
||||
self._mixins[key] = fnc_enabled
|
||||
self.setup_mixin(key, cls=cls)
|
||||
|
||||
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'):
|
||||
self._mixinreg = {}
|
||||
|
||||
@ -38,6 +41,7 @@ class MixinBase:
|
||||
|
||||
@property
|
||||
def registered_mixins(self, with_base: bool = False):
|
||||
"""get all registered mixins for the plugin"""
|
||||
mixins = getattr(self, '_mixinreg', None)
|
||||
if mixins:
|
||||
# filter out base
|
||||
@ -52,6 +56,7 @@ class MixinBase:
|
||||
class SettingsMixin:
|
||||
"""Mixin that enables settings for the plugin"""
|
||||
class Meta:
|
||||
"""meta options for this mixin"""
|
||||
MIXIN_NAME = 'Settings'
|
||||
|
||||
def __init__(self):
|
||||
@ -74,6 +79,9 @@ class SettingsMixin:
|
||||
|
||||
@property
|
||||
def settingspatterns(self):
|
||||
"""
|
||||
get patterns for InvenTreeSetting defintion
|
||||
"""
|
||||
if self.has_settings:
|
||||
return {f'PLUGIN_{self.plugin_name().upper()}_{key}': value for key, value in self.settings.items()}
|
||||
return None
|
||||
@ -82,6 +90,7 @@ class SettingsMixin:
|
||||
class UrlsMixin:
|
||||
"""Mixin that enables urls for the plugin"""
|
||||
class Meta:
|
||||
"""meta options for this mixin"""
|
||||
MIXIN_NAME = 'URLs'
|
||||
|
||||
def __init__(self):
|
||||
@ -97,12 +106,15 @@ class UrlsMixin:
|
||||
|
||||
@property
|
||||
def base_url(self):
|
||||
"""
|
||||
returns base url for this plugin
|
||||
"""
|
||||
return f'{settings.PLUGIN_URL}/{self.plugin_name()}/'
|
||||
|
||||
@property
|
||||
def urlpatterns(self):
|
||||
"""
|
||||
retruns the urlpatterns for this plugin
|
||||
returns the urlpatterns for this plugin
|
||||
"""
|
||||
if self.has_urls:
|
||||
return url(f'^{self.plugin_name()}/', include((self.urls, self.plugin_name())), name=self.plugin_name())
|
||||
@ -119,6 +131,7 @@ class UrlsMixin:
|
||||
class NavigationMixin:
|
||||
"""Mixin that enables adding navigation links with the plugin"""
|
||||
class Meta:
|
||||
"""meta options for this mixin"""
|
||||
MIXIN_NAME = 'Navigation Links'
|
||||
|
||||
def __init__(self):
|
||||
@ -148,6 +161,7 @@ class NavigationMixin:
|
||||
|
||||
|
||||
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:]
|
||||
command = ['git', 'log', '-n', '1', "--pretty=format:'%H%n%aN%n%aE%n%aI%n%f%n%G?%n%GK'", '--follow', '--', path]
|
||||
try:
|
||||
@ -158,7 +172,9 @@ def get_git_log(path):
|
||||
|
||||
|
||||
class GitStatus:
|
||||
"""class for resolving git gpg singing state"""
|
||||
class Definition:
|
||||
"""definition of a git gpg sing state"""
|
||||
key: str = 'N'
|
||||
status: int = 2
|
||||
msg: str = ''
|
||||
@ -189,19 +205,23 @@ class IntegrationPlugin(MixinBase, plugin.InvenTreePlugin):
|
||||
self.set_sign_values()
|
||||
|
||||
def mixin(self, key):
|
||||
"""check if mixin is registered"""
|
||||
return key in self._mixins
|
||||
|
||||
def mixin_enabled(self, key):
|
||||
"""check if mixin is enabled and ready"""
|
||||
if self.mixin(key):
|
||||
fnc_name = self._mixins.get(key)
|
||||
return getattr(self, fnc_name, True)
|
||||
return False
|
||||
|
||||
def get_plugin_commit(self):
|
||||
"""get last git commit for plugin"""
|
||||
path = inspect.getfile(self.__class__)
|
||||
return get_git_log(path)
|
||||
|
||||
def set_sign_values(self):
|
||||
"""add the last commit of the plugins class file into plugins context"""
|
||||
# fetch git log
|
||||
commit = self.get_plugin_commit()
|
||||
# resolve state
|
||||
|
@ -1,3 +1,4 @@
|
||||
"""sample implementations for IntegrationPlugin"""
|
||||
from plugins.integration.integration import SettingsMixin, UrlsMixin, NavigationMixin, IntegrationPlugin
|
||||
|
||||
from django.http import HttpResponse
|
||||
@ -13,6 +14,7 @@ class SampleIntegrationPlugin(SettingsMixin, UrlsMixin, NavigationMixin, Integra
|
||||
PLUGIN_NAME = "SampleIntegrationPlugin"
|
||||
|
||||
def view_test(self, request):
|
||||
"""very basic view"""
|
||||
return HttpResponse(f'Hi there {request.user.username} this works')
|
||||
|
||||
def setup_urls(self):
|
||||
|
@ -1,4 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Base Class for InvenTree plugins"""
|
||||
|
||||
|
||||
class InvenTreePlugin():
|
||||
@ -10,6 +11,7 @@ class InvenTreePlugin():
|
||||
PLUGIN_NAME = ''
|
||||
|
||||
def plugin_name(self):
|
||||
"""get plugin name"""
|
||||
return self.PLUGIN_NAME
|
||||
|
||||
def __init__(self):
|
||||
|
@ -1,4 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""general functions for plugin handeling"""
|
||||
|
||||
import inspect
|
||||
import importlib
|
||||
@ -17,17 +18,17 @@ logger = logging.getLogger("inventree")
|
||||
|
||||
|
||||
def iter_namespace(pkg):
|
||||
|
||||
"""get all modules in a package"""
|
||||
return pkgutil.iter_modules(pkg.__path__, pkg.__name__ + ".")
|
||||
|
||||
|
||||
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)]
|
||||
|
||||
|
||||
def get_classes(module):
|
||||
# Return all classes in a given module
|
||||
"""get all classes in a given module"""
|
||||
return inspect.getmembers(module, inspect.isclass)
|
||||
|
||||
|
||||
|
@ -10,15 +10,18 @@ class InvenTreePluginTests(TestCase):
|
||||
self.plugin = plugins.plugin.InvenTreePlugin()
|
||||
|
||||
class NamedPlugin(plugins.plugin.InvenTreePlugin):
|
||||
"""a named plugin"""
|
||||
PLUGIN_NAME = 'abc123'
|
||||
|
||||
self.named_plugin = NamedPlugin()
|
||||
|
||||
def test_basic_plugin_init(self):
|
||||
"""check if a basic plugin intis"""
|
||||
self.assertEqual(self.plugin.PLUGIN_NAME, '')
|
||||
self.assertEqual(self.plugin.plugin_name(), '')
|
||||
|
||||
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')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user