amoffat/sh — pull request #153 by runeksvendsen (2013-09-23 17:29:44+00:00)
Title: Add ability to pass custom data to output callback functions

This:

from sh import mpv
p = mpv("video.mp4", _err=output_callback, _bg=True)

will call the output_callback function every time mpv outputs a line to stderr.

The thing is, I need some objects to be available to the output_callback function. In my case, I need a socket and a custom class to be passed to it.

I'm opening this issue to have a discussion of what the best way of achieving this is.

Do we want to be able to supply different objects to, for example, `_err` and `_out`?

If not, this patch should suffice.

This patch is a premilinary attempt to enable passing of custom
objects/data to the callback functions in _out and/or _err.

The way it's made, the callback function *must* include the `stdin`
and `process` arguments, because otherwise it would require redoing
the code that counts arguments of the callback function.

An example callback function could look like this:

def output_callback(line, stdin, process, socket, data_class):

and the data to this callback function would be passed like this:

from sh import mpv
mpv("file.mp4", _err=output_callback, _bg=True, _callback_kwargs=\
{'socket' : socket, 'data_class' : data_class})

I am not sure if a more elegant solution exists, so I created this
to at least start the debate on what the most elegant solution is,
or if this will suffice.