[kupfer: 1/2] virtualbox: lazy loading and unloading supporting modules
- From: Ulrik Sverdrup <usverdrup src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [kupfer: 1/2] virtualbox: lazy loading and unloading supporting modules
- Date: Mon, 7 Mar 2011 16:16:33 +0000 (UTC)
commit 23ef5ab4e2a44a846917e2a40fbb88eb43d1b106
Author: Karol BÄ?dkowski <karol bedkowski gmail com>
Date: Sun Mar 6 22:26:39 2011 +0100
virtualbox: lazy loading and unloading supporting modules
Supporting modules are loading when necessary (at first use).
After change configuration ("force cli" setting) current module is removed
from sys.modules.
kupfer/plugin/virtualbox/__init__.py | 69 ++++++++++++++++++++------
kupfer/plugin/virtualbox/ose_support.py | 4 ++
kupfer/plugin/virtualbox/vboxapi4_support.py | 13 +++--
kupfer/plugin/virtualbox/vboxapi_support.py | 14 ++++--
4 files changed, 76 insertions(+), 24 deletions(-)
---
diff --git a/kupfer/plugin/virtualbox/__init__.py b/kupfer/plugin/virtualbox/__init__.py
index 16c1e2f..a2c695c 100644
--- a/kupfer/plugin/virtualbox/__init__.py
+++ b/kupfer/plugin/virtualbox/__init__.py
@@ -7,22 +7,13 @@ __description__ = _("Control VirtualBox Virtual Machines. "
__version__ = "0.3"
__author__ = "Karol BÄ?dkowski <karol bedkowski gmail com>"
+import sys
+
from kupfer.objects import Leaf, Action, Source
from kupfer import pretty
from kupfer import plugin_support
from kupfer.obj.apps import ApplicationSource
-try:
- try:
- from kupfer.plugin.virtualbox import vboxapi4_support as vboxapi_support
- pretty.print_info(__name__, 'Using vboxapi4...')
- except ImportError, err:
- from kupfer.plugin.virtualbox import vboxapi_support
- pretty.print_info(__name__, 'Using vboxapi...')
-except ImportError, err:
- pretty.print_info(__name__, 'vboxapi not available...', err)
- vboxapi_support = None
-
from kupfer.plugin.virtualbox import ose_support
from kupfer.plugin.virtualbox import constants as vbox_const
@@ -37,12 +28,49 @@ __kupfer_settings__ = plugin_support.PluginSettings(
)
+def _get_vbox():
+ if __kupfer_settings__['force_cli']:
+ pretty.print_info(__name__, 'Using cli...')
+ return ose_support
+ try:
+ from kupfer.plugin.virtualbox import vboxapi4_support
+ pretty.print_info(__name__, 'Using vboxapi4...')
+ return vboxapi4_support
+ except ImportError:
+ pass
+ try:
+ from kupfer.plugin.virtualbox import vboxapi_support
+ pretty.print_info(__name__, 'Using vboxapi...')
+ return vboxapi_support
+ except ImportError:
+ pass
+ pretty.print_info(__name__, 'Using cli...')
+ return ose_support
+
+
class _VBoxSupportProxy:
+ VBOX = None
+
def __getattr__(self, attr):
- vbox = ose_support
- if vboxapi_support and not __kupfer_settings__['force_cli']:
- vbox = vboxapi_support
- return getattr(vbox, attr)
+ if not self.VBOX:
+ self.reload_settings()
+ return getattr(self.VBOX, attr)
+
+ def reload_settings(self):
+ pretty.print_debug(__name__, '_VBoxSupportProxy.reloading...')
+ self.unload_module()
+ self.VBOX = _get_vbox()
+
+ def unload_module(self):
+ if not self.VBOX:
+ return
+ # remove module
+ self.VBOX.unload()
+ for module_name, module in sys.modules.iteritems():
+ if module == self.VBOX:
+ sys.modules.pop(module_name)
+ break
+ self.VBOX = None
vbox_support = _VBoxSupportProxy()
@@ -105,7 +133,7 @@ class VMAction(Action):
class VBoxMachinesSource(ApplicationSource):
- appleaf_content_id = vbox_support.APP_ID
+ appleaf_content_id = ("virtualbox-ose", "virtualbox")
def __init__(self, name=_("VirtualBox Machines")):
Source.__init__(self, name)
@@ -114,6 +142,11 @@ class VBoxMachinesSource(ApplicationSource):
if vbox_support.MONITORED_DIRS:
self.monitor_token = self.monitor_directories(
*vbox_support.MONITORED_DIRS)
+ __kupfer_settings__.connect("plugin-setting-changed", self._setting_changed)
+
+ def finalize(self):
+ if vbox_support:
+ vbox_support.unload_module()
def is_dynamic(self):
return vbox_support.IS_DYNAMIC
@@ -130,3 +163,7 @@ class VBoxMachinesSource(ApplicationSource):
def provides(self):
yield VirtualMachine
+
+ def _setting_changed(self, _setting, _key, _value):
+ if vbox_support:
+ vbox_support.reload_settings()
diff --git a/kupfer/plugin/virtualbox/ose_support.py b/kupfer/plugin/virtualbox/ose_support.py
index cdaf696..b1f406b 100644
--- a/kupfer/plugin/virtualbox/ose_support.py
+++ b/kupfer/plugin/virtualbox/ose_support.py
@@ -126,3 +126,7 @@ def get_machines():
name, description = _get_machine_info(vm_uuid, config)
if name:
yield (vm_uuid, name, description)
+
+
+def unload():
+ pass
diff --git a/kupfer/plugin/virtualbox/vboxapi4_support.py b/kupfer/plugin/virtualbox/vboxapi4_support.py
index 85c4a92..29e4ce4 100644
--- a/kupfer/plugin/virtualbox/vboxapi4_support.py
+++ b/kupfer/plugin/virtualbox/vboxapi4_support.py
@@ -6,14 +6,15 @@ Control VirtualBox via Python interface (vboxapi).
Only (?) Sun VirtualBox (no OSE).
'''
__author__ = "Karol BÄ?dkowski <karol bedkowski gmail com>"
-__version__ = "0.3"
+__version__ = "2011-03-06"
-from kupfer import pretty
-
-#raise ImportError()
+import sys
import vboxapi
+from kupfer import pretty
+
+
# check api
try:
vboxapi.VirtualBoxReflectionInfo(None).SessionState_Locked
@@ -152,3 +153,7 @@ def get_machines():
for machine in machines:
description = machine.description or machine.OSTypeId
yield (machine.id, machine.name, description)
+
+
+def unload():
+ sys.modules.pop('vboxapi')
diff --git a/kupfer/plugin/virtualbox/vboxapi_support.py b/kupfer/plugin/virtualbox/vboxapi_support.py
index abea025..fce39f7 100644
--- a/kupfer/plugin/virtualbox/vboxapi_support.py
+++ b/kupfer/plugin/virtualbox/vboxapi_support.py
@@ -6,14 +6,16 @@ Control VirtualBox via Python interface (vboxapi).
Only (?) Sun VirtualBox (no OSE).
'''
__author__ = "Karol BÄ?dkowski <karol bedkowski gmail com>"
-__version__ = "2011-01-20"
+__version__ = "2011-03-06"
-from kupfer import pretty
-#raise ImportError()
+import sys
+
+from kupfer import pretty
import vboxapi
+
from kupfer.plugin.virtualbox import constants as vbox_const
MONITORED_DIRS = None
@@ -77,7 +79,7 @@ def get_machine_state(machine_id):
state = vbox_const.VM_STATE_POWEROFF
elif machine_state == vbox.constants.MachineState_Saved:
state = vbox_const.VM_STATE_SAVED
- except Exception: # exception == machine is off (xpcom.Exception)
+ except Exception, err: # exception == machine is off (xpcom.Exception)
# silently set state to off
state = vbox_const.VM_STATE_POWEROFF
pretty.print_debug(__name__, 'get_machine_state error', err)
@@ -158,3 +160,7 @@ def get_machines():
description = machine.description or machine.OSTypeId
yield (machine.id, machine.name, description)
pretty.print_debug(__name__, 'get_machines finished')
+
+
+def unload():
+ sys.modules.pop('vboxapi')
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]