KProcess

KDE 4.5 PyKDE API Reference

- KDE's Python API

# KProcess Class Reference

`from PyKDE4.kdecore import *`

Inherits: QProcess → QIODevice → QObject
Subclasses: KPtyProcess

## Detailed Description

\class KProcess kprocess.h <KProcess>

Child process invocation, monitoring and control.

This class extends QProcess by some useful functionality, overrides some defaults with saner values and wraps parts of the API into a more accessible one. This is the preferred way of spawning child processes in KDE; don't use QProcess directly.

Author:
  Oswald Buddenhagen <ossi@kde.org>

## Enumerations
OutputChannelMode | { SeparateChannels, MergedChannels, ForwardedChannels, OnlyStdoutChannel, OnlyStderrChannel }
## Methods
| __init__ (self, QObject parent=0)
| clearEnvironment (self)
| clearProgram (self)
int | execute (self, int msecs=-1)
KProcess | operator << (self, QString arg)
KProcess | operator << (self, QStringList args)
KProcess.OutputChannelMode | outputChannelMode (self)
int | pid (self)
QStringList | program (self)
| setEnv (self, QString name, QString value, bool overwrite=1)
| setNextOpenMode (self, QIODevice.OpenMode mode)
| setOutputChannelMode (self, KProcess.OutputChannelMode mode)
| setProgram (self, QString exe, QStringList args=QStringList())
| setProgram (self, QStringList argv)
| setShellCommand (self, QString cmd)
| start (self)
int | startDetached (self)
| unsetEnv (self, QString name)
## Static Methods
int | execute (QString exe, QStringList args=QStringList(), int msecs=-1)
int | execute (QStringList argv, int msecs=-1)
int | startDetached (QString exe, QStringList args=QStringList())
int | startDetached (QStringList argv)

## Method Documentation

__init__ | ( | self, |
|  | QObject | parent=0
| ) |  |  |

Constructor

clearEnvironment | ( |  | self ) |

Empties the process' environment.

Note that LD_LIBRARY_PATH/DYLD_LIBRARY_PATH is automatically added on *NIX.

This function must be called before starting the process.

clearProgram | ( |  | self ) |

Clear the program and command line argument list.

int execute | ( | self, |
|  | int | msecs=-1
| ) |  |  |

Parameters:

  | argv | the program to execute and the command line arguments for the program, one per list element
  | msecs | time to wait for process to exit before killing it

Returns:
  -2 if the process could not be started, -1 if it crashed, otherwise its exit code

KProcess operator << | ( | self, |
|  | QString | arg
| ) |  |  |

Parameters:

  | args | the arguments to add

Returns:
  a reference to this KProcess

KProcess operator << | ( | self, |
|  | QStringList | args
| ) |  |  |

Parameters:

  | args | the arguments to add

Returns:
  a reference to this KProcess

KProcess.OutputChannelMode outputChannelMode | ( |  | self ) |

Query how the output channels of the child process are handled.

Returns:
  the output channel handling mode

int pid | ( |  | self ) |

Obtain the process' ID as known to the system.

Unlike with QProcess.pid(), this is a real PID also on Windows.

This function can be called only while the process is running. It cannot be applied to detached processes.

Returns:
  the process ID

QStringList program | ( |  | self ) |

Obtain the currently set program and arguments.

Returns:
  a list, the first element being the program, the remaining ones being command line arguments to the program.

setEnv | ( | self, |
|  | QString | name,
|  | QString | value,
|  | bool | overwrite=1
| ) |  |  |

Adds the variable name to the process' environment.

This function must be called before starting the process.

Parameters:

  | name | the name of the environment variable
  | value | the new value for the environment variable
  | overwrite | if false and the environment variable is already set, the old value will be preserved

setNextOpenMode | ( | self, |
|  | QIODevice.OpenMode | mode
| ) |  |  |

Set the QIODevice open mode the process will be opened in.

This function must be called before starting the process, obviously.

Parameters:

  | mode | the open mode. Note that this mode is automatically "reduced" according to the channel modes and redirections. The default is QIODevice.ReadWrite.

setOutputChannelMode | ( | self, |
|  | KProcess.OutputChannelMode | mode
| ) |  |  |

Set how to handle the output channels of the child process.

The default is ForwardedChannels, which is unlike in QProcess. Do not request more than you actually handle, as this output is simply lost otherwise.

This function must be called before starting the process.

Parameters:

  | mode | the output channel handling mode

setProgram | ( | self, |
|  | QString | exe,
|  | QStringList | args=QStringList()
| ) |  |  |

Parameters:

  | argv | the program to execute and the command line arguments for the program, one per list element

setProgram | ( | self, |
|  | QStringList | argv
| ) |  |  |

Parameters:

  | argv | the program to execute and the command line arguments for the program, one per list element

setShellCommand | ( | self, |
|  | QString | cmd
| ) |  |  |

Set a command to execute through a shell (a POSIX sh on *NIX and cmd.exe on Windows).

Using this for anything but user-supplied commands is usually a bad idea, as the command's syntax depends on the platform. Redirections including pipes, etc. are better handled by the respective functions provided by QProcess.

If KProcess determines that the command does not really need a shell, it will trasparently execute it without one for performance reasons.

This function must be called before starting the process, obviously.

Parameters:

  | cmd | the command to execute through a shell. The caller must make sure that all filenames etc. are properly quoted when passed as argument. Failure to do so often results in serious security holes. See KShell.quoteArg().

start | ( |  | self ) |

Start the process.

See also:
  QProcess.start(const QString &, const QStringList &, OpenMode)

int startDetached | ( |  | self ) |

Parameters:

  | argv | the program to start and the command line arguments for the program, one per list element

Returns:
  the PID of the started process or 0 on error

unsetEnv | ( | self, |
|  | QString | name
| ) |  |  |

Removes the variable name from the process' environment.

This function must be called before starting the process.

Parameters:

  | name | the name of the environment variable

## Static Method Documentation

int execute | ( | QString | exe,
|  | QStringList | args=QStringList(),
|  | int | msecs=-1
| ) |  |  |

Parameters:

  | argv | the program to execute and the command line arguments for the program, one per list element
  | msecs | time to wait for process to exit before killing it

Returns:
  -2 if the process could not be started, -1 if it crashed, otherwise its exit code

int execute | ( | QStringList | argv,
|  | int | msecs=-1
| ) |  |  |

Parameters:

  | argv | the program to execute and the command line arguments for the program, one per list element
  | msecs | time to wait for process to exit before killing it

Returns:
  -2 if the process could not be started, -1 if it crashed, otherwise its exit code

int startDetached | ( | QString | exe,
|  | QStringList | args=QStringList()
| ) |  |  |

Parameters:

  | argv | the program to start and the command line arguments for the program, one per list element

Returns:
  the PID of the started process or 0 on error

int startDetached | ( | QStringList | argv
| ) |  |  |

Parameters:

  | argv | the program to start and the command line arguments for the program, one per list element

Returns:
  the PID of the started process or 0 on error

## Enumeration Documentation

OutputChannelMode

Modes in which the output channels can be opened.

Enumerator:

  SeparateChannels = QProcess::SeparateChannels |
  MergedChannels = QProcess::MergedChannels |
  ForwardedChannels = QProcess::ForwardedChannels |
  OnlyStdoutChannel |
  OnlyStderrChannel |