Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Development & Programming > Programming Talk
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Programming Talk
This forum is for all programming questions.
The questions do not have to be directly related to Ubuntu and any programming language is allowed.

 
Thread Tools Display Modes
Old February 21st, 2007   #1
tseliot
Python Enthusiast
 
tseliot's Avatar
 
Join Date: May 2005
Location: Lecce, Italy
Beans: 6,288
Send a message via AIM to tseliot
Question pygtk - the output of bash commands on a TextView widget

Hi, I would like to know how I can redirect the output of bash commands (e.g. apt-get install vim-gnome) to a TextView widget using pygtk.

For example if I press a button (see the attached code) I would like to call a few commands in bash (os.popen doesn't seem to handle well commands such as "apt-get install vim-gnome" ) and display their output on the TextView widget.


If this is not possible with a TextView widget I would appreciate any suggestion about an alternative. An explanation about how I could use python-vte in this case would be very welcome.

I have attached the file project.tar.gz which contains the file written in Python (myapp.py) and the .glade file

Thanks in advance.
Attached Files
File Type: gz project.tar.gz (1.5 KB, 18 views)
tseliot is offline   Reply With Quote
Old February 21st, 2007   #2
duff
Ubuntu House Blend
 
duff's Avatar
 
Join Date: Nov 2004
Location: Clemson, SC
Beans: 271
Re: pygtk - the output of bash commands on a TextView widget

Quote:
Originally Posted by tseliot View Post
An explanation about how I could use python-vte in this case would be very welcome.
easy. here's a snippet from a program i wrote a while back.
Code:
         v = vte.Terminal()
         v.fork_command('bash')
         v.feed_child('ssh %s tail -f %s.o%s \n' % (host,jname,id))
         v.show()
         notebook.append_page(v, tab_label=gtk.Label(nodeid))
duff is offline   Reply With Quote
Old February 21st, 2007   #3
tseliot
Python Enthusiast
 
tseliot's Avatar
 
Join Date: May 2005
Location: Lecce, Italy
Beans: 6,288
Send a message via AIM to tseliot
Re: pygtk - the output of bash commands on a TextView widget

Thanks for your reply.

VTE works great now
tseliot is offline   Reply With Quote
Old September 4th, 2007   #4
earobinson
Ubuntu Member
 
earobinson's Avatar
 
Join Date: Jan 2005
Location: Toronto, Ontario, Canada
Beans: 2,566
Ubuntu 9.04 Jaunty Jackalope
Re: pygtk - the output of bash commands on a TextView widget

any way to get rid of the prompt?

eg
Code:
v = vte.Terminal()
v.fork_command('bash')
v.feed_child('whoami\n')
v.feed_child('echo test\n')
v.show()
outputs
Quote:
whoami
echotest
earobinson@minusone:/media/data/dev/propensity/src/sandbox$ whoami
earobinson
earobinson@minusone:/media/data/dev/propensity/src/sandbox$ echo test
test
but I want
Quote:
earobinson
test
__________________
Edward A Robinson -- www.earobinson.org
earobinson is offline   Reply With Quote
Old September 4th, 2007   #5
earobinson
Ubuntu Member
 
earobinson's Avatar
 
Join Date: Jan 2005
Location: Toronto, Ontario, Canada
Beans: 2,566
Ubuntu 9.04 Jaunty Jackalope
Re: pygtk - the output of bash commands on a TextView widget

Ok so I have found another problem

Code:
#!/usr/bin/env python

import os
import gtk
import vte
import time
from subprocess import Popen, PIPE

def show_callback(terminal):
    terminal.feed_child('cd /\n')
    terminal.feed_child('whoami\n')
    terminal.feed_child('echo test\n')
    for ii in range (10):
        terminal.feed_child('echo ' + str(ii + 1)  + '\n')
        time.sleep(1)

def write(terminal, text):
    x, y = terminal.get_cursor_position()
    terminal.feed(text + '\n', len(text) + 1)

window = gtk.Window()
window.connect('destroy', lambda w: gtk.main_quit())

terminal = vte.Terminal()
terminal.connect("show", show_callback)

child_pid = terminal.fork_command()

#write(terminal, '1234567890')

window.add(terminal)
window.show_all()
gtk.main()
dose not count down, instead it waits 10 seconds then prints everything, any way to make it happen in real time?
__________________
Edward A Robinson -- www.earobinson.org

Last edited by earobinson; September 4th, 2007 at 03:17 PM..
earobinson is offline   Reply With Quote
Old September 4th, 2007   #6
tseliot
Python Enthusiast
 
tseliot's Avatar
 
Join Date: May 2005
Location: Lecce, Italy
Beans: 6,288
Send a message via AIM to tseliot
Re: pygtk - the output of bash commands on a TextView widget

Here is the solution to your problem.

create a file named "ear1" and make it look like this:
Code:
#!/bin/bash

NUM="0"

cd /
whoami
echo test

while [ $NUM -lt 10 ]; do
    echo $[$NUM+1]
    NUM=$[$NUM + 1]
    sleep 1
done
type:
Code:
chmod +x ear1
then make your python file look like the following:
Code:
#!/usr/bin/env python

import os
import gtk
import vte
import time
from subprocess import Popen, PIPE

    
def show_callback(terminal):
    terminal.feed_child('./ear1')

def write(terminal, text):
    x, y = terminal.get_cursor_position()
    terminal.feed(text + '\n', len(text) + 1)

window = gtk.Window()
window.connect('destroy', lambda w: gtk.main_quit())

terminal = vte.Terminal()
terminal.fork_command()
terminal.connect("show", show_callback)



#write(terminal, '1234567890')

window.add(terminal)
window.show_all()
gtk.main()
in other words you will have to make it run a bash script
tseliot is offline   Reply With Quote
Old September 4th, 2007   #7
earobinson
Ubuntu Member
 
earobinson's Avatar
 
Join Date: Jan 2005
Location: Toronto, Ontario, Canada
Beans: 2,566
Ubuntu 9.04 Jaunty Jackalope
Re: pygtk - the output of bash commands on a TextView widget

hum, so there is no way I could have a user inputting commands in one terminal and the results coming out on the other?

UPDATE:
The problem is then I need to know all the commands that I want to run when I launch the terminal, there seems to be no way to make it interactactive(sp) or monitor the success of each command I issue to the terminal is there?

Ex like a gtk.ProgressBar that monitored each command like synaptic does.

Thanks for the feedback
__________________
Edward A Robinson -- www.earobinson.org

Last edited by earobinson; September 4th, 2007 at 06:00 PM..
earobinson is offline   Reply With Quote
Old September 4th, 2007   #8
tseliot
Python Enthusiast
 
tseliot's Avatar
 
Join Date: May 2005
Location: Lecce, Italy
Beans: 6,288
Send a message via AIM to tseliot
Re: pygtk - the output of bash commands on a TextView widget

Quote:
Originally Posted by earobinson View Post
hum, so there is no way I could have a user inputting commands in one terminal and the results coming out on the other?

UPDATE:
The problem is then I need to know all the commands that I want to run when I launch the terminal, there seems to be no way to make it interactactive(sp) or monitor the success of each command I issue to the terminal is there?
you can monitor the success of each command by keeping a log and parsing each line you append to the log on the fly.

I'm not sure as to what you mean by interactive.
tseliot is offline   Reply With Quote
Old September 4th, 2007   #9
earobinson
Ubuntu Member
 
earobinson's Avatar
 
Join Date: Jan 2005
Location: Toronto, Ontario, Canada
Beans: 2,566
Ubuntu 9.04 Jaunty Jackalope
Re: pygtk - the output of bash commands on a TextView widget

I guess the idea in my head was two guis, one that you can type commands into and the other (the terminal) that displays the commands.

maybe this isent possible?

But it seems like I should be able to issue commands to the terminal and get a response from them.

I have made a simple example., What I would like is to push the press me button and then not be able to interact any more until the command has ran to completion.

eventually I would add a gtk.entry and be able to run any command.

I guess what your saying is I should send but the program I want to run and then a output log file to signify that the program is done?
Attached Files
File Type: gz example.tar.gz (889 Bytes, 9 views)
__________________
Edward A Robinson -- www.earobinson.org
earobinson is offline   Reply With Quote
Old September 4th, 2007   #10
tseliot
Python Enthusiast
 
tseliot's Avatar
 
Join Date: May 2005
Location: Lecce, Italy
Beans: 6,288
Send a message via AIM to tseliot
Re: pygtk - the output of bash commands on a TextView widget

Quote:
Originally Posted by earobinson View Post
I have made a simple example., What I would like is to push the press me button and then not be able to interact any more until the command has ran to completion.

eventually I would add a gtk.entry and be able to run any command.

I guess what your saying is I should send but the program I want to run and then a output log file to signify that the program is done?
You can gray out the text are when the user can type the command, pass the command to VTE as you know and then look for a certain line (which you will set) in the log.

here is an example from Envy:
Code:
def log_output(self,term):
        column,row = self.term.get_cursor_position()
        if self.r != row:
            off = row-self.r
            text = self.term.get_text_range(row-off,0,row-1,-1,self.capture_text)
            self.r=row
            text = text.strip()
            if "\n" not in text or text[-1] != "\n":
                text += "\n"
            ##self.logged += text
            self.completetxt.append(text)
            a = self.logged
            error = re.compile('.*ENVY ERROR.*\n')
            success = re.compile('.*ENVY:.*Operation.*Completed.*')
            for line in self.completetxt:#self.logged:
                m1 = error.match(line)
                m2 = success.match(line)
            if m1:
                self.error = 'error'
                sep = ''#write a logfile
                logtext = sep.join(self.completetxt)
                logfile = open(self.logtxtfile, 'w')
                logfile.write(logtext)
                logfile.close()
                self.errorcheck()
            if m2:
                self.error = 'noerror'
                sep = ''#write a logfile
                logtext = sep.join(self.completetxt)
                logfile = open(self.logtxtfile, 'w')
                logfile.write(logtext)
                logfile.close()
                self.errorcheck()
in this example the method looks for strings such as "ENVY ERROR" or "ENVY:.*Operation.*Completed" and acts accordingly. For example if self.error == 'noerror' you can trigger a specific action.

You might use strings such as "DONE", etc. so that if you find such a string you can allow the user to type commands again in the text input area.


EDIT: in case you're wonder what self.term is: self.term = vte.Terminal()

Last edited by tseliot; September 4th, 2007 at 06:47 PM..
tseliot is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:01 PM.


vBulletin ©2000 - 2010, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. bilberry