It works by starting a thread within the plugin with an instance of code.InteractiveConsole, which is how you make a Python interpreter inside of a Python program, and using Applescript to ask Terminal.app to run a little Python program that gets input lines from the user, sends them to the plugin, gets responses and prints them for the user to see. The plugin thread and the process communicate through named pipes. This works well enough for what I want it to do, but isn't perfect. Perfect would require sockets, telnetlib and curses and be considerably more complicated, but then sys.stdin would work inside the console, whereas right now it gets hooked up to /dev/null.
In the process of figuring out how to do this, I discovered that this (sort of) works from within a plugin.
- Code: Select all
from pudb.remote import set_trace # you'll need to pip install pudb
set_trace(term_size=(80, 24))
And then from Terminal,
- Code: Select all
telnet 127.0.0.1 6899
If that port number is taken it will pick a different one and not tell you and this won't work. But if you're lucky, pudb will come up in glorious color. I ran this from a menu command and was able to set a breakpoint in deviceStartComm and look at variables once I got there. But the interactive interpreter in pudb didn't work and then it crashed. I am not much of a debugger user so I didn't pursue it further, but it seems possible that it could be made to work. Or maybe pdb would work using my named pipes method.