Skip to content

Commit

Permalink
Implement cli vboxmanage and selection mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
luispabon committed Sep 24, 2019
1 parent 3f25b7a commit a26e91f
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions ulauncher_virtualbox/KeywordQueryEventListener.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
import virtualbox
from ulauncher.api.shared.action.RunScriptAction import RunScriptAction
import re
import os


class KeywordQueryEventListener(EventListener):
def have_vboxapi_and_virtualbox(self):
import pkgutil
return pkgutil.find_loader('virtualbox') and pkgutil.find_loader('vboxapi')

def get_machine_info_cli(self):
pass
# Will allow us to capture separately the vm name and id
regex = '^\"(.*)\" {(.*)}$'

# Get list of all boxen and running boxen
vms_raw = os.popen('vboxmanage list vms').read()
running_vms_raw = os.popen('vboxmanage list runningvms').read()

vms = vms_raw.strip('\n').split('\n')

items = []
for vm in vms:
match = re.match(regex, vm)

vm_name = match.group(1)
vm_id = match.group(2)

items.append({
'id': vm_id,
'name': vm_name,
'description': 'State: running' if vm_id in running_vms_raw else 'State: stopped'
})

return items

def get_machine_info_vboxapi(self):
import virtualbox

vbox = virtualbox.VirtualBox()
items = []

Expand All @@ -27,11 +56,13 @@ def get_machine_info_vboxapi(self):

return items


def on_event(self, event, extension):
vbox_exec = extension.preferences.get('vbox_exec')

machines = self.get_machine_info_vboxapi()
machines = self.get_machine_info_vboxapi() \
if self.have_vboxapi_and_virtualbox() \
else self.get_machine_info_cli()

items = []
for machine in machines:
# There doesn't seem to be any way at the moment to run custom python code as an action
Expand All @@ -46,30 +77,4 @@ def on_event(self, event, extension):
on_enter=RunScriptAction(command)
))

#
#
# vbox = virtualbox.VirtualBox()
# items = []
#
# vbox_exec = extension.preferences.get('vbox_exec')
#
# for machine in vbox.machines:
# description = \
# 'State: ' + str(machine.state) \
# + '; OS: ' + machine.os_type_id \
# + '; CPUs: ' + str(machine.cpu_count) \
# + '; RAM: ' + str(machine.memory_size) + 'MB'
#
# # There doesn't seem to be any way at the moment to run custom python code as an action
# # Also, virtualbox does not yet support wayland, so make sure it starts as X
# # Even more also, can't use f strings cause I can't assure py 3.6 is available
# command = 'QT_QPA_PLATFORM=xcb ' + vbox_exec + ' --startvm "' + machine.id_p + '"'
#
# items.append(ExtensionResultItem(
# icon='images/icon.png',
# name=machine.name,
# description=description,
# on_enter=RunScriptAction(command)
# ))

return RenderResultListAction(items)

0 comments on commit a26e91f

Please sign in to comment.