summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--portato/plugin.py109
1 files changed, 82 insertions, 27 deletions
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):
"""