forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathematica_eval.py
45 lines (34 loc) · 1.18 KB
/
mathematica_eval.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
# -*- coding: utf-8 -*-
"""Evaluate Mathematica expressions.
Synopsis: <trigger> [expr]"""
import subprocess
from shutil import which
from tempfile import NamedTemporaryFile
from albertv0 import ClipAction, Item, iconLookup
__iid__ = 'PythonInterface/v0.1'
__prettyname__ = 'Mathematica eval'
__version__ = '1.0'
__trigger__ = 'mma '
__author__ = 'Asger Hautop Drewsen'
__dependencies__ = ['mathematica']
if not which('wolframscript'):
raise Exception("`wolframscript` is not in $PATH.")
ICON_PATH = iconLookup('wolfram-mathematica')
def handleQuery(query):
if not query.isTriggered:
return
item = Item(completion=query.rawString, icon=ICON_PATH)
stripped = query.string.strip()
if stripped:
with NamedTemporaryFile() as f:
f.write(bytes(stripped, 'utf-8'))
f.flush()
output = subprocess.check_output(['wolframscript', '-print', '-f', f.name])
result = str(output.strip(), 'utf-8')
item.text = result
item.subtext = 'Result'
item.addAction(ClipAction('Copy result to clipboard', result))
else:
item.text = ''
item.subtext = 'Type a Mathematica expression'
return item