Re: [Tutor] How to interact with the result of subprocess.call()

2016-12-25 Thread Alan Gauld via Tutor
On 25/12/16 01:58, boB Stepp wrote:

> the stdin option of call()might be used to direct the desired
> keystrokes to LO?  After looking at

The problem is that keystrokes in a GUI are not read from
stdin, they are read as events from the GUI event loop.
So, if LO was a CLI tool (like vim or top, say) then you
are right, you could pipe the keystrokes through stdin,
but in a GUI those keystrokes would never be seen (or
if seen do something very different to what is expected)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to interact with the result of subprocess.call()

2016-12-25 Thread Jim Byrnes

On 12/24/2016 07:43 PM, Alan Gauld via Tutor wrote:

On 25/12/16 01:21, Jim Byrnes wrote:


I am not trying to automate libreoffice using subprocess.


No, but you are trying to automate LO from within Python
by sending it keystrokes and that's not easy. That's why I
previously asked whether you really wanted to open the LO
file directly and manipulate it from within Python
- that's (slightly) easier than manipulating LO directly
and much easier than manipulating LO from Python via
keystrokes.




message I was told that subprocess was the way to open libreoffice from
a python script.


Which is true if you want to bring up a LO session for
your user to manipulate. But it's not the way to drive
LO automatically. (One option is to start LO from Python
then use macros within LO to do the automation - there may
even be a command line switch to trigger a macro - I can't
remember off hand)




To drive LO via keystrokes your program needs to inject
key/mouse events into the LO event queue. That's not easy
and not very reliable either(*). There are some libraries that
can help but it should be the path of last resort.

(*)LO remembers its last screen setting and opens with them,
if those screen settings are different than the ones you
programmed for then navigation will be different and so on.
That's easy to deal with for a human who can see the screen
but sending keystrokes programmatically you are effectively
trying to drive the system blindfolded!


I don't think I need to "know where stuff is" to manipulate LO. At first 
I was just using Selenium to get the data from the web page, but the 
focus would end up in the url bar. I forget the exact details but I 
could not get Selenium to manipulate Chrome anymore at that point. I did 
some searching and found pykeyboard. Using it I was able to send Ctrl-A 
and then Ctrl-C to copy the page to the clipboard.


My thinking is if I can get LO to accept keystrokes I can send 
Shift-Ctrl-V to paste special and the two enter keys to answer dialog 
questions and paste the info into a sheet.  I would use the fact that LO 
reopens to where it was closed to my advantage by not having to use a 
macro to navigate to the proper page.



Up until this point in the script I have used a combination of Selenium
and pykeyboard to log on to a web site and put some info in the
clipboard. Now I need to send keystrokes to libreoffice to paste from
the clipboard into the spreadsheet.


Or you could just open the spreadsheet file directly
and insert the data directly into it from Python. I think
there is a library for that - there are several for doing
it in Excel (so if your spreadsheet is in Excel format it
is fairly easy). Or, if you can use CSV format, its just a
standard library module.


I'll look into these alternatives if I can't figure out how to get 
keystrokes into LO using my present approach.



Alternatively you can use the LO API to directly inject
the data into the spreadsheet objects (like using COM
in Microsoft land).


I have used pyuno api to automate libreoffice in the past, but it was a
time consuming and confusing process.


Trust me it is nowhere near as confusing and frustrating
as trying to drive LO (Or any other GUI) via keystrokes!


Based on my success with pykeyboard and Chrome I thought it would be 
easier than diving back into Uno.  However, using subprocess seems to be 
blocking me from sending any keystrokes to LO. I don't understand 
subprocess well enough to know if it is actually blocking my keystrokes. 
I concluded that based on the fact that when I closed LO the two 
enter_keys at the end of the script were executed in the terminal.


Is there a way to terminate subprocess and still keep LO open so 
pykeyboard can send it keystrokes from the script?



I was trying this approach
because it looked like I could avoid the uno complexity.


If there isn't a direct file manipulation library for LO
spreadsheets then UNO is probably the easiest option.




Regards,  Jim


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to interact with the result of subprocess.call()

2016-12-25 Thread boB Stepp
On Sun, Dec 25, 2016 at 3:08 AM, Alan Gauld via Tutor  wrote:
>
> On 25/12/16 01:58, boB Stepp wrote:
>
> > the stdin option of call()might be used to direct the desired
> > keystrokes to LO?  After looking at
>
> The problem is that keystrokes in a GUI are not read from
> stdin, they are read as events from the GUI event loop.
> So, if LO was a CLI tool (like vim or top, say) then you
> are right, you could pipe the keystrokes through stdin,
> but in a GUI those keystrokes would never be seen (or
> if seen do something very different to what is expected)

Then I see that I have a GCE (Gross Conceptual Error) floating around.
I thought that event loops are just intercepting the redirected stdin
from the keyboard.  This is not true?  If not, then how is this
working?

boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to interact with the result of subprocess.call()

2016-12-25 Thread Alan Gauld via Tutor
On 25/12/16 17:08, boB Stepp wrote:

> Then I see that I have a GCE (Gross Conceptual Error) floating around.
> I thought that event loops are just intercepting the redirected stdin
> from the keyboard.  This is not true?  If not, then how is this
> working?

No event loops don't use stdin. They are monitoring the
hardware devices directly and creating event objects that
are put in a queue maintained by the GUI framework itself.
Most frameworks allow you create your own events and
inject them into the queue but they don;t come from stdin.
You could write a background thread that read stdin and
generated events from there but that's not normal by any
means. Some GUI programs will read stdin and process anything
found there but its usually a completely different set
of commands from those used by the normal user - and
even this is very rare.

For more exact detail of how GUIs get their input you
will need to investigate the individual GUIs, even
drilling down to X-Server level or the Windows API.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor