mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Use metadata in projects for plugin info (#3282)
* Use metadata in projects for plugin info * use metadata as backup
This commit is contained in:
parent
e314e879da
commit
7493c8c6cb
@ -6,6 +6,7 @@ import os
|
||||
import pathlib
|
||||
import warnings
|
||||
from datetime import datetime
|
||||
from importlib.metadata import metadata
|
||||
|
||||
from django.conf import settings
|
||||
from django.db.utils import OperationalError, ProgrammingError
|
||||
@ -195,11 +196,26 @@ class InvenTreePlugin(MixinBase, MetaBase):
|
||||
|
||||
self.define_package()
|
||||
|
||||
def _get_value(self, meta_name: str, package_name: str) -> str:
|
||||
"""Extract values from class meta or package info.
|
||||
|
||||
Args:
|
||||
meta_name (str): Name of the class meta to use.
|
||||
package_name (str): Name of the package data to use.
|
||||
|
||||
Returns:
|
||||
str: Extracted value, None if nothing found.
|
||||
"""
|
||||
val = getattr(self, meta_name, None)
|
||||
if not val:
|
||||
val = self.package.get(package_name, None)
|
||||
return val
|
||||
|
||||
# region properties
|
||||
@property
|
||||
def description(self):
|
||||
"""Description of plugin."""
|
||||
description = getattr(self, 'DESCRIPTION', None)
|
||||
description = self._get_value('DESCRIPTION', 'description')
|
||||
if not description:
|
||||
description = self.plugin_name()
|
||||
return description
|
||||
@ -207,9 +223,7 @@ class InvenTreePlugin(MixinBase, MetaBase):
|
||||
@property
|
||||
def author(self):
|
||||
"""Author of plugin - either from plugin settings or git."""
|
||||
author = getattr(self, 'AUTHOR', None)
|
||||
if not author:
|
||||
author = self.package.get('author')
|
||||
author = self._get_value('AUTHOR', 'author')
|
||||
if not author:
|
||||
author = _('No author found') # pragma: no cover
|
||||
return author
|
||||
@ -229,19 +243,19 @@ class InvenTreePlugin(MixinBase, MetaBase):
|
||||
@property
|
||||
def version(self):
|
||||
"""Version of plugin."""
|
||||
version = getattr(self, 'VERSION', None)
|
||||
version = self._get_value('VERSION', 'version')
|
||||
return version
|
||||
|
||||
@property
|
||||
def website(self):
|
||||
"""Website of plugin - if set else None."""
|
||||
website = getattr(self, 'WEBSITE', None)
|
||||
website = self._get_value('WEBSITE', 'website')
|
||||
return website
|
||||
|
||||
@property
|
||||
def license(self):
|
||||
"""License of plugin."""
|
||||
lic = getattr(self, 'LICENSE', None)
|
||||
lic = self._get_value('LICENSE', 'license')
|
||||
return lic
|
||||
# endregion
|
||||
|
||||
@ -273,9 +287,18 @@ class InvenTreePlugin(MixinBase, MetaBase):
|
||||
"""Get last git commit for the plugin."""
|
||||
return get_git_log(self.def_path)
|
||||
|
||||
def _get_package_metadata(self):
|
||||
@classmethod
|
||||
def _get_package_metadata(cls):
|
||||
"""Get package metadata for plugin."""
|
||||
return {} # pragma: no cover # TODO add usage for package metadata
|
||||
meta = metadata(cls.__name__)
|
||||
|
||||
return {
|
||||
'author': meta['Author-email'],
|
||||
'description': meta['Summary'],
|
||||
'version': meta['Version'],
|
||||
'website': meta['Project-URL'],
|
||||
'license': meta['License']
|
||||
}
|
||||
|
||||
def define_package(self):
|
||||
"""Add package info of the plugin into plugins context."""
|
||||
|
@ -208,6 +208,7 @@ class PluginsRegistry:
|
||||
try:
|
||||
plugin = entry.load()
|
||||
plugin.is_package = True
|
||||
plugin._get_package_metadata()
|
||||
self.plugin_modules.append(plugin)
|
||||
except Exception as error:
|
||||
handle_error(error, do_raise=False, log_name='discovery')
|
||||
|
Loading…
Reference in New Issue
Block a user