sc3nb.process_handling

Module for process handling.

Module Contents

Function List

find_executable

Looks for executable in os $PATH or specified path

kill_processes

Kill processes with the same path for the executable.

Class List

Process

Class for starting a executable and communication with it.

Content

sc3nb.process_handling._LOGGER[source]
sc3nb.process_handling.ANSI_ESCAPE[source]
sc3nb.process_handling.ALLOWED_PARENTS = ('scide', 'python', 'tox')[source]
sc3nb.process_handling.find_executable(executable: str, search_path: str = None, add_to_path: bool = False)[source]

Looks for executable in os $PATH or specified path

Parameters:
executablestr

Executable to be found

search_pathstr, optional

Path at which to look for, by default None

add_to_pathbool, optional

Wether to add the provided path to os $PATH or not, by default False

Returns:
str

Full path to executable

Raises:
FileNotFoundError

Raised if executable cannot be found

sc3nb.process_handling.kill_processes(exec_path, allowed_parents: Optional[tuple] = None)[source]

Kill processes with the same path for the executable.

If allowed_parent is provided it will be searched in the names of the parent processes of the process with the executable path before terminating. If it is found the process won’t be killed.

Parameters:
exec_pathstr

path of the executable to kill

allowed_parentstr, optional

parents name of processes to keep, by default None

exception sc3nb.process_handling.ProcessTimeout(executable, output, timeout, expected)[source]

Bases: Exception

Process Timeout Exception

Initialize self. See help(type(self)) for accurate signature.

class sc3nb.process_handling.Process(executable: str, programm_args: Optional[Sequence[str]] = None, executable_path: str = None, console_logging: bool = True, kill_others: bool = True, allowed_parents: Sequence[str] = None)[source]

Class for starting a executable and communication with it.

Parameters:
executablestr

Name of executable to start

programm_argsOptional[Sequence[str]], optional

Arguments to program start with Popen, by default None

executable_pathstr, optional

Path with executalbe, by default system PATH

console_loggingbool, optional

Flag for controlling console logging, by default True

kill_othersbool, optional

Flag for controlling killing of other executables with the same name. This is useful when processes where left over, by default True

allowed_parentsSequence[str], optional

Sequence of parent names that won’t be killed when kill_others is True, by default None

Overview:

_read_loop

read

Reads current output from output queue until expect is found

empty

Empties output queue.

write

Send input to process

kill

Kill the process.

__del__

__repr__

Return repr(self).

_read_loop()[source]
read(expect: Optional[str] = None, timeout: float = 3) str[source]

Reads current output from output queue until expect is found

Parameters:
expectstr, optional

str that we expect to find, by default None

timeoutfloat, optional

timeout in seconds for waiting for output, by default 3

Returns:
str

Output of process.

Raises:
ProcessTimeout

If neither output nor expect is found

empty() None[source]

Empties output queue.

write(input_str: str) None[source]

Send input to process

Parameters:
input_strstr

Input to be send to process

Raises:
RuntimeError

If writing to process fails

kill() int[source]

Kill the process.

Returns:
int

return code of process

__del__()[source]
__repr__() str[source]

Return repr(self).