[kupfer] plugin.virtualbox: Import without catching ImportError
- From: Ulrik Sverdrup <usverdrup src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [kupfer] plugin.virtualbox: Import without catching ImportError
- Date: Sun, 11 Oct 2009 20:39:06 +0000 (UTC)
commit c4b992331cabc377e59e8e434a491225ba8fc7c8
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date: Thu Oct 8 22:17:35 2009 +0200
plugin.virtualbox: Import without catching ImportError
Generally, kupfer's Manual.rst says this:
The plugin should not do **anything at all** upon module load,
except loading its required modules. Load modules without
try/except; ImportErrors will be caught by the plugin loader and
the plugin disabled
This makes the plugin much simpler to implement. However the drawback
until now has been that the plugin would not even show up in the
plugin list. Now with some experimental changes I inject a FakePlugin
object if we encounter an ImportError when loading a plugin.
The FakePlugin can read in the plugin name and description to be shown
in the plugin info, and the error will be displayed prominently.
This means that plugins can now use bare imports, without catching
ImportError, but they have to do this after definining the special
kupfer plugin attributes, so that those lines can be executed before
the exception is raised.
kupfer/plugin/virtualbox.py | 34 ++++++++++++++--------------------
1 files changed, 14 insertions(+), 20 deletions(-)
---
diff --git a/kupfer/plugin/virtualbox.py b/kupfer/plugin/virtualbox.py
index f1bba4b..700bde9 100644
--- a/kupfer/plugin/virtualbox.py
+++ b/kupfer/plugin/virtualbox.py
@@ -7,26 +7,6 @@ from kupfer.objects import Leaf, Action, Source, AppLeafContentMixin
from kupfer.helplib import FilesystemWatchMixin
from kupfer import pretty, plugin_support
-# try import vboxapi
-try:
- import vboxapi
-except ImportError:
- pretty.print_error('No vboxapi found...')
- vboxapi = None
-
-# try to load xpcom from vbox sdk
-if vboxapi:
- xpcom_path = vboxapi.VboxSdkDir+'/bindings/xpcom/python/'
- xpcom_Exception = Exception
- if os.path.isdir(xpcom_path):
- sys.path.append(xpcom_path)
- try:
- import xpcom
- except ImportError:
- pretty.print_error('Cannot import xpcom')
- else:
- xpcom_Exception = xpcom.Exception
-
__kupfer_name__ = _("VirtualBox")
__kupfer_sources__ = ("VBoxMachinesSource", )
__description__ = _("Control Sun VirtualBox Virtual Machines")
@@ -36,6 +16,20 @@ __kupfer_settings__ = plugin_support.PluginSettings(
plugin_support.SETTING_PREFER_CATALOG,
)
+import vboxapi
+
+# try to load xpcom from vbox sdk
+xpcom_path = vboxapi.VboxSdkDir+'/bindings/xpcom/python/'
+xpcom_Exception = Exception
+if os.path.isdir(xpcom_path):
+ sys.path.append(xpcom_path)
+ try:
+ import xpcom
+ except ImportError:
+ pretty.print_error('Cannot import xpcom')
+ else:
+ xpcom_Exception = xpcom.Exception
+
VM_POWEROFF = 0
VM_POWERON = 1
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]