forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
units.py
62 lines (50 loc) · 1.94 KB
/
units.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
# -*- coding: utf-8 -*-
"""Convert units.
This extension is a wrapper for the (extremely) powerful GNU units tool. Note that spaces are \
interpreted as separators, i.e. dont use spaces between numbers and units.
Synopsis:
<trigger> <src> [dst]
<src> to <dst>"""
import re
import subprocess as sp
from shutil import which
from albertv0 import *
__iid__ = "PythonInterface/v0.1"
__prettyname__ = "GNU Units"
__version__ = "1.1"
__trigger__ = "units "
__author__ = "Manuel Schneider"
__dependencies__ = ["units"]
if which("units") is None:
raise Exception("'units' is not in $PATH.")
icon = iconLookup('calc')
if not icon:
icon = ":python_module"
regex = re.compile(r"(\S+)(?:\s+to)\s+(\S+)")
def handleQuery(query):
if query.isTriggered:
args = query.string.split()
item = Item(id='python.gnu_units', icon=icon, completion=query.rawString)
if args:
try:
item.text = sp.check_output(['units', '-t'] + query.string.split(), stderr=sp.STDOUT).decode().strip()
item.addAction(ClipAction("Copy to clipboard", item.text))
except sp.CalledProcessError as e:
item.text = e.stdout.decode().strip().partition('\n')[0]
item.subtext = "Result of 'units -t %s'" % query.string
else:
item.text = "Empty input"
item.subtext = "Enter something to convert"
return item
else:
match = regex.fullmatch(query.string.strip())
if match:
args = match.group(1, 2)
try:
item = Item(id='python.gnu_units', icon=icon, completion=query.rawString)
item.text = sp.check_output(['units', '-t'] + list(args)).decode().strip()
item.subtext = "Result of 'units -t %s %s'" % args
item.addAction(ClipAction("Copy to clipboard", item.text))
return item
except sp.CalledProcessError as e:
pass