From 62725d63688631950d21a6a03a1cecfccf78f4e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Fri, 24 Apr 2009 17:53:17 +0200 Subject: Removed special Menu from plugin structure. Added more general 'WidgetSlot' --- portato/plugin.py | 109 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 82 insertions(+), 27 deletions(-) (limited to 'portato') diff --git a/portato/plugin.py b/portato/plugin.py index 94a0b4c..45305b0 100644 --- a/portato/plugin.py +++ b/portato/plugin.py @@ -34,24 +34,6 @@ class PluginLoadException (Exception): """ pass -class Menu (object): - """ - One single menu entry. - - :IVariables: - - label : string - The label of the entry. Can have underscores to define the shortcut. - - call - The function to call, if the entry is clicked. - """ - __slots__ = ("label", "call") - - def __init__ (self, label, call): - self.label = label - self.call = call - class Call (object): """ This class represents an object, which is attached to a specified hook. @@ -104,6 +86,51 @@ class Hook (object): self.override = None self.after = [] +class WidgetSlot (object): + """ + A slot, where plugins can add widgets. + + :IVariables: + + widget : gobject.GObjectMeta + The widget class, which can be used in this slot. + + name : string + The slot's name. + + max : int + The maximum number of widgets which can be registered. -1 means unlimited. + + """ + + slots = {} + __slots__ = ("widget", "name", "max") + + def __init__ (self, widget, name, max = -1): + self.widget = widget + self.name = name + self.max = max + + WidgetSlot.slots[name] = self + +class Widget (object): + """ + Fills a WidgetSlot. + + :IVariables: + + slot : string + The name of the slot to fill. + + widget : gtk.Widget + The widget which is added. + + """ + + def __init__ (self, slot, widget): + self.slot = slot + self.widget = widget + class Plugin (object): """ This is the main plugin object. It is used where ever a plugin is wanted, and it is the one, which needs to be subclassed by plugin authors. @@ -134,7 +161,7 @@ class Plugin (object): :param disable: Forcefully disable the plugin :type disable: bool """ - self.__menus = [] #: List of `Menu` + self.__widgets = [] #: List of `Widget` self.__calls = [] #: List of `Call` self._unresolved_deps = False #: Does this plugin has unresolved dependencies? @@ -203,13 +230,13 @@ class Plugin (object): return getattr(self, "__name__", self.__class__.__name__) @property - def menus (self): + def widget (self): """ - Returns an iterator over the menus for this plugin. + Returns an iterator over the widgets for this plugin. - :rtype: iter<`Menu`> + :rtype: iter<`Widget`> """ - return iter(self.__menus) + return iter(self.__widgets) @property def calls (self): @@ -243,13 +270,41 @@ class Plugin (object): """ return (self.status in (self.STAT_ENABLED, self.STAT_TEMP_ENABLED)) - def add_menu (self, label, callable): + def add_widget (self, slot, widget): + """ + Adds a new widget for this plugin. + + :see: `Widget` """ - Adds a new menu item for this plugin. + self.__widgets.append(Widget(slot, widget)) - :see: `Menu` + def create_widget (self, slot, args, **kwargs): """ - self.__menus.append(Menu(label, callable)) + Creates a new widget for the plugin. + + :Parameters: + + slot : string + The slot to create a widget for. + + args : list of objects + The arguments to pass to the widget constructor. + + kwargs : dict + Additional named parameters are for callback functions. + """ + + try: + widget = WidgetSlot.slots[slot].widget + except KeyError: + raise PluginLoadException, "Could not find specified widget slot: %s" % slot + + w = widget(*args) + + for k,v in kwargs.iteritems(): + w.connect(k, v) + + self.add_widget(slot, w) def add_call (self, hook, callable, type = "before", dep = None): """ -- cgit v1.2.3 From 9a9e90bf28509147eedb5a2ebe5fc110c6244a34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Fri, 24 Apr 2009 18:11:53 +0200 Subject: Should add widgets now --- portato/plugin.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) (limited to 'portato') diff --git a/portato/plugin.py b/portato/plugin.py index 45305b0..9efe396 100644 --- a/portato/plugin.py +++ b/portato/plugin.py @@ -98,21 +98,37 @@ class WidgetSlot (object): name : string The slot's name. + init : function + Function to call, iff there is at least one widget for that slot. + + add : function(`Widget`) + Function to call, if a widget is added. + max : int The maximum number of widgets which can be registered. -1 means unlimited. """ slots = {} - __slots__ = ("widget", "name", "max") - - def __init__ (self, widget, name, max = -1): + + def __init__ (self, widget, name, add = None, init = None, max = -1): self.widget = widget self.name = name self.max = max + self.init = init + self.add = add + self._inited = False WidgetSlot.slots[name] = self + def add_widget (self, w): + if not self._inited: + if self.init is not None: + self.init() + self._inited = True + + self.add(w) + class Widget (object): """ Fills a WidgetSlot. @@ -127,6 +143,8 @@ class Widget (object): """ + __slots__ = ("slot", "widget") + def __init__ (self, slot, widget): self.slot = slot self.widget = widget @@ -230,7 +248,7 @@ class Plugin (object): return getattr(self, "__name__", self.__class__.__name__) @property - def widget (self): + def widgets (self): """ Returns an iterator over the widgets for this plugin. @@ -389,6 +407,10 @@ class PluginQueue (object): self._organize() + for p in self.plugins: + for w in p.widgets: + WidgetSlot[w.slot].add_widget(w) + def add (self, plugin, disable = False): """ Adds a plugin to the internal list. @@ -536,7 +558,6 @@ class PluginQueue (object): for hook, calls in star_after.iteritems(): self.hooks[hook].after.extend(calls) # append the list - def _resolve_unresolved (self, before, after): def resolve(hook, list, type, add): if not list: -- cgit v1.2.3 From ab69d8ac22a4548a55d3fbb4f5d8e7aff77df293 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Fri, 24 Apr 2009 18:13:05 +0200 Subject: Load plugins later in the startup process. No menu magic anymore --- portato/gui/windows/main.py | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) (limited to 'portato') diff --git a/portato/gui/windows/main.py b/portato/gui/windows/main.py index a0127e0..6a65d2e 100644 --- a/portato/gui/windows/main.py +++ b/portato/gui/windows/main.py @@ -472,31 +472,6 @@ class MainWindow (Window): # package db splash(_("Creating Database")) self.db = Database(self.cfg.get("type", section = "DATABASE")) - - # set plugins and plugin-menu - splash(_("Loading Plugins")) - - plugin.load_plugins() - menus = [p.menus for p in plugin.get_plugin_queue().get_plugins()] - if menus: - uim = self.tree.get_widget("uimanager") - ag = self.tree.get_widget("pluginActionGroup") - - ctr = 0 - for m in itt.chain(*menus): - - # create action - aname = "plugin%d" % ctr - a = gtk.Action(aname, m.label, None, None) - a.connect("activate", m.call) - ag.add_action(a) - - # add to UI - mid = uim.new_merge_id() - uim.add_ui(mid, "ui/menubar/pluginMenu", aname, aname, gtk.UI_MANAGER_MENUITEM, False) - - ctr += 1 - splash(_("Building frontend")) # set paned position @@ -581,6 +556,11 @@ class MainWindow (Window): self.queueTree = GtkTree(self.queueList.get_model()) self.queue = EmergeQueue(console = self.console, tree = self.queueTree, db = self.db, title_update = self.title_update, threadClass = GtkThread) + # set plugins and plugin-menu + splash(_("Loading Plugins")) + + plugin.load_plugins() + # session splash(_("Restoring Session")) try: -- cgit v1.2.3 From 4c1d61cb71a396f09969ed264c91ebfff29145c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Fri, 24 Apr 2009 18:34:21 +0200 Subject: Create PluginMenuSlot --- portato/gui/windows/main.py | 22 ++++++++++++++++++++++ portato/plugin.py | 1 + 2 files changed, 23 insertions(+) (limited to 'portato') diff --git a/portato/gui/windows/main.py b/portato/gui/windows/main.py index 6a65d2e..cb463c8 100644 --- a/portato/gui/windows/main.py +++ b/portato/gui/windows/main.py @@ -50,6 +50,27 @@ from .preference import PreferenceWindow from .search import SearchWindow from .update import UpdateWindow +class PluginMenuSlot (plugin.WidgetSlot): + + def __init__ (self, tree): + plugin.WidgetSlot.__init__(self, self.create_action, "Plugin Menu") + + self.ctr = 0 # counter for the plugin actions + self.uim = tree.get_widget("uimanager") + self.ag = tree.get_widget("pluginActionGroup") + + def create_action (self, label): + aname = "plugin%d" % self.ctr + a = gtk.Action(aname, label, None, None) + self.ctr += 1 + + def add (self, action): + self.ag.add_action(action) + + # add to UI + mid = self.uim.new_merge_id() + self.uim.add_ui(mid, "ui/menubar/pluginMenu", action.get_name(), action.get_name(), gtk.UI_MANAGER_MENUITEM, False) + class PackageTable: """A window with data about a specfic package.""" @@ -559,6 +580,7 @@ class MainWindow (Window): # set plugins and plugin-menu splash(_("Loading Plugins")) + PluginMenuSlot(self.tree) plugin.load_plugins() # session diff --git a/portato/plugin.py b/portato/plugin.py index 9efe396..50cc29f 100644 --- a/portato/plugin.py +++ b/portato/plugin.py @@ -119,6 +119,7 @@ class WidgetSlot (object): self.add = add self._inited = False + debug("Registering new WidgetSlot '%s'.", name) WidgetSlot.slots[name] = self def add_widget (self, w): -- cgit v1.2.3 From 785e1f2cb2fcd32426931d47fbca72c23ccc06ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Fri, 24 Apr 2009 19:11:04 +0200 Subject: Make the new widget structure work. At least for the Plugin Menu --- portato/gui/templates/MainWindow.ui | 1 + portato/gui/windows/main.py | 7 ++++++- portato/plugin.py | 26 +++++++++++++++++++++----- 3 files changed, 28 insertions(+), 6 deletions(-) (limited to 'portato') diff --git a/portato/gui/templates/MainWindow.ui b/portato/gui/templates/MainWindow.ui index 383011e..473821d 100644 --- a/portato/gui/templates/MainWindow.ui +++ b/portato/gui/templates/MainWindow.ui @@ -8,6 +8,7 @@ pluginMenuAction Plu_gins + True diff --git a/portato/gui/windows/main.py b/portato/gui/windows/main.py index cb463c8..0259fc7 100644 --- a/portato/gui/windows/main.py +++ b/portato/gui/windows/main.py @@ -63,14 +63,19 @@ class PluginMenuSlot (plugin.WidgetSlot): aname = "plugin%d" % self.ctr a = gtk.Action(aname, label, None, None) self.ctr += 1 + + return a - def add (self, action): + def add (self, widget): + action = widget.widget self.ag.add_action(action) # add to UI mid = self.uim.new_merge_id() self.uim.add_ui(mid, "ui/menubar/pluginMenu", action.get_name(), action.get_name(), gtk.UI_MANAGER_MENUITEM, False) + self.uim.ensure_update() + class PackageTable: """A window with data about a specfic package.""" diff --git a/portato/plugin.py b/portato/plugin.py index 50cc29f..4914f25 100644 --- a/portato/plugin.py +++ b/portato/plugin.py @@ -115,8 +115,16 @@ class WidgetSlot (object): self.widget = widget self.name = name self.max = max - self.init = init - self.add = add + + # we might be subclassed + # in this case do not overwrite + + if not hasattr(self, "init"): + self.init = init + + if not hasattr(self, "add"): + self.add = add + self._inited = False debug("Registering new WidgetSlot '%s'.", name) @@ -128,7 +136,8 @@ class WidgetSlot (object): self.init() self._inited = True - self.add(w) + if self.add is not None: + self.add(w) class Widget (object): """ @@ -295,6 +304,10 @@ class Plugin (object): :see: `Widget` """ + + if not slot in WidgetSlot.slots: + raise PluginLoadException, "Could not find specified widget slot: %s" % slot + self.__widgets.append(Widget(slot, widget)) def create_widget (self, slot, args, **kwargs): @@ -318,7 +331,10 @@ class Plugin (object): except KeyError: raise PluginLoadException, "Could not find specified widget slot: %s" % slot - w = widget(*args) + if not hasattr(args, "__iter__"): + w = widget(args) + else: + w = widget(*args) for k,v in kwargs.iteritems(): w.connect(k, v) @@ -410,7 +426,7 @@ class PluginQueue (object): for p in self.plugins: for w in p.widgets: - WidgetSlot[w.slot].add_widget(w) + WidgetSlot.slots[w.slot].add_widget(w) def add (self, plugin, disable = False): """ -- cgit v1.2.3 From 8b43c15dbc1207afece005c0d04a73e7d08306b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Fri, 24 Apr 2009 19:12:09 +0200 Subject: Committed a wrong change --- portato/gui/templates/MainWindow.ui | 1 - 1 file changed, 1 deletion(-) (limited to 'portato') diff --git a/portato/gui/templates/MainWindow.ui b/portato/gui/templates/MainWindow.ui index 473821d..383011e 100644 --- a/portato/gui/templates/MainWindow.ui +++ b/portato/gui/templates/MainWindow.ui @@ -8,7 +8,6 @@ pluginMenuAction Plu_gins - True -- cgit v1.2.3 From 1091a8192ba7a5ccdc6a3c31d87864939fe4adb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Sat, 25 Apr 2009 11:15:52 +0200 Subject: Add emerge options slot --- portato/gui/templates/MainWindow.ui | 2 +- portato/gui/windows/main.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) (limited to 'portato') diff --git a/portato/gui/templates/MainWindow.ui b/portato/gui/templates/MainWindow.ui index 383011e..b0a9f18 100644 --- a/portato/gui/templates/MainWindow.ui +++ b/portato/gui/templates/MainWindow.ui @@ -963,7 +963,7 @@ - + True True diff --git a/portato/gui/windows/main.py b/portato/gui/windows/main.py index 0259fc7..290fe6a 100644 --- a/portato/gui/windows/main.py +++ b/portato/gui/windows/main.py @@ -585,6 +585,9 @@ class MainWindow (Window): # set plugins and plugin-menu splash(_("Loading Plugins")) + optionsHB = self.tree.get_widget("optionsHB") + plugin.WidgetSlot(gtk.CheckButton, "Emerge Options", add = lambda w: optionsHB.pack_end(w.widget)) + PluginMenuSlot(self.tree) plugin.load_plugins() -- cgit v1.2.3 From a6a43af88f97e8cd4684e220dc97d3037d92a068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Fri, 15 May 2009 17:36:04 +0200 Subject: Introduced extra slots module. --- portato/gui/slots.py | 42 ++++++++++++++++++++++++++++++++++++++++++ portato/gui/windows/main.py | 36 +++++++----------------------------- 2 files changed, 49 insertions(+), 29 deletions(-) create mode 100644 portato/gui/slots.py (limited to 'portato') diff --git a/portato/gui/slots.py b/portato/gui/slots.py new file mode 100644 index 0000000..2ad4c01 --- /dev/null +++ b/portato/gui/slots.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +# +# File: portato/gui/slots.py +# This file is part of the Portato-Project, a graphical portage-frontend. +# +# Copyright (C) 2006-2009 René 'Necoro' Neumann +# This is free software. You may redistribute copies of it under the terms of +# the GNU General Public License version 2. +# There is NO WARRANTY, to the extent permitted by law. +# +# Written by René 'Necoro' Neumann + +from __future__ import absolute_import, with_statement + +import gtk +from ...plugin import WidgetSlot # other modules might import WidgetSlot from here + +class PluginMenuSlot (WidgetSlot): + + def __init__ (self, tree): + WidgetSlot.__init__(self, self.create_action, "Plugin Menu") + + self.ctr = 0 # counter for the plugin actions + self.uim = tree.get_widget("uimanager") + self.ag = tree.get_widget("pluginActionGroup") + + def create_action (self, label): + aname = "plugin%d" % self.ctr + a = gtk.Action(aname, label, None, None) + self.ctr += 1 + + return a + + def add (self, widget): + action = widget.widget + self.ag.add_action(action) + + # add to UI + mid = self.uim.new_merge_id() + self.uim.add_ui(mid, "ui/menubar/pluginMenu", action.get_name(), action.get_name(), gtk.UI_MANAGER_MENUITEM, False) + + self.uim.ensure_update() diff --git a/portato/gui/windows/main.py b/portato/gui/windows/main.py index 290fe6a..1758033 100644 --- a/portato/gui/windows/main.py +++ b/portato/gui/windows/main.py @@ -23,13 +23,17 @@ from collections import defaultdict # our backend stuff from ...backend import flags, system # must be the first to avoid circular deps -from ... import get_listener, plugin +from ... import get_listener from ...helper import debug, warning, error, info from ...session import Session from ...db import Database from ...constants import CONFIG_LOCATION, VERSION, APP_ICON from ...backend.exceptions import PackageNotFoundException, BlockedException, VersionsNotFoundException +# plugin stuff +from ... import plugin +from .. import slots + # more GUI stuff from ..utils import Config, GtkThread, get_color from ..queue import EmergeQueue @@ -50,32 +54,6 @@ from .preference import PreferenceWindow from .search import SearchWindow from .update import UpdateWindow -class PluginMenuSlot (plugin.WidgetSlot): - - def __init__ (self, tree): - plugin.WidgetSlot.__init__(self, self.create_action, "Plugin Menu") - - self.ctr = 0 # counter for the plugin actions - self.uim = tree.get_widget("uimanager") - self.ag = tree.get_widget("pluginActionGroup") - - def create_action (self, label): - aname = "plugin%d" % self.ctr - a = gtk.Action(aname, label, None, None) - self.ctr += 1 - - return a - - def add (self, widget): - action = widget.widget - self.ag.add_action(action) - - # add to UI - mid = self.uim.new_merge_id() - self.uim.add_ui(mid, "ui/menubar/pluginMenu", action.get_name(), action.get_name(), gtk.UI_MANAGER_MENUITEM, False) - - self.uim.ensure_update() - class PackageTable: """A window with data about a specfic package.""" @@ -586,9 +564,9 @@ class MainWindow (Window): splash(_("Loading Plugins")) optionsHB = self.tree.get_widget("optionsHB") - plugin.WidgetSlot(gtk.CheckButton, "Emerge Options", add = lambda w: optionsHB.pack_end(w.widget)) + slots.WidgetSlot(gtk.CheckButton, "Emerge Options", add = lambda w: optionsHB.pack_end(w.widget)) - PluginMenuSlot(self.tree) + slots.PluginMenuSlot(self.tree) plugin.load_plugins() # session -- cgit v1.2.3