Use metadata in projects for plugin info (#3282)

* Use metadata in projects for plugin info

* use metadata as backup
This commit is contained in:
Matthias Mair 2022-07-09 09:06:50 +02:00 committed by GitHub
parent e314e879da
commit 7493c8c6cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 9 deletions

View File

@ -6,6 +6,7 @@ import os
import pathlib import pathlib
import warnings import warnings
from datetime import datetime from datetime import datetime
from importlib.metadata import metadata
from django.conf import settings from django.conf import settings
from django.db.utils import OperationalError, ProgrammingError from django.db.utils import OperationalError, ProgrammingError
@ -195,11 +196,26 @@ class InvenTreePlugin(MixinBase, MetaBase):
self.define_package() 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 # region properties
@property @property
def description(self): def description(self):
"""Description of plugin.""" """Description of plugin."""
description = getattr(self, 'DESCRIPTION', None) description = self._get_value('DESCRIPTION', 'description')
if not description: if not description:
description = self.plugin_name() description = self.plugin_name()
return description return description
@ -207,9 +223,7 @@ class InvenTreePlugin(MixinBase, MetaBase):
@property @property
def author(self): def author(self):
"""Author of plugin - either from plugin settings or git.""" """Author of plugin - either from plugin settings or git."""
author = getattr(self, 'AUTHOR', None) author = self._get_value('AUTHOR', 'author')
if not author:
author = self.package.get('author')
if not author: if not author:
author = _('No author found') # pragma: no cover author = _('No author found') # pragma: no cover
return author return author
@ -229,19 +243,19 @@ class InvenTreePlugin(MixinBase, MetaBase):
@property @property
def version(self): def version(self):
"""Version of plugin.""" """Version of plugin."""
version = getattr(self, 'VERSION', None) version = self._get_value('VERSION', 'version')
return version return version
@property @property
def website(self): def website(self):
"""Website of plugin - if set else None.""" """Website of plugin - if set else None."""
website = getattr(self, 'WEBSITE', None) website = self._get_value('WEBSITE', 'website')
return website return website
@property @property
def license(self): def license(self):
"""License of plugin.""" """License of plugin."""
lic = getattr(self, 'LICENSE', None) lic = self._get_value('LICENSE', 'license')
return lic return lic
# endregion # endregion
@ -273,9 +287,18 @@ class InvenTreePlugin(MixinBase, MetaBase):
"""Get last git commit for the plugin.""" """Get last git commit for the plugin."""
return get_git_log(self.def_path) return get_git_log(self.def_path)
def _get_package_metadata(self): @classmethod
def _get_package_metadata(cls):
"""Get package metadata for plugin.""" """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): def define_package(self):
"""Add package info of the plugin into plugins context.""" """Add package info of the plugin into plugins context."""

View File

@ -208,6 +208,7 @@ class PluginsRegistry:
try: try:
plugin = entry.load() plugin = entry.load()
plugin.is_package = True plugin.is_package = True
plugin._get_package_metadata()
self.plugin_modules.append(plugin) self.plugin_modules.append(plugin)
except Exception as error: except Exception as error:
handle_error(error, do_raise=False, log_name='discovery') handle_error(error, do_raise=False, log_name='discovery')