Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持执行 python 脚本吗 #146

Closed
cxyxiaokui opened this issue Nov 26, 2024 · 8 comments
Closed

支持执行 python 脚本吗 #146

cxyxiaokui opened this issue Nov 26, 2024 · 8 comments
Labels
enhancement New feature or request

Comments

@cxyxiaokui
Copy link

cxyxiaokui commented Nov 26, 2024

希望可以添加执行 python 脚本的功能

@phodal
Copy link
Owner

phodal commented Nov 27, 2024

支持

@phodal phodal closed this as completed Nov 27, 2024
@cxyxiaokui
Copy link
Author

cxyxiaokui commented Nov 27, 2024

我有一个想法 是想通过shire 发起一个 python 通过 tkinter 弹的输入框提示 然后输入内容在返回的 shire 这个可以实现吗?我自己尝试没有搞定

python 脚本如下

import tkinter as tk
from tkinter import ttk
from tkinter.scrolledtext import ScrolledText

def show_input_dialog():
    # 创建主窗口
    root = tk.Tk()
    root.title("Input Dialog")  # 使用英文标题,保持与IDE一致
    
    # 设置窗口大小和位置
    window_width = 500
    window_height = 320
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
    center_x = int(screen_width/2 - window_width/2)
    center_y = int(screen_height/2 - window_height/2)
    root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
    
    # 设置现代深色主题
    root.configure(bg='#2b2b2b')  # IntelliJ暗色主题背景色
    
    # 配置ttk样式
    style = ttk.Style()
    style.theme_use('clam')  # 使用clam主题作为基础
    
    # 自定义样式
    style.configure('Custom.TFrame', background='#2b2b2b')
    style.configure('Custom.TLabel',
        background='#2b2b2b',
        foreground='#bababa',  # IntelliJ文字颜色
        font=('Segoe UI', 12)
    )
    
    # 扁平化主按钮样式
    style.configure('Primary.TButton',
        background='#4B6EAF',
        foreground='#ffffff',
        padding=(16, 4),  # 减小垂直内边距
        font=('Segoe UI', 11),
        relief='flat',  # 扁平化效果
        borderwidth=0   # 移除边框
    )
    style.map('Primary.TButton',
        background=[('active', '#5881d8')],
        foreground=[('active', '#ffffff')],
        relief=[('pressed', 'flat')]  # 按下时保持扁平
    )
    
    # 扁平化次要按钮样式
    style.configure('Secondary.TButton',
        background='#3C3F41',
        foreground='#bababa',
        padding=(16, 4),  # 减小垂直内边距
        font=('Segoe UI', 11),
        relief='flat',  # 扁平化效果
        borderwidth=0   # 移除边框
    )
    style.map('Secondary.TButton',
        background=[('active', '#494b4d')],
        foreground=[('active', '#ffffff')],
        relief=[('pressed', 'flat')]  # 按下时保持扁平
    )
    
    # 创建主容器
    main_frame = ttk.Frame(root, style='Custom.TFrame')
    main_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=15)
    
    # 创建标签
    label = ttk.Label(
        main_frame,
        text="Enter your content:",
        style='Custom.TLabel'
    )
    label.pack(anchor='w', pady=(0, 10))
    
    # 创建文本框
    text_widget = ScrolledText(
        main_frame,
        width=40,
        height=10,
        font=('JetBrains Mono', 12),  # 使用JetBrains字体
        wrap=tk.WORD,
        bg='#313335',  # 编辑器背景色
        fg='#bababa',  # 文字颜色
        insertbackground='#bababa',  # 光标颜色
        selectbackground='#214283',  # 选中文本背景色
        selectforeground='#ffffff',  # 选中文本前景色
        relief='flat',
        borderwidth=1
    )
    text_widget.pack(fill=tk.BOTH, expand=True, pady=(0, 8))
    
    # 添加默认提示文本
    placeholder = "Type your content here...\n\nSupports multiple lines"
    text_widget.insert('1.0', placeholder)
    text_widget.configure(fg='#666666')  # 提示文本颜色
    
    # 用于存储结果的变量
    result = {'text': None}
    
    # 按钮回调函数
    def on_confirm():
        result['text'] = text_widget.get('1.0', tk.END).strip()
        root.destroy()
    
    def on_cancel():
        result['text'] = None
        root.destroy()
    
    # 清除提示文本的函数
    def clear_placeholder(event):
        if text_widget.get('1.0', tk.END).strip() == placeholder.strip():
            text_widget.delete('1.0', tk.END)
            text_widget.configure(fg='#bababa')
    
    # 恢复提示文本的函数
    def restore_placeholder(event):
        if not text_widget.get('1.0', tk.END).strip():
            text_widget.insert('1.0', placeholder)
            text_widget.configure(fg='#666666')
    
    # 绑定事件
    text_widget.bind('<FocusIn>', clear_placeholder)
    text_widget.bind('<FocusOut>', restore_placeholder)
    
    # 创建按钮框架
    button_frame = ttk.Frame(main_frame, style='Custom.TFrame')
    button_frame.pack(fill=tk.X, pady=(0, 2))
    
    # 创建按钮
    cancel_btn = ttk.Button(
        button_frame,
        text="Cancel",
        style='Secondary.TButton',
        command=on_cancel
    )
    cancel_btn.pack(side=tk.RIGHT, padx=(4, 0))
    
    confirm_btn = ttk.Button(
        button_frame,
        text="Commit",
        style='Primary.TButton',
        command=on_confirm
    )
    confirm_btn.pack(side=tk.RIGHT)
    
    # 设置窗口为模态
    root.transient()
    root.grab_set()
    
    # 绑定ESC键为取消
    root.bind('<Escape>', lambda e: on_cancel())
    # 绑定回车键为确认
    root.bind('<Return>', lambda e: on_confirm())
    
    root.mainloop()
    
    return result['text']

if __name__ == "__main__":
    result = show_input_dialog()
    if result:
        print(f"User input:\n{result}")
    else:
        print("User cancelled the input")

@cxyxiaokui
Copy link
Author

可以给出一个能执行 python 的 测试案例吗? 我参照js 使用py 不行

@phodal phodal reopened this Nov 27, 2024
@phodal
Copy link
Owner

phodal commented Nov 27, 2024

使用的是 thread 还是 FFI ? Ops,当前应该是有 Bug,如果是出现以下的问题:

Prepare for running python-thread.shire...
Shire Script: /Users/phodal/IdeaProjects/shire-demo/.shire/ffi/python-thread.shire
Shire Script Compile output:
hello, No run service found for PyFile:hello.py, .shire/ffi/hello.py

--------------------
你好!根据你提供的信息,似乎你在尝试运行一个名为 `hello.py` 的 Python 文件,但系统提示找不到运行服务。这可能是由于以下几个原因:

1. **文件路径错误**:确

@phodal
Copy link
Owner

phodal commented Nov 27, 2024

测试了一个,应该是之前 IDEA 的限制,限制了 Python PSI 和模块的调用。现在一个比较简单的折中办法是,执行 shell,在 shell 里调用 Python

---
name: "hello"
variables:
  "var2": /.*ple.shire/ { thread(".shire/ffi/run-python.sh") }
---

hello, $var2

对应的 .sh

#!/bin/bash

# 检查是否安装了Python
if ! command -v python3 &> /dev/null
then
    echo "Python is not installed. Please install Python before running this script."
    exit
fi

# 运行hello.py脚本
python3 hello.py

@cxyxiaokui
Copy link
Author

可以了非常感谢支持

@phodal phodal added the enhancement New feature or request label Nov 28, 2024
@phodal
Copy link
Owner

phodal commented Nov 29, 2024

https://youtrack.jetbrains.com/issue/PY-70729/Class-com.jetbrains.python.psi.PyElementType-must-not-be-requested-from-main-classloader-of-Pythonid-plugin

Prepare for running python-shell-thread.shire...
Failed to run python-shell-thread.shire: Class com.jetbrains.python.psi.PyFile must not be requested from main classloader of Pythonid plugin. Matches content module (packagePrefix=com.jetbrains.python., moduleName=intellij.python.community.impl). [Plugin: com.phodal.shire]

243 版本问题:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Do not use URL connection as JarURLConnection
2024-11-29 15:31:15,237 [ 180854] SEVERE - #c.i.u.c.SchedulingWrapper - Worker exited due to exception
java.lang.IllegalStateException: SDK is not defined for Run Configuration
	at com.jetbrains.python.run.PythonCommandLineState.getPythonTargetInterpreter(PythonCommandLineState.java:1028)
	at com.jetbrains.python.run.PythonCommandLineState.getPythonTargetInterpreter(PythonCommandLineState.java:1023)
	at com.jetbrains.python.run.PythonCommandLineState.startProcess(PythonCommandLineState.java:309)
	at com.jetbrains.python.run.PythonCommandLineState.execute(PythonCommandLineState.java:232)
	at com.jetbrains.python.run.PythonScriptCommandLineState.execute(PythonScriptCommandLineState.java:147)
	at com.jetbrains.python.run.PythonCommandLineState.execute(PythonCommandLineState.java:179)
	at com.jetbrains.python.run.PythonRunner.lambda$execute$1(PythonRunner.java:66)
	at com.intellij.util.concurrency.ChildContext$runInChildContext$1.invoke(propagation.kt:101)
	at com.intellij.util.concurrency.ChildContext$runInChildContext$1.invoke(propagation.kt:101)
	at com.intellij.util.concurrency.ChildContext.runInChildContext(propagation.kt:107)
	at com.intellij.util.concurrency.ChildContext.runInChildContext(propagation.kt:101)
	at com.intellij.util.concurrency.ContextRunnable.run(ContextRunnable.java:27)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
	at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:735)
	at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:732)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
	at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1.run(Executors.java:732)
	at java.base/java.lang.Thread.run(Thread.java:1583)
2024-11-29 15:31:15,248 [ 180865] SEVERE - #c.i.u.c.SchedulingWrapper - IntelliJ IDEA 2024.3  Build #IU-243.21565.193
2024-11-29 15:31:15,249 [ 180866] SEVERE - #c.i.u.c.SchedulingWrapper - JDK: 21.0.5; VM: OpenJDK 64-Bit Server VM; Vendor: JetBrains s.r.o.
2024-11-29 15:31:15,249 [ 180866] SEVERE - #c.i.u.c.SchedulingWrapper - OS: Mac OS X
2024-11-29 15:31:15,249 [ 180866] SEVERE - #c.i.u.c.SchedulingWrapper - Plugin to blame: Python Community Edition version: 243.21565.211
Exception in thread "ApplicationImpl pooled thread 4" java.lang.IllegalStateException: SDK is not defined for Run Configuration
	at com.jetbrains.python.run.PythonCommandLineState.getPythonTargetInterpreter(PythonCommandLineState.java:1028)
	at com.jetbrains.python.run.PythonCommandLineState.getPythonTargetInterpreter(PythonCommandLineState.java:1023)
	at com.jetbrains.python.run.PythonCommandLineState.startProcess(PythonCommandLineState.java:309)
	at com.jetbrains.python.run.PythonCommandLineState.execute(PythonCommandLineState.java:232)
	at com.jetbrains.python.run.PythonScriptCommandLineState.execute(PythonScriptCommandLineState.java:147)
	at com.jetbrains.python.run.PythonCommandLineState.execute(PythonCommandLineState.java:179)
	at com.jetbrains.python.run.PythonRunner.lambda$execute$1(PythonRunner.java:66)
	at com.intellij.util.concurrency.ChildContext$runInChildContext$1.invoke(propagation.kt:101)
	at com.intellij.util.concurrency.ChildContext$runInChildContext$1.invoke(propagation.kt:101)
	at com.intellij.util.concurrency.ChildContext.runInChildContext(propagation.kt:107)
	at com.intellij.util.concurrency.ChildContext.runInChildContext(propagation.kt:101)
	at com.intellij.util.concurrency.ContextRunnable.run(ContextRunnable.java:27)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
	at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:735)
	at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:732)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
	at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1.run(Executors.java:732)
	at java.base/java.lang.Thread.run(Thread.java:1583)

phodal added a commit that referenced this issue Nov 29, 2024
Update IntelliJ IDEA version to IU-2024.3 and PythonCore plugin to version 243.21565.211 to address compatibility issues with the Python SDK for #146. Additionally, refactor build script to correctly include platform plugins and improve error handling in the ConfigurationRunner.
phodal added a commit that referenced this issue Nov 29, 2024
The commit updates the `FileRunService` by introducing asynchronous execution using `CompletableFuture`. It removes the temporary flag and directly sets the configuration as temporary. Additionally, the `RunServiceTask` now accepts a `CompletableFuture` and completes it with the result of the run operation. This change enhances the run operation by enabling asynchronous handling of the run task.
phodal added a commit that referenced this issue Nov 29, 2024
The commit relocates the `ShireProcessHandler` class from the `shirelang` module to the `core` module under a new package name, updates its import paths, and renames the file accordingly. Additionally, the class has been enhanced with new functionality, including input/output stream handling and ANSI escape decoder integration for improved console output support. Furthermore, related classes such as `ShireRunConfigurationProfileState` and `RunServiceTask` have been updated to reflect these changes.
phodal added a commit that referenced this issue Nov 29, 2024
…Task.kt #146

The commit streamlines the creation of the execution environment within the RunServiceTask.kt file by combining the null check and throw statement into a single line using the elvis operator. This enhances code readability and reduces the number of lines.
@phodal phodal closed this as completed in 3f57680 Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants