forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
49 lines (40 loc) · 1.58 KB
/
__init__.py
File metadata and controls
49 lines (40 loc) · 1.58 KB
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
# -*- coding: utf-8 -*-
from builtins import pow
from math import *
from pathlib import Path
from albert import *
md_iid = '2.0'
md_version = "1.5"
md_name = "Python Eval"
md_description = "Evaluate Python code"
md_license = "BSD-3"
md_url = "https://github.com/albertlauncher/python/tree/master/python_eval"
class Plugin(PluginInstance, TriggerQueryHandler):
def __init__(self):
TriggerQueryHandler.__init__(self,
id=md_id,
name=md_name,
description=md_description,
synopsis='<Python expression>',
defaultTrigger='py ')
PluginInstance.__init__(self, extensions=[self])
self.iconUrls = [f"file:{Path(__file__).parent}/python.svg"]
def handleTriggerQuery(self, query):
stripped = query.string.strip()
if stripped:
try:
result = eval(stripped)
except Exception as ex:
result = ex
result_str = str(result)
query.add(StandardItem(
id=md_id,
text=result_str,
subtext=type(result).__name__,
inputActionText=query.trigger + result_str,
iconUrls=self.iconUrls,
actions = [
Action("copy", "Copy result to clipboard", lambda r=result_str: setClipboardText(r)),
Action("exec", "Execute python code", lambda r=result_str: exec(stripped)),
]
))