Here is a basic (actually a "fancy" version since a generator)
```python
#!/usr/bin/env python
# emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
# ex: set sts=4 ts=4 sw=4 noet:
from datalad.runner.protocol import GeneratorMixIn
from datalad.runner.utils import (
AssemblingDecoderMixIn,
)
from datalad.cmd import (
GitWitlessRunner,
StdOutErrCapture,
)
class GeneratorStdOutErrCapture(GeneratorMixIn,
AssemblingDecoderMixIn,
StdOutErrCapture):
"""
Generator-runner protocol that captures and yields stdout and stderr.
"""
def __init__(self):
GeneratorMixIn.__init__(self)
AssemblingDecoderMixIn.__init__(self)
StdOutErrCapture.__init__(self)
def pipe_data_received(self, fd, data):
if fd in (1, 2):
print(f"processing data: {data} for {fd}")
self.send_result((fd, self.decode(fd, data, self.encoding)))
else:
StdOutErrCapture.pipe_data_received(self, fd, data)
if __name__ == '__main__':
import sys, os
git_runner = GitWitlessRunner()
generator = git_runner.run(
sys.argv[1:],
protocol=GeneratorStdOutErrCapture,
env = os.environ.copy(),
)
for out in generator:
print(f"generator output: {out}")
```
which on this simple script
#!/usr/bin/env python
from time import sleep
import sys
for i in range(5):
sys.stdout.write(f"stdout {i}\n")
sys.stderr.write(f"stderr {i}\n")
sleep(0.1)
which, if stdout is not piped would just interleave
Here is a basic (actually a "fancy" version since a generator)
```python #!/usr/bin/env python # emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*- # ex: set sts=4 ts=4 sw=4 noet: from datalad.runner.protocol import GeneratorMixIn from datalad.runner.utils import ( AssemblingDecoderMixIn, ) from datalad.cmd import ( GitWitlessRunner, StdOutErrCapture, ) class GeneratorStdOutErrCapture(GeneratorMixIn, AssemblingDecoderMixIn, StdOutErrCapture): """ Generator-runner protocol that captures and yields stdout and stderr. """ def __init__(self): GeneratorMixIn.__init__(self) AssemblingDecoderMixIn.__init__(self) StdOutErrCapture.__init__(self) def pipe_data_received(self, fd, data): if fd in (1, 2): print(f"processing data: {data} for {fd}") self.send_result((fd, self.decode(fd, data, self.encoding))) else: StdOutErrCapture.pipe_data_received(self, fd, data) if __name__ == '__main__': import sys, os git_runner = GitWitlessRunner() generator = git_runner.run( sys.argv[1:], protocol=GeneratorStdOutErrCapture, env = os.environ.copy(), ) for out in generator: print(f"generator output: {out}") ```which on this simple script
which, if stdout is not piped would just interleave
and if stdout redirected, wouldn't be flushed I guess so we get
So datalad runner produces