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

remote files are not opened when clicking on the stacktrace hyperlinks #3187

Open
wohltat opened this issue May 5, 2024 · 2 comments
Open
Milestone

Comments

@wohltat
Copy link

wohltat commented May 5, 2024

When an error occurs in a python file for example boot.py, clicking on the stacktrace hyperlink for that file opens a local file instead of the remote one.
This is obviously know according to the TODO comments..

thonny/thonny/shell.py

Lines 1490 to 1491 in 78a6fa8

# TODO: handle remote files
# TODO: better use events instead direct referencing

What about the following solution. It works on my machine. Tested it for micropython and local python.

from thonny.editors import make_remote_path

def _handle_hyperlink(self, event):
        import webbrowser

        try:
            line = self.get("insert linestart", "insert lineend")
            # Python stacktrace
            matches = list(re.finditer(r'File "(?P<file>[^"]+)", line (?P<line>\d+)', line))
            if not matches:
                # Friendly traceback
                matches = list(
                    re.finditer(
                        r"\b(?P<line>\d+)\b.+'(?P<file>[^\']+\.pyw?)'", line, flags=re.IGNORECASE
                    )
                )

from thonny.editors import make_remote_path

def _handle_hyperlink(self, event):
        import webbrowser

        try:
            line = self.get("insert linestart", "insert lineend")
            # Python stacktrace
            matches = list(re.finditer(r'File "(?P<file>[^"]+)", line (?P<line>\d+)', line))
            if not matches:
                # Friendly traceback
                matches = list(
                    re.finditer(
                        r"\b(?P<line>\d+)\b.+'(?P<file>[^\']+\.pyw?)'", line, flags=re.IGNORECASE
                    )
                )

            if len(matches) == 1:
                filename = os.path.expanduser(matches[0].group("file"))
                lineno = int(matches[0].group("line"))
                
                if (
                    filename in (STRING_PSEUDO_FILENAME, REPL_PSEUDO_FILENAME)
                    and self._last_main_file
                ):
                    filename = self._last_main_file

                elif runner := get_runner():
                    if proxy := runner.get_backend_proxy():
                        from thonny.plugins.cpython_frontend import LocalCPythonProxy
                        if proxy.is_connected() and not isinstance(proxy, LocalCPythonProxy):
                            filename = make_remote_path('/') + filename
                            print(f'{filename=}')

                get_workbench().get_editor_notebook().show_file( filename, lineno, set_focus=False)
                print(f'{filename=}')
                # NB! Don't attempt to check the existence of the file as it may be remote file
                # or editor id (of untitled editor)
                # TODO: handle remote files
                # TODO: better use events instead direct referencing

            else:
                r = self.tag_prevrange("io_hyperlink", "@%d,%d" % (event.x, event.y))
                if r and len(r) == 2:
                    url = self.get(r[0], r[1])
                    if SIMPLE_URL_SPLIT_REGEX.match(url):
                        webbrowser.open(url)

        except Exception as e:
            logger.exception("Could not handle hyperlink click", exc_info=e)

            else:
                r = self.tag_prevrange("io_hyperlink", "@%d,%d" % (event.x, event.y))
                if r and len(r) == 2:
                    url = self.get(r[0], r[1])
                    if SIMPLE_URL_SPLIT_REGEX.match(url):
                        webbrowser.open(url)

        except Exception as e:
            logger.exception("Could not handle hyperlink click", exc_info=e)

Can you give an example of how this could be handled with events?


Thonny 5.0.0.b1-dev
Manjaro Linux
Python 3.11.8
Tk 8.6.14

@wohltat
Copy link
Author

wohltat commented May 5, 2024

The Code above still has problems with and files in subfolders.
I'll try to fix it later.

Edit: fixed the above code. Should work now.

@aivarannamaa
Copy link
Member

Thanks for the suggestion!

Actually it is not always clear, whether to open the file on the device or on the development machine. Some users keep and edit the master copies on the dev machine and upload when ready. Of course, Thonny could keep track of this relationship and open the master copy. I'll consider implementing this in the next feature release after 5.0.

@aivarannamaa aivarannamaa added this to the 5.x milestone May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants