-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKeywordQueryEventListener.py
88 lines (67 loc) · 2.99 KB
/
KeywordQueryEventListener.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
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):
# 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 = []
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'
items.append({
'id': machine.id_p,
'name': machine.name,
'description': description,
})
return items
def on_event(self, event, extension):
# search term
search_term = (event.get_query().get_argument() or "")
vbox_exec = extension.preferences.get('vbox_exec')
machines = self.get_machine_info_vboxapi() \
if self.have_vboxapi_and_virtualbox() \
else self.get_machine_info_cli()
items = []
for machine in machines:
# filter machine based on search term
if not search_term.lower() in str(machine['name']).lower():
continue
# 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'] + '"'
items.append(ExtensionResultItem(
icon='images/icon.png',
name=machine['name'],
description=machine['description'],
on_enter=RunScriptAction(command)
))
return RenderResultListAction(items)