API: ExecResult

class exec_helpers.ExecResult[source]

Command execution result.

__init__(cmd, stdin=None, stdout=None, stderr=None, exit_code=0xDEADBEEF, *, started=None)[source]
Parameters:
  • cmd (str) – command
  • stdin (typing.Union[bytes, str, bytearray, None]) – STDIN
  • stdout (typing.Optional[typing.Iterable[bytes]]) – binary STDOUT
  • stderr (typing.Optional[typing.Iterable[bytes]]) – binary STDERR
  • exit_code (typing.Union[int, ExitCodes]) – Exit code. If integer - try to convert to BASH enum.
  • started (typing.Optional[datetime.datetime]) – Timestamp of command start
stdout_lock

threading.RLock Lock object for thread-safe operation.

New in version 2.2.0.

stderr_lock

threading.RLock Lock object for thread-safe operation.

New in version 2.2.0.

timestamp

typing.Optional(datetime.datetime) Timestamp

set_timestamp()[source]

Set timestamp if empty.

This will block future object changes.

New in version 4.0.0.

cmd

str Command

stdin

typing.Optional[str] Stdin input as string.

stdout

typing.Tuple[bytes, ...] Stdout output as list of binaries.

stderr

typing.Tuple[bytes, ...] Stderr output as list of binaries.

stdout_bin

bytearray Stdout in binary format.

stderr_bin

bytearray Stderr in binary format.

stdout_str

str Stdout output as string.

stderr_str

str Stderr output as string.

stdout_brief

str Brief stdout output (mostly for exceptions).

stderr_brief

str Brief stderr output (mostly for exceptions).

stdout_lines

Get lines by indexes

Return type:LinesAccessProxy

Usage example:

lines: str = res.stdout_lines[<line_number>, <index_start>:<index_end>, ...]
stderr_lines

Get lines by indexes

Return type:LinesAccessProxy
exit_code

Return(exit) code of command.

Return type:typing.Union[int, ExitCodes]
started

datetime.datetime Timestamp of command start.

New in version 4.0.0.

stdout_json

JSON from stdout.

Return type:typing.Any
Raises:DeserializeValueError – STDOUT can not be deserialized as JSON
stdout_yaml

YAML from stdout.

Return type:

typing.Any

Raises:
  • DeserializeValueError – STDOUT can not be deserialized as YAML
  • AttributeError – no any yaml parser installed
stdout_xml

XML from stdout

Return type:

xml.etree.ElementTree.Element

Raises:
  • DeserializeValueError – STDOUT can not be deserialized as XML
  • AttributeError – defusedxml is not installed
stdout_lxml

XML from stdout using lxml.

Return type:

lxml.etree.Element

Raises:
  • DeserializeValueError – STDOUT can not be deserialized as XML
  • AttributeError – lxml is not installed

Note

Can be insecure.

read_stdout(src=None, log=None, verbose=False)[source]

Read stdout file-like object to stdout.

Parameters:
  • src (typing.Optional[typing.Iterable]) – source
  • log (typing.Optional[logging.Logger]) – logger
  • verbose (bool) – use log.info instead of log.debug

Changed in version 1.2.0: - src can be None

read_stderr(src=None, log=None, verbose=False)[source]

Read stderr file-like object to stderr.

Parameters:
  • src (typing.Optional[typing.Iterable]) – source
  • log (typing.Optional[logging.Logger]) – logger
  • verbose (bool) – use log.info instead of log.debug

Changed in version 1.2.0: - src can be None

class exec_helpers.LinesAccessProxy

Lines access proxy.

__init__(self, data)

Lines access proxy.

Parameters:data (typing.Sequence[bytes]) – data to work with.
__getitem__(self, item)

Access magic.

Parameters:item (typing.Union[int, slice, typing.Iterable[typing.Union[int, slice, ellipsis]]]) – index
Returns:Joined selected lines
Return type:str
Raises:TypeError – Unexpected key
__str__(self)

Get string for debug purposes.

__len__(self)

Data len.