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
50 lines (39 loc) · 1.43 KB
/
__init__.py
File metadata and controls
50 lines (39 loc) · 1.43 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
50
# -*- coding: utf-8 -*-
# Copyright (c) 2017-2014 Manuel Schneider
from pathlib import Path
from albert import *
md_iid = "3.0"
md_version = "2.0"
md_name = "Python Eval"
md_description = "Evaluate Python code"
md_license = "BSD-3"
md_url = "https://github.com/albertlauncher/python/tree/main/python_eval"
md_authors = "@manuelschneid3r"
class Plugin(PluginInstance, TriggerQueryHandler):
def __init__(self):
PluginInstance.__init__(self)
TriggerQueryHandler.__init__(self)
self.iconUrls = [f"file:{Path(__file__).parent}/python.svg"]
def synopsis(self, query):
return "<Python expression>"
def defaultTrigger(self):
return "py "
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=self.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)),
]
))