Merge pull request #1980 from normanjaeckel/PluginAPI

Updated API for plugins.
This commit is contained in:
Emanuel Schütze 2016-02-17 20:40:40 +01:00
commit ecb29a1403
7 changed files with 36 additions and 40 deletions

View File

@ -1,8 +1,8 @@
import os import os
import pkgutil import pkgutil
import sys import sys
from importlib import import_module
from django.apps import apps
from django.conf import settings from django.conf import settings
from pkg_resources import iter_entry_points from pkg_resources import iter_entry_points
@ -12,8 +12,6 @@ from openslides.utils.main import (
get_win32_portable_user_data_path, get_win32_portable_user_data_path,
) )
plugins = {}
# Methods to collect plugins. # Methods to collect plugins.
@ -52,35 +50,14 @@ def collect_plugins():
return collected_plugins return collected_plugins
# Methods to retrieve plugins and their metadata. # Methods to retrieve plugin metadata and urlpatterns.
def get_plugin(plugin):
"""
Returns the imported module. The plugin argument must be a python dotted
module path.
"""
try:
plugin = plugins[plugin]
except KeyError:
plugins[plugin] = import_module(plugin)
plugin = get_plugin(plugin)
return plugin
def get_plugin_verbose_name(plugin): def get_plugin_verbose_name(plugin):
""" """
Returns the verbose name of a plugin. The plugin argument must be a python Returns the verbose name of a plugin. The plugin argument must be a python
dotted module path. dotted module path.
""" """
plugin = get_plugin(plugin) return apps.get_app_config(plugin).verbose_name
try:
verbose_name = plugin.get_verbose_name()
except AttributeError:
try:
verbose_name = plugin.__verbose_name__
except AttributeError:
verbose_name = plugin.__name__
return verbose_name
def get_plugin_description(plugin): def get_plugin_description(plugin):
@ -88,12 +65,12 @@ def get_plugin_description(plugin):
Returns the short descrption of a plugin. The plugin argument must be a Returns the short descrption of a plugin. The plugin argument must be a
python dotted module path. python dotted module path.
""" """
plugin = get_plugin(plugin) plugin_app_config = apps.get_app_config(plugin)
try: try:
description = plugin.get_description() description = plugin_app_config.get_description()
except AttributeError: except AttributeError:
try: try:
description = plugin.__description__ description = plugin_app_config.description
except AttributeError: except AttributeError:
description = '' description = ''
return description return description
@ -104,12 +81,12 @@ def get_plugin_version(plugin):
Returns the version string of a plugin. The plugin argument must be a Returns the version string of a plugin. The plugin argument must be a
python dotted module path. python dotted module path.
""" """
plugin = get_plugin(plugin) plugin_app_config = apps.get_app_config(plugin)
try: try:
version = plugin.get_version() version = plugin_app_config.get_version()
except AttributeError: except AttributeError:
try: try:
version = plugin.__version__ version = plugin_app_config.version
except AttributeError: except AttributeError:
version = 'unknown' version = 'unknown'
return version return version
@ -120,12 +97,12 @@ def get_plugin_urlpatterns(plugin):
Returns the urlpatterns object for a plugin. The plugin argument must be Returns the urlpatterns object for a plugin. The plugin argument must be
a python dotted module path. a python dotted module path.
""" """
plugin = get_plugin(plugin) plugin_app_config = apps.get_app_config(plugin)
try: try:
urlpatterns = plugin.get_urlpatterns() urlpatterns = plugin_app_config.get_urlpatterns()
except AttributeError: except AttributeError:
try: try:
urlpatterns = plugin.urls.urlpatterns urlpatterns = plugin_app_config.urlpatterns
except AttributeError: except AttributeError:
urlpatterns = None urlpatterns = None
return urlpatterns return urlpatterns

View File

@ -70,8 +70,8 @@ class VersionView(TestCase):
self.assertEqual(json.loads(response.content.decode()), { self.assertEqual(json.loads(response.content.decode()), {
'openslides_version': version, 'openslides_version': version,
'plugins': [ 'plugins': [
{'verbose_name': 'Plugin tests.old.utils', {'verbose_name': 'OpenSlides Test Plugin',
'description': 'Description of plugin tests.old.utils', 'description': 'This is a test plugin for OpenSlides.',
'version': 'unknown'}]}) 'version': 'unknown'}]})

View File

@ -0,0 +1,4 @@
__verbose_name__ = 'OpenSlides Test Plugin'
__description__ = 'This is a test plugin for OpenSlides.'
default_app_config = 'tests.integration.test_plugin.apps.TestPluginAppConfig'

View File

@ -0,0 +1,10 @@
from django.apps import AppConfig
from . import __description__, __verbose_name__
class TestPluginAppConfig(AppConfig):
name = 'tests.integration.test_plugin'
label = 'tests.integration.test_plugin'
verbose_name = __verbose_name__
description = __description__

View File

@ -1,2 +1 @@
__verbose_name__ = 'Plugin tests.old.utils' default_app_config = 'tests.old.utils.apps.TestPluginAppConfig'
__description__ = 'Description of plugin tests.old.utils'

6
tests/old/utils/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class TestPluginAppConfig(AppConfig):
name = 'tests.old.utils'
label = 'tests.old.utils'

View File

@ -17,7 +17,7 @@ SECRET_KEY = 'secret'
# Add plugins to this list. # Add plugins to this list.
INSTALLED_PLUGINS += ( INSTALLED_PLUGINS += (
'tests.old.utils', 'tests.integration.test_plugin',
) )
INSTALLED_APPS += INSTALLED_PLUGINS INSTALLED_APPS += INSTALLED_PLUGINS