From 1735514364097d7bfbf7808778c9cc5af5465a38 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 25 Sep 2021 01:22:48 +0200 Subject: [PATCH] more docstrings! --- InvenTree/plugins/action/action.py | 1 + .../plugins/action/simpleactionplugin.py | 1 + .../plugins/integration/another_sample.py | 1 + InvenTree/plugins/integration/integration.py | 22 ++++++++++++++++++- InvenTree/plugins/integration/sample.py | 2 ++ InvenTree/plugins/plugin.py | 2 ++ InvenTree/plugins/plugins.py | 7 +++--- InvenTree/plugins/test_plugin.py | 3 +++ 8 files changed, 35 insertions(+), 4 deletions(-) diff --git a/InvenTree/plugins/action/action.py b/InvenTree/plugins/action/action.py index 16e63a3a5c..72b24b1a14 100644 --- a/InvenTree/plugins/action/action.py +++ b/InvenTree/plugins/action/action.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""Class for ActionPlugin""" import logging diff --git a/InvenTree/plugins/action/simpleactionplugin.py b/InvenTree/plugins/action/simpleactionplugin.py index 95a50ddbca..07ac81f6a6 100644 --- a/InvenTree/plugins/action/simpleactionplugin.py +++ b/InvenTree/plugins/action/simpleactionplugin.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""sample implementation for ActionPlugin""" from plugins.action.action import ActionPlugin diff --git a/InvenTree/plugins/integration/another_sample.py b/InvenTree/plugins/integration/another_sample.py index a52fd6d32f..0dd2eb9996 100644 --- a/InvenTree/plugins/integration/another_sample.py +++ b/InvenTree/plugins/integration/another_sample.py @@ -1,3 +1,4 @@ +"""sample implementation for IntegrationPlugin""" from plugins.integration.integration import IntegrationPlugin, UrlsMixin diff --git a/InvenTree/plugins/integration/integration.py b/InvenTree/plugins/integration/integration.py index 4a0aafc8ff..f7f40ec508 100644 --- a/InvenTree/plugins/integration/integration.py +++ b/InvenTree/plugins/integration/integration.py @@ -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 diff --git a/InvenTree/plugins/integration/sample.py b/InvenTree/plugins/integration/sample.py index d4c2ea43de..347e0d9b7b 100644 --- a/InvenTree/plugins/integration/sample.py +++ b/InvenTree/plugins/integration/sample.py @@ -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): diff --git a/InvenTree/plugins/plugin.py b/InvenTree/plugins/plugin.py index de728ec873..93199df7b7 100644 --- a/InvenTree/plugins/plugin.py +++ b/InvenTree/plugins/plugin.py @@ -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): diff --git a/InvenTree/plugins/plugins.py b/InvenTree/plugins/plugins.py index b06957ae07..440aaa6c39 100644 --- a/InvenTree/plugins/plugins.py +++ b/InvenTree/plugins/plugins.py @@ -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) diff --git a/InvenTree/plugins/test_plugin.py b/InvenTree/plugins/test_plugin.py index ce03ab686e..db8952c38f 100644 --- a/InvenTree/plugins/test_plugin.py +++ b/InvenTree/plugins/test_plugin.py @@ -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')