From 8e07fd436cfbf02fbf43c9e221badb55acdb546a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Fri, 4 Jul 2008 14:15:55 +0200 Subject: Some more dependency awareness --- plugins/etc_proposals.py | 4 +-- plugins/gpytage.py | 3 +-- plugins/notify.py | 12 ++++++--- portato/gui/windows/main.py | 3 +++ portato/plugin.py | 60 ++++++++++++++++++++++++++++++++++----------- 5 files changed, 59 insertions(+), 23 deletions(-) diff --git a/plugins/etc_proposals.py b/plugins/etc_proposals.py index 07f9a80..c32c8f3 100644 --- a/plugins/etc_proposals.py +++ b/plugins/etc_proposals.py @@ -20,9 +20,7 @@ class EtcProposals (Plugin): __description__ = "Adds support for etc-proposals, a graphical etc-update replacement." __dependency__ = ["app-portage/etc-proposals"] - def __init__ (self): - Plugin.__init__(self) - + def init (self): self.prog = ["/usr/sbin/etc-proposals"] self.add_call("after_emerge", self.hook, type = "after") self.add_menu("Et_c-Proposals", self.menu) diff --git a/plugins/gpytage.py b/plugins/gpytage.py index 33509e1..d8c2831 100644 --- a/plugins/gpytage.py +++ b/plugins/gpytage.py @@ -17,8 +17,7 @@ class GPytage (Plugin): __description__ = "Adds a menu entry to directly start gpytage, a config editor." __dependency__ = ["app-portage/gpytage"] - def __init__ (self): - Plugin.__init__(self) + def init (self): self.add_menu("Config _Editor", self.menu) def menu (self, *args): diff --git a/plugins/notify.py b/plugins/notify.py index bc1b2ea..6446812 100644 --- a/plugins/notify.py +++ b/plugins/notify.py @@ -10,7 +10,12 @@ # # Written by René 'Necoro' Neumann -import pynotify +disable = False + +try: + import pynotify +except ImportError: + disable = True from portato import get_listener @@ -22,8 +27,7 @@ class Notify (Plugin): __description__ = "Show notifications when an emerge process finishes." __dependency__ = ["dev-python/notify-python"] - def __init__ (self): - Plugin.__init__(self) + def init (self): self.add_call("after_emerge", self.notify) def notify (self, retcode, **kwargs): @@ -42,4 +46,4 @@ class Notify (Plugin): get_listener().send_notify(base = text, descr = descr, icon = icon, urgency = urgency) -register(Notify) +register(Notify, disable) diff --git a/portato/gui/windows/main.py b/portato/gui/windows/main.py index b4e6353..479274d 100644 --- a/portato/gui/windows/main.py +++ b/portato/gui/windows/main.py @@ -1082,6 +1082,9 @@ class MainWindow (Window): def save_plugin (p): def _save (): + if p.status == p.STAT_HARD_DISABLED: + return "" + return int(p.status >= p.STAT_ENABLED) return _save diff --git a/portato/plugin.py b/portato/plugin.py index 7b8a493..ada7a0f 100644 --- a/portato/plugin.py +++ b/portato/plugin.py @@ -19,6 +19,7 @@ from functools import wraps from .helper import debug, warning, info, error from .constants import PLUGIN_DIR +from .backend import system from . import plugins as plugin_module class PluginLoadException (Exception): @@ -52,11 +53,30 @@ class Hook (object): class Plugin (object): (STAT_DISABLED, STAT_TEMP_ENABLED, STAT_ENABLED, STAT_TEMP_DISABLED) = range(4) + STAT_HARD_DISABLED = -1 - def __init__ (self): + def __init__ (self, disable = False): self.__menus = [] self.__calls = [] - self.status = self.STAT_ENABLED + self._unresolved_deps = False + + if not disable: + self.status = self.STAT_ENABLED + else: + self.status = self.STAT_HARD_DISABLED + + def _init (self): + + for d in self.deps: + if not system.find_packages(d, pkgSet="installed", with_version = False): + self._unresolved_deps = True + break + + if self.status != self.STAT_HARD_DISABLED and not self._unresolved_deps: + self.init() + + def init (self): + pass @property def author (self): @@ -140,8 +160,28 @@ class PluginQueue (object): self._organize() - def add (self, plugin): - self.plugins.append(plugin) + def add (self, plugin, disable = False): + if callable(plugin) and Plugin in plugin.__bases__: + p = plugin(disable = disable) # need an instance and not the class + elif isinstance(plugin, Plugin): + p = plugin + if disable: + p.status = p.STAT_HARD_DISABLED + else: + raise PluginLoadException, "Is neither a subclass nor an instance of Plugin." + + p._init() + + self.plugins.append(p) + + if p.status == p.STAT_HARD_DISABLED: + msg = _("Plugin is disabled!") + elif p._unresolved_deps: + msg = _("Plugin has unresolved dependencies - disabled!") + else: + msg = "" + + info("%s %s", _("Plugin '%s' loaded.") % p.name, msg) def hook (self, hook, *hargs, **hkwargs): @@ -298,14 +338,6 @@ def hook(hook, *args, **kwargs): else: return __plugins.hook(hook, *args, **kwargs) -def register (plugin): +def register (plugin, disable = False): if __plugins is not None: - if callable(plugin) and Plugin in plugin.__bases__: - p = plugin() # need an instance and not the class - elif isinstance(plugin, Plugin): - p = plugin - else: - raise PluginLoadException, "Is neither a subclass nor an instance of Plugin." - - info(_("Plugin '%s' loaded."), p.name) - __plugins.add(p) + __plugins.add(plugin, disable) -- cgit v1.2.3