octoprint.util.commandline#
CommandlineCaller()
#
The CommandlineCaller is a utility class that allows running command line commands while logging their stdout and stderr via configurable callback functions.
Callbacks are expected to have a signature matching
.. code-block:: python
def callback(*lines): do_something_with_the_passed_lines()
The class utilizes sarge underneath.
Example:
.. code-block:: python
from octoprint.util.commandline import CommandLineCaller, CommandLineError
def log(prefix, *lines): for line in lines: print("{} {}".format(prefix, line))
def log_stdout(lines): log(">>>", lines)
def log_stderr(lines): log("!!!", lines)
def log_call(lines) log("---", lines)
caller = CommandLineCaller() caller.on_log_call = log_call caller.on_log_stdout = log_stdout caller.on_log_stderr = log_stderr
try: caller.checked_call(["some", "command", "with", "parameters"]) except CommandLineError as err: print("Command returned {}".format(err.returncode)) else: print("Command finished successfully")
on_log_call = lambda : None
instance-attribute
#
Callback for the called command line
on_log_stderr = lambda : None
instance-attribute
#
Callback for stderr output
on_log_stdout = lambda : None
instance-attribute
#
Callback for stdout output
call(command: Union[str, List[str], Tuple[str]], delimiter: bytes = b'\n', buffer_size: int = -1, logged: bool = True, **kwargs: bool) -> Tuple[Optional[int], List[str], List[str]]
#
checked_call(command: Union[str, List[str], Tuple[str]], **kwargs: Union[str, List[str], Tuple[str]]) -> Tuple[int, List[str], List[str]]
#
CommandlineError(returncode, stdout, stderr)
#
clean_ansi(line: Union[str, bytes]) -> Union[str, bytes]
#
Removes ANSI control codes from line.
Note: This function also still supports an input of bytes, leading to an
output of bytes. This if for reasons of backwards compatibility only,
should no longer be used and considered to be deprecated and to be removed in
a future version of OctoPrint. A warning will be logged.
Parameters:
Returns:
.. versionchanged:: 1.8.0
Usage as clean_ansi(line: bytes) -> bytes is now deprecated and will be removed
in a future version of OctoPrint.