[kupfer: 7/11] plugin.virtualbox: cleanup code



commit 9065fefaaef47e77c95df1d9060bbd9ea2e71644
Author: Karol BÄ?dkowski <karol bedkowsk+gh gmail com>
Date:   Thu Oct 22 13:27:38 2009 +0200

    plugin.virtualbox: cleanup code
    
    - change virtualbox_const.py -> virtualbox_const_support.py
    - wrap long lines

 kupfer/plugin/virtualbox.py                        |   64 ++++++++++---------
 ...ualbox_const.py => virtualbox_const_support.py} |    6 +-
 kupfer/plugin/virtualbox_ose_support.py            |   31 +++++-----
 kupfer/plugin/virtualbox_vboxapi_support.py        |   42 +++++++-------
 4 files changed, 74 insertions(+), 69 deletions(-)
---
diff --git a/kupfer/plugin/virtualbox.py b/kupfer/plugin/virtualbox.py
index de59b94..626252f 100644
--- a/kupfer/plugin/virtualbox.py
+++ b/kupfer/plugin/virtualbox.py
@@ -6,22 +6,22 @@ from kupfer import pretty, plugin_support
 
 __kupfer_name__ = _("VirtualBox")
 __kupfer_sources__ = ("VBoxMachinesSource", )
-__description__ = _("Control Sun VirtualBox Virtual Machines. Support also VirtualBox OpenSource Edition.")
-__version__ = "0.2"
+__description__ = _("Control Sun VirtualBox Virtual Machines. "
+					"Support also VirtualBox OpenSource Edition.")
+__version__ = "0.3"
 __author__ = "Karol BÄ?dkowski <karol bedkowski gmail com>"
 __kupfer_settings__ = plugin_support.PluginSettings(
 		plugin_support.SETTING_PREFER_CATALOG,
 )
 
-
 try:
-	import virtualbox_vboxapi_support as virtualbox_support
+	import virtualbox_vboxapi_support as vbox_support
 	pretty.print_info(__name__, 'Using vboxapi...')
 except ImportError, err:
-	import virtualbox_ose_support as virtualbox_support
+	import virtualbox_ose_support as vbox_support
 	pretty.print_info(__name__, 'Using cli...', err)
 
-import virtualbox_const
+import virtualbox_const_support as vbox_const
 
 
 class VirtualMachine(Leaf):
@@ -33,27 +33,29 @@ class VirtualMachine(Leaf):
 		return self.description
 
 	def get_icon_name(self):
-		return virtualbox_support.ICON
+		return vbox_support.ICON
 
 	def get_actions(self):
-		state = virtualbox_support.get_machine_state(self.object)
-		if state == virtualbox_const.VM_STATE_POWEROFF:
-			yield VMAction(_('Power On'), 'system-run', \
-					virtualbox_const.VM_START_NORMAL)
-			yield VMAction(_('Power On Headless'), 'system-run', \
-					virtualbox_const.VM_START_HEADLESS, -5)
-		elif state == virtualbox_const.VM_STATE_POWERON:
-			yield VMAction(_('Send Power Off Signal'), 'system-shutdown', \
-					virtualbox_const.VM_ACPI_POWEROFF, -5)
-			yield VMAction(_('Pause'), 'pause', virtualbox_const.VM_PAUSE)
-			yield VMAction(_('Reboot'), 'system-reboot', virtualbox_const.VM_REBOOT, -10)
+		state = vbox_support.get_machine_state(self.object)
+		if state == vbox_const.VM_STATE_POWEROFF:
+			yield VMAction(_('Power On'), 'system-run',
+					vbox_const.VM_START_NORMAL)
+			yield VMAction(_('Power On Headless'), 'system-run',
+					vbox_const.VM_START_HEADLESS, -5)
+		elif state == vbox_const.VM_STATE_POWERON:
+			yield VMAction(_('Send Power Off Signal'), 'system-shutdown',
+					vbox_const.VM_ACPI_POWEROFF, -5)
+			yield VMAction(_('Pause'), 'pause', vbox_const.VM_PAUSE)
+			yield VMAction(_('Reboot'), 'system-reboot', 
+					vbox_const.VM_REBOOT, -10)
 		else: # VM_STATE_PAUSED
-			yield VMAction(_('Resume'), 'resume', virtualbox_const.VM_RESUME)
+			yield VMAction(_('Resume'), 'resume', vbox_const.VM_RESUME)
 
-		if state in (virtualbox_const.VM_STATE_POWERON, virtualbox_const.VM_STATE_PAUSED):
-			yield VMAction(_('Save State'), 'system-supsend', virtualbox_const.VM_SAVE)
-			yield VMAction(_('Power Off'), 'system-shutdown', \
-					virtualbox_const.VM_POWEROFF, -10)
+		if state in (vbox_const.VM_STATE_POWERON, vbox_const.VM_STATE_PAUSED):
+			yield VMAction(_('Save State'), 'system-supsend',
+					vbox_const.VM_SAVE)
+			yield VMAction(_('Power Off'), 'system-shutdown',
+					vbox_const.VM_POWEROFF, -10)
 
 
 class VMAction(Action):
@@ -70,10 +72,11 @@ class VMAction(Action):
 		yield VirtualMachine
 
 	def activate(self, leaf):
-		virtualbox_support.vm_action(self.command, leaf.object)
+		vbox_support.vm_action(self.command, leaf.object)
 
 
-class VBoxMachinesSource(AppLeafContentMixin, Source, PicklingHelperMixin, FilesystemWatchMixin):
+class VBoxMachinesSource(AppLeafContentMixin, Source, PicklingHelperMixin, 
+		FilesystemWatchMixin):
 	appleaf_content_id = ('VirtualBox OSE', 'Sun VirtualBox')
 
 	def __init__(self, name=_("Sun VirtualBox Machines")):
@@ -81,21 +84,22 @@ class VBoxMachinesSource(AppLeafContentMixin, Source, PicklingHelperMixin, Files
 		self.unpickle_finish()
 
 	def unpickle_finish(self):
-		if virtualbox_support.MONITORED_DIRS:
-			self.monitor_token = self.monitor_directories(*virtualbox_support.MONITORED_DIRS)
+		if vbox_support.MONITORED_DIRS:
+			self.monitor_token = self.monitor_directories(
+					*vbox_support.MONITORED_DIRS)
 
 	def is_dynamic(self):
-		return virtualbox_support.IS_DYNAMIC
+		return vbox_support.IS_DYNAMIC
 
 	def get_items(self):
-		for machine_id, machine_name, machine_desc in virtualbox_support.get_machines():
+		for machine_id, machine_name, machine_desc in vbox_support.get_machines():
 			yield VirtualMachine(machine_id, machine_name, machine_desc)
 
 	def get_description(self):
 		return None
 
 	def get_icon_name(self):
-		return virtualbox_support.ICON
+		return vbox_support.ICON
 
 	def provides(self):
 		yield VirtualMachine
diff --git a/kupfer/plugin/virtualbox_const.py b/kupfer/plugin/virtualbox_const_support.py
similarity index 82%
rename from kupfer/plugin/virtualbox_const.py
rename to kupfer/plugin/virtualbox_const_support.py
index 3ace381..5f85c6b 100644
--- a/kupfer/plugin/virtualbox_const.py
+++ b/kupfer/plugin/virtualbox_const_support.py
@@ -1,11 +1,11 @@
 # -*- coding: UTF-8 -*-
 '''
-virtualbox_support.py
+virtualbox_const_support.py
 
-Constants for VirtualBox
+Constants for VirtualBox.
 '''
 __author__ = "Karol BÄ?dkowski <karol bedkowski gmail com>"
-__revision__ = '0.1'
+__version__ = '0.3'
 
 # virtual machine states
 VM_STATE_POWEROFF = 0
diff --git a/kupfer/plugin/virtualbox_ose_support.py b/kupfer/plugin/virtualbox_ose_support.py
index f8d9cb4..2e1ca44 100644
--- a/kupfer/plugin/virtualbox_ose_support.py
+++ b/kupfer/plugin/virtualbox_ose_support.py
@@ -7,13 +7,13 @@ Support both Sun VirtualBox and VirtualBox OpenSource Edition.
 '''
 from __future__ import with_statement
 
-__revision__ = "0.1"
 __author__ = "Karol BÄ?dkowski <karol bedkowski gmail com>"
+__version__ = '0.3'
 
 import os
 from xml.dom import minidom
 from kupfer import pretty, utils
-import virtualbox_const
+import virtualbox_const_support as vbox_const
 
 _VBOX_CONFIG_DIR = os.path.expanduser('~/.VirtualBox/')
 _VBOX_CONFIG_FILE = os.path.join(_VBOX_CONFIG_DIR, 'VirtualBox.xml')
@@ -24,18 +24,18 @@ ICON = "virtualbox-ose"
 
 # parameters for VBoxManage
 _ACTIONS = {
-		virtualbox_const.VM_POWEROFF: 'poweroff',
-		virtualbox_const.VM_ACPI_POWEROFF: 'acpipowerbutton',
-		virtualbox_const.VM_PAUSE: 'pause',
-		virtualbox_const.VM_REBOOT: 'reset',
-		virtualbox_const.VM_RESUME: 'resume',
-		virtualbox_const.VM_SAVE: 'savestate'
+		vbox_const.VM_POWEROFF: 'poweroff',
+		vbox_const.VM_ACPI_POWEROFF: 'acpipowerbutton',
+		vbox_const.VM_PAUSE: 'pause',
+		vbox_const.VM_REBOOT: 'reset',
+		vbox_const.VM_RESUME: 'resume',
+		vbox_const.VM_SAVE: 'savestate'
 }
 
 
 def get_machine_state(vm_uuid):
 	''' check vms state (on/off/paused) '''
-	state = virtualbox_const.VM_STATE_POWEROFF
+	state = vbox_const.VM_STATE_POWEROFF
 	try:
 		str_state = 'poweroff'
 		with os.popen('VBoxManage showvminfo %s --machinereadable' % vm_uuid) \
@@ -45,13 +45,13 @@ def get_machine_state(vm_uuid):
 					str_state = line.strip()[9:-1]
 					break
 		if str_state == 'paused':
-			state = virtualbox_const.VM_STATE_PAUSED
+			state = vbox_const.VM_STATE_PAUSED
 		elif str_state == 'running':
-			state = virtualbox_const.VM_STATE_POWERON
+			state = vbox_const.VM_STATE_POWERON
 
 	except IOError, err:
 		pretty.print_error(__name__, 'get_machine_state', vm_uuid, 'error', err)
-		state = virtualbox_const.VM_STATE_POWEROFF
+		state = vbox_const.VM_STATE_POWEROFF
 
 	return state
 
@@ -61,10 +61,10 @@ def vm_action(action, vm_uuid):
 		@param action - one of the const VM_*
 		@param vm_uuid - virtual machine uuid
 	'''
-	if action == virtualbox_const.VM_START_NORMAL:
+	if action == vbox_const.VM_START_NORMAL:
 		utils.launch_commandline('VBoxManage startvm ' + vm_uuid + \
 				' --type gui')
-	elif action == virtualbox_const.VM_START_HEADLESS:
+	elif action == vbox_const.VM_START_HEADLESS:
 		utils.launch_commandline('VBoxManage startvm ' + vm_uuid + \
 				' --type headless')
 	else:
@@ -82,7 +82,8 @@ def _get_virtual_machines(config_file):
 		dtree = minidom.parse(config_file)
 		machine_registry = dtree.getElementsByTagName('MachineRegistry')[0]
 		for machine in machine_registry.getElementsByTagName('MachineEntry'):
-			yield (machine.getAttribute('uuid')[1:-1], machine.getAttribute('src'))
+			yield (machine.getAttribute('uuid')[1:-1], 
+					machine.getAttribute('src'))
 
 	except StandardError, err:
 		pretty.print_error(__name__, '_get_virtual_machines', config_file, 
diff --git a/kupfer/plugin/virtualbox_vboxapi_support.py b/kupfer/plugin/virtualbox_vboxapi_support.py
index f96fa1d..c5b6625 100644
--- a/kupfer/plugin/virtualbox_vboxapi_support.py
+++ b/kupfer/plugin/virtualbox_vboxapi_support.py
@@ -5,8 +5,8 @@ virtualbox_vboxapi_support.py
 Control VirtualBox via Python interface (vboxapi).
 Only (?) Sun VirtualBox (no OSE).
 '''
-__revision__ = "0.1"
 __author__ = "Karol BÄ?dkowski <karol bedkowski gmail com>"
+__version__ = "0.3"
 
 from kupfer import pretty 
 
@@ -14,7 +14,7 @@ from kupfer import pretty
 
 import vboxapi
 
-import virtualbox_const
+import virtualbox_const_support as vbox_const
 
 MONITORED_DIRS = None
 IS_DYNAMIC = False
@@ -22,12 +22,12 @@ ICON = "VBox"
 
 
 _ACTIONS = {
-		virtualbox_const.VM_POWEROFF:		lambda c:c.powerDown(),
-		virtualbox_const.VM_ACPI_POWEROFF:	lambda c:c.powerButton(),
-		virtualbox_const.VM_PAUSE:			lambda c:c.pause(),
-		virtualbox_const.VM_REBOOT:			lambda c:c.reset(),
-		virtualbox_const.VM_RESUME:			lambda c:c.resume(),
-		virtualbox_const.VM_SAVE:			lambda c:c.saveState()
+		vbox_const.VM_POWEROFF:			lambda c:c.powerDown(),
+		vbox_const.VM_ACPI_POWEROFF:	lambda c:c.powerButton(),
+		vbox_const.VM_PAUSE:			lambda c:c.pause(),
+		vbox_const.VM_REBOOT:			lambda c:c.reset(),
+		vbox_const.VM_RESUME:			lambda c:c.resume(),
+		vbox_const.VM_SAVE:				lambda c:c.saveState()
 }
 
 
@@ -50,8 +50,8 @@ def _get_existing_session(vm_uuid):
 		session = vbox.mgr.getSessionObject(vbox.vbox)
 		vbox.vbox.openExistingSession(session, vm_uuid)
 	except Exception, err:
-		pretty.print_error(__name__, 'virtualbox: get session to %s error' % \
-				vm_uuid, err)
+		pretty.print_error(__name__, 'virtualbox: get session error', vm_uuid,
+				err)
 
 	return vbox, session
 
@@ -60,21 +60,21 @@ def get_machine_state(machine_id):
 	
 	vbox, vbox_sess = _get_object_session()
 	if vbox_sess is None:
-		return virtualbox_const.VM_STATE_POWEROFF
+		return vbox_const.VM_STATE_POWEROFF
 
-	state = virtualbox_const.VM_STATE_POWERON
+	state = vbox_const.VM_STATE_POWERON
 	try:
 		vbox.vbox.openExistingSession(vbox_sess, machine_id)
 		machine_state = vbox_sess.machine.state
 		if machine_state == vbox.constants.MachineState_Paused:
-			state = virtualbox_const.VM_STATE_PAUSED
+			state = vbox_const.VM_STATE_PAUSED
 		elif machine_state in (vbox.constants.MachineState_PoweredOff, 
 				vbox.constants.MachineState_Aborted,
 				vbox.constants.MachineState_Starting):
-			state = virtualbox_const.VM_STATE_POWEROFF
+			state = vbox_const.VM_STATE_POWEROFF
 	except Exception: # exception == machine is off (xpcom.Exception)
 		# silently set state to off
-		state = virtualbox_const.VM_STATE_POWEROFF
+		state = vbox_const.VM_STATE_POWEROFF
 
 	if vbox_sess.state == vbox.constants.SessionState_Open:
 		vbox_sess.close()
@@ -90,7 +90,8 @@ def _machine_start(vm_uuid, mode):
 	vbox, session = _get_object_session()
 	if session:
 		try:
-			remote_sess = vbox.vbox.openRemoteSession(session, vm_uuid, mode, '')
+			remote_sess = vbox.vbox.openRemoteSession(session, vm_uuid, mode, 
+					'')
 			remote_sess.waitForCompletion(-1)
 		except Exception, err: 
 			pretty.print_error(__name__, "StartVM:", vm_uuid, "Mode ", mode, 
@@ -121,9 +122,9 @@ def vm_action(action, vm_uuid):
 		@param action - one of the const VM_*
 		@param vm_uuid - virtual machine uuid
 	'''
-	if action == virtualbox_const.VM_START_NORMAL:
+	if action == vbox_const.VM_START_NORMAL:
 		_machine_start(vm_uuid, 'gui')
-	elif action == virtualbox_const.VM_START_HEADLESS:
+	elif action == vbox_const.VM_START_HEADLESS:
 		_machine_start(vm_uuid, 'headless')
 	else:
 		command = _ACTIONS[action]
@@ -131,7 +132,8 @@ def vm_action(action, vm_uuid):
 
 
 def get_machines():
-	''' Get generator of items: (machine uuid, machine name, machine description)
+	''' Get generator of items: 
+		(machine uuid, machine name, machine description)
 	'''
 	vbox, vbox_sess = _get_object_session()
 	if vbox_sess is None:
@@ -142,5 +144,3 @@ def get_machines():
 		description = machine.description or machine.OSTypeId
 		yield (machine.id, machine.name, description)
 
-
-



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]