forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Units.py
44 lines (37 loc) · 1.42 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
# -*- coding: utf-8 -*-
"""This extension is an adaptor for the powerful GNU units.
Synopsis: 'units <from> [to]'
Note that spaces are separators."""
from albertv0 import *
import subprocess as sp
from shutil import which
__iid__ = "PythonInterface/v0.1"
__prettyname__ = "GNU Units"
__version__ = "1.0"
__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"
def handleQuery(query):
if query.isTriggered:
args = query.string.split()
item = Item(id='python.gnu_units', icon=icon, completion=query.rawString)
if len(args) < 1:
item.text = 'Enter something to convert'
item.subtext = 'Units takes one or two arguments.'
elif len(args) > 2:
item.text = 'Too many arguments'
item.subtext = 'Units takes one or two arguments.'
else:
try:
item.text = sp.check_output(['units', '-t'] + args,
stderr=sp.STDOUT).decode('utf-8').strip()
except sp.CalledProcessError as e:
item.text = e.stdout.decode('utf-8').strip().partition('\n')[0]
item.subtext = "Result of 'units -t %s'" % query.string
item.addAction(ClipAction("Copy to clipboard", item.text))
return item