解决GNU Radio的内嵌代码块无法打开代码编辑器

发布时间 2023-12-21 18:17:42作者: qsBye

摘要

解决GNU Radio的内嵌代码块无法打开编辑器的问题.通过修改py脚本实现使用VSCode编辑内嵌代码.

问题描述

环境:

  1. 系统macOS 13.5
  2. GNU Radio Companion 3.10.8.0 (Python 3.10.13)
代码块 选择应用程序是空的 选择应用程序是空的

实现

修改

/Users/workspace/radioconda/lib/python3.10/site-packages/gnuradio/grc/gui/Dialogs.py

def choose_editor(parent, config):
    """
    Give the option to either choose an editor or use the default.
    """
    content_type = Gio.content_type_from_mime_type("text/x-python")
    if content_type == "*":
        # fallback to plain text on Windows if no useful x-python association
        content_type = Gio.content_type_from_mime_type("text/plain")
    dialog = Gtk.AppChooserDialog.new_for_content_type(
        parent,
        Gtk.DialogFlags.MODAL,
        content_type,
    )
    dialog.set_heading("Choose an editor below")
    widget = dialog.get_widget()
    widget.set_default_text("Choose an editor")
    widget.set_show_default(True)
    widget.set_show_recommended(True)
    widget.set_show_fallback(True)

    editor = None
    response = dialog.run()
    if response == Gtk.ResponseType.OK:
        appinfo = dialog.get_app_info()
        editor = config.editor = appinfo.get_executable()
    dialog.destroy()

    # 设置默认编辑器
    editor = "/Applications/Visual Studio Code.app/Contents/MacOS/Electron"
    
    return editor

注意是使用app内的可执行文件.

效果

  1. 启动gnu radio companion
gnuradio-companion &
  1. 点击打开编辑器以直接打开VSCode编辑代码
图片 动图

参考文献

如何在windows上编辑gnuradio的python块?
如何在windows上编辑gnuradio的python块?我已经安装了python,比如PyScripter,Anaconda,但是按下‘使用默认值’‘在编辑器中打开’下面的两行错误将运行
我想任何编辑都行。我刚用代码试了一下。唯一的缺点是文件对话框默认不显示隐藏的文件,因此您需要右键单击对话,然后选择显示它们的选项,然后导航到您想要的目录(如果不更改目录就不会更新,或者可能有刷新选项。

如果您安装了Python,您可以使用Idle,可以像我一样使用,或者使用记事本(或者Notepad++作为其他回答状态)。numpy和gnuradio的导入被Visual代码标记为未解决,所以我还没有脱离困境,但作为一个编辑器,它工作得很好。
It is easily reproduced. Just create an embedded Python block in Windows and double-click it, and click "Open in Editor". A dialogue to choose the editor presents 3 options 1) Choose an editor, 2) Use the default editor 3) Cancel. Using the default editor gives the error "Unable to load the default editor, please choose an editor". Choosing an editor opens a file dialogue box, which curiously does not allow browsing the entire system. For instance, c:\users\username\AppData is not visible, although a directory called "Application Data" appears in the same place. Navigating into it shows no files, when there are actually hundreds of files there, including the default location of Visual Studio Code.

I tried setting the default editor to a shortcut to vscode.exe, which could be seen from the dialogue box. That yielded the error "Error opening an external editor. Please select a different editor", after which the clicking the "Open in Editor" button does not open the dialogue to choose the editor, but just produces the same error message.

Restarting gnu radio fixed that problem. Then I stumbled upon the right click menu in the file dialogue that adds hidden files to the view, and that works. Absent vscode, perhaps Idle or Notepad would suffice for casual use.

73,
Chris VE3NRT

Hi all,

This is not a fix, but a hack. Maybe it will help you ?

This is the function that defines what editor will be used (or prompts you to pick one)

gnuradio/grc/gui/Dialogs.py
Line 405 in b2c9623

 def choose_editor(parent, config): 

I replaced line 405 here with:

def choose_editor(parent,config):
    return r'C:\path\to\vscode.exe'
def _choose_editor(parent, config):

For me, Dialogs.py was in: C:\ProgramData\radioconda\pkgs\gnuradio-grc-3.10.4.0-py310h4a85852_2\Lib\site-packages\gnuradio\grc\gui\Dialogs.py. This will vary depending on where you installed radioconda and what version you have.

I'm sure there is a more elegant solution, but this worked for me!

There's a bit of inconsistency in the way config files are used. In Windows, GRC stores its config values in C:\Users\Bob.gnuradio\grc.conf. However, the preferred editor value is loaded by the core Gnu Radio framework from C:\Users\Bob\AppData\Roaming.gnuradio\config.conf. If you open that config file (create it if it doesn't exist) and add the following,

[grc]
editor = C:\Program Files\Notepad++\notepad++.exe

GRC will automatically launch Notepad++ when you click the "Open In Editor" button. Obviously, change the paths appropriately.

The "Use Default Editor" button causes the choose_editor() function in grc/gui/Dialogs.py to execute, which returns the name of an executable to use for editing the file. If one is specified in the config file, the function returns that value. Otherwise, it tries to return the system-specific executable for launching the default application associated with a filetype (eg. when double-clicking on a .txt file, the system launches the app associated with .txt files). This executable is called open in OSX and xdg-open in Linux. The reason that it doesn't work in this case is because Windows doesn't have an executable for that purpose. There is a built-in start command in the command prompt which does the same thing, but since it's not an executable, it can't be "called" from Python.

A fix might be to change the way the choose_editor() dialog works from returning the name of an executable to maybe returning a shell command. Another way might be to include an executable or batch script or something in an appropriate bin directory, and update choose_editor() to return the name of that executable. Something like

process = None
if sys.platform.startswith('linux'):
    process = find_executable('xdg-open')
elif sys.platform.startswith('darwin'):
    process = find_executable('open')
elif sys.platform.startswith('win32'):
    process = find_executable('custom-script-that-launches-file')
if process is None:
    raise ValueError("Can't find default editor executable")

Thanks to @pedro-sidra's comment for giving me a hint for where to look.