forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fortune.py
50 lines (36 loc) · 1.12 KB
/
fortune.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
# -*- coding: utf-8 -*-
"""Display random poignant, inspirational, silly or snide phrase.
Fortune wrapper extension.
Synopsis: <trigger>"""
import subprocess as sp
from shutil import which
from albertv0 import *
__iid__ = "PythonInterface/v0.1"
__prettyname__ = "Fortune"
__version__ = "1.0"
__trigger__ = "fortune"
__author__ = "Kelvin Wong"
__dependencies__ = ["fortune"]
cmd = __dependencies__[0]
if which(cmd) is None:
raise Exception("'%s' is not in $PATH." % cmd)
iconPath = iconLookup("font")
def handleQuery(query):
if query.isTriggered:
newFortune = generateFortune()
if newFortune is not None:
return getFortuneItem(query, newFortune)
def generateFortune():
try:
return sp.check_output(["fortune", "-s"]).decode().strip()
except sp.CalledProcessError as e:
return None
def getFortuneItem(query, fortune):
return Item(
id=__prettyname__,
icon=iconPath,
text=fortune,
subtext="Copy this random, hopefully interesting, adage",
completion=query.rawString,
actions=[ClipAction("Copy to clipboard", fortune)]
)