Automatically load plugins from portable plugins dir; closes #1178

All modules/ packages in the openslides/plugins subdirectory of the
portable version will be automatically added to INSTALLED_PLUGINS
This commit is contained in:
Andy Kittner 2014-04-28 23:37:22 +02:00
parent 3ece699cd6
commit 3584da89c3
3 changed files with 39 additions and 9 deletions

10
extras/openslides_gui/gui.py Normal file → Executable file
View File

@ -21,10 +21,10 @@ import openslides
from openslides.utils.main import (
detect_openslides_type,
filesystem2unicode,
unicode2filesystem,
get_default_user_data_path,
get_port,
get_win32_portable_path,
PortableDirNotWritable
PortableDirNotWritable,
)
@ -97,11 +97,7 @@ class RunCommandControl(wx.Panel):
# XXX: subprocess on windows only handles byte strings
# with python3 this will hopefully no longer be the case
fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
cmd = [
x.encode(fs_encoding) if isinstance(x, unicode) else x
for x in cmd
]
cmd = [unicode2filesystem(x) for x in cmd]
creationflags = getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0)
self.child_process = subprocess.Popen(

View File

@ -5,7 +5,7 @@ import os
from django.utils.translation import ugettext_lazy
from openslides.utils.main import filesystem2unicode
from openslides.utils.plugins import get_plugins_from_entry_points
from openslides.utils.plugins import collect_plugins
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
@ -129,4 +129,4 @@ HAYSTACK_CONNECTIONS = {
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
# Adds all automaticly collected plugins
INSTALLED_PLUGINS = get_plugins_from_entry_points()
INSTALLED_PLUGINS = collect_plugins()

34
openslides/utils/plugins.py Normal file → Executable file
View File

@ -1,8 +1,18 @@
# -*- coding: utf-8 -*-
import pkgutil
import os
import sys
from django.utils.importlib import import_module
from pkg_resources import iter_entry_points
from openslides.utils.main import (
detect_openslides_type,
WINDOWS_PORTABLE_VERSION,
get_win32_portable_user_data_path,
)
plugins = {}
@ -15,6 +25,30 @@ def get_plugins_from_entry_points():
return tuple(entry_point.module_name for entry_point in iter_entry_points('openslides_plugins'))
def get_plugins_from_path(path):
"""
Collects all modules/packages in the given `path`
and returns a tuple of their names
"""
importer = pkgutil.get_importer(path)
return tuple(x[0] for x in importer.iter_modules())
def collect_plugins():
"""Collect all plugins that can be automatically discovered."""
plugins = get_plugins_from_entry_points()
# add all modules in plugins/ dir of portable automatically
if detect_openslides_type() == WINDOWS_PORTABLE_VERSION:
plugins_path = os.path.join(
get_win32_portable_user_data_path(), "plugins")
if plugins_path not in sys.path:
sys.path.append(plugins_path)
plugins += get_plugins_from_path(plugins_path)
return plugins
def get_plugin(plugin):
"""
Returns the imported module. The plugin argument must be a python dotted