Results 1 to 4 of 4

Thread: display output of a command line utility in GUI

  1. #1
    Join Date
    Jul 2007
    Location
    The Bavarian Alps
    Beans
    129
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    display output of a command line utility in GUI

    The problem in a nutshell – I am writing a GUI wrapper for a command line utility (GPSBabel) and want to show the output of the command line on an error. I am a very inexperienced Python programmer.

    Calling the program from the command line as follows
    Code:
    neill@NeillsPC:$ gpsbabel -igarmin -f/dev/ttyS1 -o zzz -F/home/neill/temp
    produces the following output to the screen
    [ERROR] GPS_Packet_Read: Timeout. No data received.
    GARMIN:Can't init /dev/ttyS1

    This is what I would like to show the user of the GUI but I can find no way of doing so.

    I though that sending the output to the file would be a good start. From the command line I changed the command to
    Code:
    gpsbabel -igarmin -f/dev/ttyS1 -o zzz -F/home/neill/temp > /home/neill/temp/err.log  2>&1
    which sent everything to the file err.log as expected.

    In my program I call GPSBabel with
    Code:
    result = os.spawnlp(os.P_WAIT, 'gpsbabel', 'gpsbabel', dataString, inputFormatStr, inputFileStr, outputFormatStr, outputFileStr)
    and can not see how to change that to be the same as my command line option.

    I also tried
    Code:
    import sys
    saveout = sys.stdout 
    fsock = open('out.log', 'w')
    sys.stdout = fsock
    with stdout and stderr before calling the spawn.

    Now I am stumped. Please help me some one even if only by telling me that it is impossible.

    Thanks!
    Neill

  2. #2

    Re: display output of a command line utility in GUI

    I want to show the output of the command line on an error.
    Personally, I prefer to use the subprocess module.

    Code:
    import subprocess
    process = subprocess.Popen("your command here", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, bufsize=0)
    print process.stdout.read()
    print process.stderr.read()
    Last edited by strider1551; August 17th, 2007 at 09:34 PM. Reason: Added the link to the library documentation

  3. #3
    Join Date
    Jul 2007
    Location
    The Bavarian Alps
    Beans
    129
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Re: display output of a command line utility in GUI

    Thank you for that.
    I just had a quick look at the code and it looks complicated.
    I will get back to you in a few hours/days once I have worked out what I need to do.
    As I said "thanks"

    Neill!

  4. #4
    Join Date
    Jul 2007
    Location
    The Bavarian Alps
    Beans
    129
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Re: display output of a command line utility in GUI

    It works

    Code:
    # the command line to call
    progString = 'gpsbabel ' + dataString + inputFormatStr + ' ' + inputFileStr + ' ' + \
    outputFormatStr + ' ' + outputFileStr				
    # Information what we are calling
    self.textEditInfo.append('Command: ' + progString + '\n')
    # Call GPSBabel
    extProcess = subprocess.Popen(progString, shell=True, stderr=subprocess.PIPE)
    #  and read any errors
    errStr = extProcess.stderr.read()
    self.textEditInfo.append('GPSBabel says: ' + errStr + '\n')
    Thank you so much for your simple but very effective tip.
    I found spawn starnge but subprocess acted exactly as I expected.

    What held me up for an hour was that all the little strings that go to make up progString are input from lineEdits and I was using lineEdit.text() which would not work - lineEdit.text().ascii() would though.

    Thanks!
    Neill

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •