[pitivi] pluginmanager: Create PluginManager to handle plugins and extensions



commit ed321acfa2e84b67ce2ff7754ad135674b0aa74a
Author: Fabian Orccon <cfoch fabian gmail com>
Date:   Tue May 23 12:58:01 2017 -0500

    pluginmanager: Create PluginManager to handle plugins and extensions
    
    Differential Revision: https://phabricator.freedesktop.org/D1748

 pitivi/application.py   |    2 +
 pitivi/configure.py.in  |   10 +++++
 pitivi/pluginmanager.py |   88 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 100 insertions(+), 0 deletions(-)
---
diff --git a/pitivi/application.py b/pitivi/application.py
index f3309c8..48f7062 100644
--- a/pitivi/application.py
+++ b/pitivi/application.py
@@ -32,6 +32,7 @@ from pitivi.configure import VERSION
 from pitivi.dialogs.startupwizard import StartUpWizard
 from pitivi.effects import EffectsManager
 from pitivi.mainwindow import MainWindow
+from pitivi.pluginmanager import PluginManager
 from pitivi.project import ProjectManager
 from pitivi.settings import get_dir
 from pitivi.settings import GlobalSettings
@@ -144,6 +145,7 @@ class Pitivi(Gtk.Application, Loggable):
         self.effects = EffectsManager()
         self.proxy_manager = ProxyManager(self)
         self.system = get_system()
+        self.plugin_manager = PluginManager(self)
 
         self.project_manager.connect(
             "new-project-loading", self._newProjectLoadingCb)
diff --git a/pitivi/configure.py.in b/pitivi/configure.py.in
index 62d81a1..3d1c67a 100644
--- a/pitivi/configure.py.in
+++ b/pitivi/configure.py.in
@@ -22,6 +22,7 @@ Enables identical use for installed and uninstalled versions.
 """
 
 import os.path
+from gi.repository import GLib
 
 
 # Again, mostly the same thing as in bin/pitivi.in and pitivi/utils/misc.py:
@@ -83,3 +84,12 @@ def get_videopresets_dir():
 def get_gstpresets_dir():
     """Returns our directory with Gst Presets files."""
     return os.path.join(get_data_dir(), 'gstpresets')
+
+def get_plugins_dir():
+    """Returns our default directory to store official plugins."""
+    return os.path.join(_get_root_dir(), 'plugins')
+
+def get_user_plugins_dir():
+    """Returns our default directory to store non-official plugins."""
+    user_data_dir = GLib.get_user_data_dir()
+    return os.path.join(user_data_dir, 'pitivi', 'plugins')
diff --git a/pitivi/pluginmanager.py b/pitivi/pluginmanager.py
new file mode 100644
index 0000000..ef953d6
--- /dev/null
+++ b/pitivi/pluginmanager.py
@@ -0,0 +1,88 @@
+# pylint: disable=missing-docstring
+# -*- coding: utf-8 -*-
+# Copyright (c) 2017, Fabian Orccon <cfoch fabian gmail com>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the
+# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+# Boston, MA 02110-1301, USA.
+import os
+
+from gi.repository import GObject
+from gi.repository import Peas
+
+from pitivi.configure import get_plugins_dir
+from pitivi.configure import get_user_plugins_dir
+
+
+class API(GObject.GObject):
+    """Interface that gives access to all the objects inside Pitivi."""
+
+    def __init__(self, app):
+        GObject.GObject.__init__(self)
+        self.app = app
+
+
+class PluginManager:
+    """Pitivi Plugin Manager to handle a set of plugins.
+
+    Attributes:
+        DEFAULT_LOADERS (tuple): Default loaders used by the plugin manager. For
+            possible values see
+            https://developer.gnome.org/libpeas/stable/PeasEngine.html#peas-engine-enable-loader
+    """
+
+    DEFAULT_LOADERS = ("python3", )
+
+    def __init__(self, app):
+        self.app = app
+        self.engine = Peas.Engine.get_default()
+
+        for loader in self.DEFAULT_LOADERS:
+            self.engine.enable_loader(loader)
+
+        self._setup_plugins_dir()
+        self._setup_extension_set()
+
+    @property
+    def plugins(self):
+        """Gets the engine's plugin list."""
+        return self.engine.get_plugin_list()
+
+    def _setup_extension_set(self):
+        plugin_iface = API(self.app)
+        self.extension_set =\
+            Peas.ExtensionSet.new_with_properties(self.engine,
+                                                  Peas.Activatable,
+                                                  ["object"],
+                                                  [plugin_iface])
+        self.extension_set.connect("extension-removed",
+                                   self.__extension_removed_cb)
+        self.extension_set.connect("extension-added",
+                                   self.__extension_added_cb)
+
+    def _setup_plugins_dir(self):
+        plugins_dir = get_plugins_dir()
+        user_plugins_dir = get_user_plugins_dir()
+        if os.path.exists(plugins_dir):
+            self.engine.add_search_path(plugins_dir)
+        if os.path.exists(plugins_dir):
+            self.engine.add_search_path(user_plugins_dir)
+
+    @staticmethod
+    def __extension_removed_cb(unused_set, unused_plugin_info, extension):
+        extension.deactivate()
+
+    @staticmethod
+    def __extension_added_cb(unused_set, unused_plugin_info, extension):
+        extension.activate()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]