-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
71 lines (60 loc) · 2.81 KB
/
main.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
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
import subprocess
import os
def which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
UNITS_EXISTS = None
class ConverterExtension(Extension):
def __init__(self):
super(ConverterExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
global UNITS_EXISTS
items = []
query = event.get_argument()
if UNITS_EXISTS is None:
if which('units'):
UNITS_EXISTS = True
else:
UNITS_EXISTS = False
if UNITS_EXISTS:
if " to " in query:
start, end = query.split(" to ")
proc = subprocess.Popen(['units', start, end], stdout=subprocess.PIPE)
out, err = proc.communicate()
out = out.decode("UTF-8")
if "Unknown" in out:
items.append(ExtensionResultItem(icon='images/icon.png',
name='Invalid units',
description=out.strip(),
on_enter=HideWindowAction()))
else:
items.append(ExtensionResultItem(icon='images/icon.png',
name='%s %s' % (out.splitlines()[0].split("* ")[-1], end),
#description=out.splitlines()[0].strip(),
on_enter=HideWindowAction()))
else:
items.append(ExtensionResultItem(icon='images/icon.png',
name='Missing library',
description='Please install the "units" package.',
on_enter=HideWindowAction()))
return RenderResultListAction(items)
if __name__ == '__main__':
ConverterExtension().run()