trouble controlling vim with subprocess on windows machine

E

Eric_Dexter

I am having trouble contolling vim with subprocess on a windows
machine. It appears that vim comes up on the machine all right and it
sometimes looks like it is doing the searchs what I am asking it to do
but when I am asking it to load a file it doesn't do anything. Is
there something I need to do to push the data through the pipe?? Here
is a couple different ways I am trying to do it.

def load_instrument(instr_name, csdname):
"load an instrument using vim a path should be defined for vim
currently I am doing that with the .bat file that loads the program"
f = open('csdfile.tmp','w')
my_path = "/dex tracker"
cmd = ["gvim",csdname]
vimin = subprocess.Popen(cmd,stdin=subprocess.PIPE)#.comunicate()
[0] #, stdout = f, stderr = f, cwd = my_path )
#f.close
#x = subprocess.Popen("clear").communicate(None)[0]
innum = csr.return_unique_instr_number(csdname) - 1
cmd = """/;<start""" + str(innum) + '>' +'\n'
print(cmd)
vimin.stdin.write(cmd)
cmd = """/;<endin>""" +'\n'
vimin.stdin.communicate(cmd)
cmd = ':r ' + instr_name +'\n'
print(cmd)
vimin.stdin.write(cmd)
#vimin.stdin.write(instr_name + '\n')
cmd = instr_name + '\n'

f.close()
def load_instrument2(instr_name, csdname):
"second try uses comunicate"
cmd = ["gvim",csdname]
proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, )
cmd = """/;start""" + '\n'
proc.communicate(cmd)[0]
cmd = """/;<endin>""" + '\n'
proc.communicate(cmd)[0]
cmd = ':r ' + instr_name + '/n'
proc.communicate(cmd)[0]
proc.close()

I am calling these with

load_instrument2("""strings.orc""",'bay-at-night.csd')

and I define the path of vim in a batch file befour I call the
routines

path c:\program files\vim\vim71
python_awk_bridge.py
pause
 
N

Nick Craig-Wood

I am having trouble contolling vim with subprocess on a windows
machine. It appears that vim comes up on the machine all right and it
sometimes looks like it is doing the searchs what I am asking it to do
but when I am asking it to load a file it doesn't do anything. Is
there something I need to do to push the data through the pipe?? Here
is a couple different ways I am trying to do it.

For controlling an interactive program subprocess isn't the right
tool. Subprocess only really does one way communication - it isn't
good at conversations. I'd suggest pexpect but it doesn't work on
windows.

You appear to be doing stuff with csound. There are several python
modules out there which interface with csound - did you investigate
those?
 
E

Eric_Dexter

For controlling an interactive program subprocess isn't the right
tool. Subprocess only really does one way communication - it isn't
good at conversations. I'd suggest pexpect but it doesn't work on
windows.

You appear to be doing stuff with csound. There are several python
modules out there which interface with csound - did you investigate
those?

The book (as kramer would say)

https://sourceforge.net/projects/dex-tracker

I would think that the purpose for stdin is input and stdout is
output.. otherwise why have stdin?? The pyexpect does look like what
I am after but it isn't windows.. And did I mention I am willing to
cheat I have a large number of unix tools that are converted over (but
not cigwin) and will even be willing to generate a script file if I
can get it to vim when it starts up... whatever it takes regardless
of how ugly looking it is.

I did notice expect

http://expect.nist.gov/#windows
http://bmrc.berkeley.edu/people/chaffee/tcltk.html

I am not sure what to do to call the tcl stuff from python though.
 
J

Josiah Carlson

I am having trouble contolling vim with subprocess on a windows
machine. It appears that vim comes up on the machine all right and it
sometimes looks like it is doing the searchs what I am asking it to do
but when I am asking it to load a file it doesn't do anything. Is
there something I need to do to push the data through the pipe?? Here
is a couple different ways I am trying to do it.
[snip]

This recipe for asynchronous communication using subprocess could be
used to write an expect-like tool:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440554

It works on both Windows and *nix.


- Josiah
 
E

Eric_Dexter

I am having trouble contolling vim with subprocess on a windows
machine. It appears that vim comes up on the machine all right and it
sometimes looks like it is doing the searchs what I am asking it to do
but when I am asking it to load a file it doesn't do anything. Is
there something I need to do to push the data through the pipe?? Here
is a couple different ways I am trying to do it.

[snip]

This recipe for asynchronous communication using subprocess could be
used to write an expect-like tool:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440554

It works on both Windows and *nix.

- Josiah

I had the original dir work but when I tried to trade it out with vim
it isn't clear
how I should call it.. vim filename and it doesn't find filename for
some reason.
I called it pipe and then


inport pipe

def load_instrument3(instr_name, csd_name):
if sys.platform == 'win32':
shell, commands, tail = ('gvim' + csd_name, (csd_name,
csd_name), '\r\n')
else:
shell, commands, tail = ('sh', ('ls', 'echo HELLO WORLD'),
'\n')

a = pipe.Popen(shell, stdin=pipe.PIPE, stdout=pipe.PIPE)
print pipe.recv_some(a),
for cmd in commands:
pipe.send_all(a, csd_name)
print pipe.recv_some(a),
pipe.send_all(a, csd_name)
print pipe.recv_some(a, e=0)
a.wait()
 
J

Josiah Carlson

I am having trouble contolling vim with subprocess on a windows
machine. It appears that vim comes up on the machine all right and it
sometimes looks like it is doing the searchs what I am asking it to do
but when I am asking it to load a file it doesn't do anything. Is
there something I need to do to push the data through the pipe?? Here
is a couple different ways I am trying to do it.
[snip]

This recipe for asynchronous communication using subprocess could be
used to write an expect-like tool:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440554

It works on both Windows and *nix.

- Josiah

I had the original dir work but when I tried to trade it out with vim
it isn't clear
how I should call it.. vim filename and it doesn't find filename for
some reason.
I called it pipe and then


inport pipe

def load_instrument3(instr_name, csd_name):
if sys.platform == 'win32':
shell, commands, tail = ('gvim' + csd_name, (csd_name,
csd_name), '\r\n')
else:
shell, commands, tail = ('sh', ('ls', 'echo HELLO WORLD'),
'\n')

a = pipe.Popen(shell, stdin=pipe.PIPE, stdout=pipe.PIPE)
print pipe.recv_some(a),
for cmd in commands:
pipe.send_all(a, csd_name)
print pipe.recv_some(a),
pipe.send_all(a, csd_name)
print pipe.recv_some(a, e=0)
a.wait()

The example uses a platform-specific shell in order to use the
environment variable PATH to discover the executable to run. If you
know the exact path to your binary ('gvim' for you), it should work.
As-is, your program would require a binary with the name 'gvim'+csd_name
in the same path as the script you are executing.

- Josiah
 
A

agc

Hi Josiah,

I have played with the above recipe and it is excellent,
but could you please go into some more detail about what is needed
to make a cross platform [p]expect like (non-pty based) tool?

Specifically, could you describe how you would connect to
*another* interactive Python process with your subclass of
subprocess.Popen?
i.e:

a = Popen('python', stdin=?, stdout=?, stderr=?)
#now run an interactive session with 'a'

I have tried several combinations of the above and I seem
to be stuck on the fact that python is interacting with a
'tty', not 'std*'. Maybe I'm missing a basic piece?

Thanks for any input,
Alex
 
E

Eric_Dexter

Hi Josiah,

I have played with the above recipe and it is excellent,
but could you please go into some more detail about what is needed
to make a cross platform [p]expect like (non-pty based) tool?

Specifically, could you describe how you would connect to
*another* interactive Python process with your subclass of
subprocess.Popen?
i.e:

a = Popen('python', stdin=?, stdout=?, stderr=?)
#now run an interactive session with 'a'

I have tried several combinations of the above and I seem
to be stuck on the fact that python is interacting with a
'tty', not 'std*'. Maybe I'm missing a basic piece?

Thanks for any input,
Alex


The example uses a platform-specific shell in order to use the
environment variable PATH to discover the executable to run. If you
know the exact path to your binary ('gvim' for you), it should work.
As-is, your program would require a binary with the name 'gvim'+csd_name
in the same path as the script you are executing.
- Josiah- Hide quoted text -

- Show quoted text -


I gave this a shot with a windows batch file (although I am not sure I
am doing it right)

path c:\program files\vim\vim71
path c:\dex tracker
gvim bay-at-night.csd
echo \intsr | gvim
echo \<endin> | gvim
echo :r strings.orc | gvim
pause


and it just pulls up gvim bay-at-night.csd and never does the two
searches or pulls up the file. I do have a windows version of sh if
that works better.
 
E

Eric_Dexter

Hi Josiah,
I have played with the above recipe and it is excellent,
but could you please go into some more detail about what is needed
to make a cross platform [p]expect like (non-pty based) tool?
Specifically, could you describe how you would connect to
*another* interactive Python process with your subclass of
subprocess.Popen?
i.e:
a = Popen('python', stdin=?, stdout=?, stderr=?)
#now run an interactive session with 'a'
I have tried several combinations of the above and I seem
to be stuck on the fact that python is interacting with a
'tty', not 'std*'. Maybe I'm missing a basic piece?
Thanks for any input,
Alex
- Show quoted text -

I gave this a shot with a windows batch file (although I am not sure I
am doing it right)

path c:\program files\vim\vim71
path c:\dex tracker
gvim bay-at-night.csd
echo \intsr | gvim
echo \<endin> | gvim
echo :r strings.orc | gvim
pause

and it just pulls up gvim bay-at-night.csd and never does the two
searches or pulls up the file. I do have a windows version of sh if
that works better.- Hide quoted text -

- Show quoted text -

since there are alot of questions about subprocess I will suggest
http://www.rutherfurd.net/python/sendkeys/ this emulates the keyboard
for windows.. I am not sure if it works yet,
 
J

Josiah Carlson

agc said:
Hi Josiah,

I have played with the above recipe and it is excellent,
but could you please go into some more detail about what is needed
to make a cross platform [p]expect like (non-pty based) tool?

We only get ptys. Unless you can figure out some incantation to fake
the creation of a tty (so that ssh and other software don't conmplain),
all you get is a pty.

To make it work like expect, one would (strictly speaking) need to do
the pattern matching that expect does, which may require writing a new
regular expression library.

Specifically, could you describe how you would connect to
*another* interactive Python process with your subclass of
subprocess.Popen?
i.e:

a = Popen('python', stdin=?, stdout=?, stderr=?)
#now run an interactive session with 'a'

Some platforms have very strange buffering behavior with Python. What I
have done in my own code (for creating a Python subprocess, though not
using the subprocess module) is to do something like the following...

python_cmd = '''<path to python> -u -c "import sys; \
sys.stderr=sys.__stderr__=sys.stdout;import __builtin__;\
__builtin__.quit=__builtin__.exit=\
'use Ctrl-Break to restart *this* interpreter';import code;\
code.interact(readfunc=raw_input)"'''

a = Popen(python_cmd, subprocess.PIPE, subprocess.PIPE)
I have tried several combinations of the above and I seem
to be stuck on the fact that python is interacting with a
'tty', not 'std*'. Maybe I'm missing a basic piece?

I've had buffering issues on certain platforms (Windows), error messages
about not having a tty (when trying to control an ssh session on *nix
and Windows), etc. Getting this to work for arbitrary programs that
expect a tty, and/or being able to query the terminal for other
information (dimension, terminal emulation, etc.), will be a chore, if
not impossible.


- Josiah
 
Y

Yves Pouplard

For controlling an interactive program subprocess isn't the right
tool. Subprocess only really does one way communication - it isn't
good at conversations. I'd suggest pexpect but it doesn't work on
windows.

pexpect works with Cygwin's python :)
 
E

Eric_Dexter

pexpect works with Cygwin's python :)




- Show quoted text -

I managed to get it as a com object (they have an example that isn't
very good on the gvim site) and it is in csound routines as of 2pm
today... Looks like it should be easy to do with sed also but I don't
know that much about it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top