Page 1 of 1

[SOLVED] Developer tool: "Copy as Python"

Posted: Tue Sep 18, 2012 2:54 am
by blackwind
The current method of retrieving an action's corresponding Python code (Ctrl+C) has a number of pitfalls:
  1. Why should I have to open another window, paste, and clip out what I need?
  2. Why should I have to manually unescape ampersands and other HTML entities escaped by the XML generator?
  3. Why should I have to prepend "eg.plugins." to the result?
These could all be resolved with a "Copy as Python" menuitem, perhaps with Shift+Ctrl+C as its shortcut. Such an addition would increase the speed of my workflow immensely. Thoughts?

Re: Developer tool: "Copy as Python"

Posted: Sun Dec 02, 2012 4:51 pm
by Pako
=> 0.4.1.r1600

Re: Developer tool: "Copy as Python"

Posted: Mon Feb 04, 2013 8:45 am
by miljbee
Excellent !

Re: Developer tool: "Copy as Python"

Posted: Wed Feb 20, 2013 7:28 pm
by cfull1
Very nice.

Re: Developer tool: "Copy as Python"

Posted: Sat Mar 30, 2013 11:17 pm
by jonib
So I finally needed to use the "Copy as Python" command. Thanks for adding it very useful.

jonib

Re: Developer tool: "Copy as Python"

Posted: Fri Apr 26, 2013 12:11 am
by blackwind
A small bug: Copy as Python doesn't work as expected with Python Script objects.

Re: Developer tool: "Copy as Python"

Posted: Fri Apr 26, 2013 6:55 am
by Pako
blackwind wrote:A small bug: Copy as Python doesn't work as expected with Python Script objects.
This is weird.
I tried it just now and no problem I have encountered.
Probably it will depend on the content. Can you please attach a script to which it is a problem?

Pako

Re: Developer tool: "Copy as Python"

Posted: Fri Apr 26, 2013 9:05 pm
by blackwind

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
<EventGhost Version="1610">
    <Action Name="Disable WOW64 Redirection">
        EventGhost.PythonScript(u'import win32file\nwin32file.Wow64DisableWow64FsRedirection()\n')
    </Action>
</EventGhost>
When copied as Python, this is the result:

Code: Select all

eg.plugins.sion="1.0" encoding="UTF-8" ?>
<EventGhost Version="1610">
    <Action Name="Disable WOW64 Redirection">
        EventGhost.PythonScript(u'import win32file\nwin32file.Wow64DisableWow64FsRedirection()\n')
This only applies to Python Scripts -- not Python Commands.

Re: Developer tool: "Copy as Python"

Posted: Mon Apr 29, 2013 2:39 pm
by Pako
So now it is completely clear.
The bug is not related to Python Script action, but any action that is renamed (its label in the tree is written in italics).
But it is obvious, that it happens most frequently just with Python Script :) .
You can download the corrected file TreeItem.py.

Pako

Re: Developer tool: "Copy as Python"

Posted: Mon Apr 29, 2013 9:58 pm
by blackwind
Ah, that's interesting. Yep, Python Scripts are the only actions I ever rename, and I've done so for every single one in my config.

One last thing: I was thinking, wouldn't it make more sense if Python Commands and Scripts copied as their contained code rather than an eg.plugins reference? So, this:

Code: Select all

import win32file
win32file.Wow64DisableWow64FsRedirection()
...rather than this:

Code: Select all

eg.plugins.EventGhost.PythonScript(u'import win32file\nwin32file.Wow64DisableWow64FsRedirection()\n')
I, for one, can't think of any scenario in which I'd want the latter over the former.

Re: Developer tool: "Copy as Python"

Posted: Wed May 01, 2013 5:10 pm
by Pako
Yes, it is a good idea. It's logical.
I thought it would be very simple. But it gave me quite a hard time.
I had received ...

Code: Select all

import win32file\nwin32file.Wow64DisableWow64FsRedirection()
instead of ...

Code: Select all

import win32file
win32file.Wow64DisableWow64FsRedirection()
and long time I could not figure out how to get rid of.
But now it works. Again, you can download the new version of TreeItem.py file.

Pako

Re: Developer tool: "Copy as Python"

Posted: Wed May 01, 2013 6:55 pm
by blackwind
Excellent. I'd recommend removing the trailing " + chr(10)" on both lines so the code that's copied is identical to the code written, but other than that, looks good!