By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,247 Members | 2,096 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,247 IT Pros & Developers. It's quick & easy.

trouble controlling vim with subprocess on windows machine

P: n/a
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)#.comun icate()
[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

Jul 9 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Er*********@msn.com <Er*********@msn.comwrote:
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?

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jul 9 '07 #2

P: n/a
On Jul 9, 5:30 am, Nick Craig-Wood <n...@craig-wood.comwrote:
Eric_Dex...@msn.com <Eric_Dex...@msn.comwrote:
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?

--
Nick Craig-Wood <n...@craig-wood.com--http://www.craig-wood.com/nick
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.

Jul 9 '07 #3

P: n/a
Er*********@msn.com wrote:
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/Coo.../Recipe/440554

It works on both Windows and *nix.
- Josiah
Jul 9 '07 #4

P: n/a
On Jul 9, 11:06 am, Josiah Carlson <josiah.carl...@sbcglobal.net>
wrote:
Eric_Dex...@msn.com wrote:
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/Coo.../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()
Jul 9 '07 #5

P: n/a
agc wrote:
Hi Josiah,
>>>This recipe for asynchronous communication usingsubprocesscould be
used to write an expect-like tool:
http://aspn.activestate.com/ASPN/Coo.../Recipe/440554

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
Jul 11 '07 #6

P: n/a
On Jul 16, 7:45 am, Yves Pouplard <yves.poupl...@free.frwrote:
On Mon, 09 Jul 2007 05:30:04 -0500, Nick Craig-Wood

<n...@craig-wood.comwrote:
Eric_Dex...@msn.com <Eric_Dex...@msn.comwrote:
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.

pexpect works with Cygwin's python :)
You appear to be doing stuff withcsound. There are several python
modules out there which interface withcsound- did you investigate
those?- Hide quoted text -

- 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.

Jul 16 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.