From 3584da89c364b368580713d7b356ef739abd4632 Mon Sep 17 00:00:00 2001 From: Andy Kittner Date: Mon, 28 Apr 2014 23:37:22 +0200 Subject: [PATCH] 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 --- extras/openslides_gui/gui.py | 10 +++------- openslides/global_settings.py | 4 ++-- openslides/utils/plugins.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 9 deletions(-) mode change 100644 => 100755 extras/openslides_gui/gui.py mode change 100644 => 100755 openslides/utils/plugins.py diff --git a/extras/openslides_gui/gui.py b/extras/openslides_gui/gui.py old mode 100644 new mode 100755 index 296ee2a8f..427c105c7 --- a/extras/openslides_gui/gui.py +++ b/extras/openslides_gui/gui.py @@ -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( diff --git a/openslides/global_settings.py b/openslides/global_settings.py index e72824234..6de07d99b 100644 --- a/openslides/global_settings.py +++ b/openslides/global_settings.py @@ -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() diff --git a/openslides/utils/plugins.py b/openslides/utils/plugins.py old mode 100644 new mode 100755 index d494de0f8..56efa564c --- a/openslides/utils/plugins.py +++ b/openslides/utils/plugins.py @@ -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