QED

java..python..eclipse..and whatever else interests me.

Wednesday, April 26, 2006

Scripting Eclipse with Jython and PyDev

I joined the Eclipse Monkey project Dashtime the other day, and spend the hour talking to Ward Cunningham (Wiki inventor)... very cool.

I had seen Monkey on Wayne Beaton's Eclipse when he talked at NEJUG/BostonEdge, and scripting sounded interesting. I hadn't even installed, and didn't expect to be the only caller!

We had a good discussion, my notes are here. Apparently Rhino was selected because JavaScript is"nobody's favorite language" :)

Monkey is useful but doesn't yet have autocomplete or an interactive shell. I think that's needed to learn the API's, and delve further into Eclipse plugins and SWT. EclipseShell is interesting, but it's not a straight interactive console like Python's, where you can create and interact with objects. (Perhaps the Rhino console can be hooked up)

I use PyDev for Python development, and even sprang for the extensions, which allow an Interactive Console. I can fire up Jython within Eclipse and then import and invoke Java objects-

However, something is wrong.. I want to get the the active window (the window global in Monkey), but it is coming back null:

window = wb.getActiveWorkbenchWindow() print window None

Other API's work fine...

for vd in wb.getViewRegistry().getViews():
    print vd.getId()
byecycle.views.ByecycleView
com.kodeshare.search.ui.KodeSearchView edu.mit.csail.relo.console.ConsoleView
...

There's more than one way to skin a cat--getWorkbenchWindows() works:

wnds = PlatformUI.getWorkbench().getWorkbenchWindows()
len(wnds)
1
window=wnds[0]

Great! Let's try a HelloWorld..

import org
org.eclipse.jface.dialogs.MessageDialog.openInformation( window.getShell(), Hello","World")
Traceback (innermost last): File "<console>", line 1, in ? org.eclipse.swt.SWTException: Invalid thread access

At least this has a reasonable explanation (I'm on a worker thread).

So, you can use Eclipse API's and objects interactively. Excellent. Next, I would love for Autocomplete to work. Ctrl-Space doesn't work in the console. (Bug? I think so.. the console has the Jython nature now.)

For some wierd reason, the python dir() function isn't working, returning an empty list even for a builtin class:

str = "Hello"
dir(str)
[]

Normally this would return a list of methods:

>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__g t__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__ ', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', ' __rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdi git', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lst rip', 'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'split lines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill ']

So close.. hopefully pydev extensions can enable autocomplete on the console. That would make for a truly interactive exploration of the Eclipse environment.

1 Comments:

At 6:58 AM, Blogger Fabio Zadrozny said...

Hi there...

Actually you got into a jython pitfall... The following interactive session should make things clearer:

[D:\bin\jython21]jython.bat
Jython 2.1 on java1.5.0_04 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> a = '1'
>>> dir(a)
[]
>>> dir(a.__class__)
['__add__', '__cmp__', '__complex__', '__contains__', '__len__', '__mod__', '__repr__', '__str__', 'alnum', 'alpha', 'ca
pitalize', 'center', 'classDictInit', 'count', 'decimal', 'digit', 'encode', 'endswith', 'expandtabs', 'find', 'index',
'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'islower', 'isnumeric', 'isspace', 'istitle', 'isunicode', 'isupper', 'joi
n', 'ljust', 'lower', 'lstrip', 'numeric', 'replace', 'rfind', 'rindex', 'rjust', 'rstrip', 'space', 'split', 'splitline
s', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'unicode', 'upper', 'zfill']
>>>

And there's even more to it. If you are working with java classes you have to get and dir the whole hierarchy (class by class). I already do that for code-completion in the editor, and I intend to make a better dir function available for download in the next pydev version as an utility.

As for Ctrl+Space directly in the console, that's something much more difficult than it appears and I still haven't figured out how I'd go to implement it....

That's why I binded the console to the editor, so that you can use the code-completion capabilities of the editor toghether with the interactive console (and yes, I realize that's not a perfect solution, but still, much better than nothing).

-- and as for getting the window, you need to be on a gui thread... maybe there's a way to do some utils that will allow you to do that (but I still haven't gotten to it).

The pydev-code list has some interesting discussions about scripting on pydev, and I believe you could forward your questions there (https://lists.sourceforge.net/lists/listinfo/pydev-code).


Cheers,

Fabio

 

Post a Comment

Links to this post:

Create a Link

<< Home