Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
68 lines (55 sloc) 2.03 KB
from io import StringIO
import contextlib
import sys
import time
import subprocess
import webbrowser
from codelab_adapter.core_extension import Extension
from codelab_adapter.utils import verify_token
from codelab_adapter.settings import TOKEN
# 安全性原则: 打开这个插件前,提醒社区用户确认积木中没有危险的Python代码, 允许社区成员举报危险代码
# 在EIM中运行Python代码,通用
class PyHelper:
def open_url(self, url):
def mac_open(self, path):
open_cmd = "/usr/bin/open" # which open"{open_cmd} {path}", shell=True)
class PythonKernelExtension(Extension):
def __init__(self):
self.EXTENSION_ID = "eim/python"
self.PyHelper = PyHelper()
def stdoutIO(self, stdout=None):
old = sys.stdout
if stdout is None:
stdout = StringIO()
sys.stdout = stdout
yield stdout
sys.stdout = old
def extension_message_handle(self, topic, payload):
''''python code: {payload["content"]}')
message_id = payload.get("message_id")
python_code = payload["content"]
# 出于安全考虑, 放弃使用exec,如果需要,可以自行下载exec版本
# eval(expression, globals=None, locals=None)
# 如果只是调用(插件指责)可以使用json-rpc
output = eval(python_code, {"__builtins__": None}, {
"PyHelper": self.PyHelper,
except Exception as e:
output = e
payload["content"] = str(output)
message = {"payload": payload} # 无论是否有message_id都返回
def run(self):
while self._running:
export = PythonKernelExtension
You can’t perform that action at this time.