File: redirection.rst

package info (click to toggle)
python-sh 2.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 900 kB
  • sloc: python: 4,157; makefile: 25
file content (65 lines) | stat: -rw-r--r-- 1,460 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
.. _redirection:

Redirection
===========

sh can redirect the STDOUT and STDERR of a process to many different types of
targets, using the :ref:`_out <out>` and :ref:`_err <err>` special kwargs.

Filename
--------

If a string is used, it is assumed to be a filename.  The filename is opened as
"wb", meaning truncate-write and binary mode.

.. code-block:: python

    import sh
    sh.ifconfig(_out="/tmp/interfaces")

.. seealso:: :ref:`faq_append`

File-like Object
----------------

You may also use any object that supports ``.write(data)``, like
:class:`io.StringIO`:

.. code-block:: python

    import sh
    from io import StringIO

    buf = StringIO()
    sh.ifconfig(_out=buf)
    print(buf.getvalue())

.. _red_func:

Function Callback
-----------------

A callback function may also be used as a target.  The function must conform to
one of three signatures:

.. py:function:: fn(data)
    :noindex:

    The function takes just the chunk of data from the process.

.. py:function:: fn(data, stdin_queue)
    :noindex:

    In addition to the previous signature, the function also takes a
    :class:`queue.Queue`, which may be used to communicate programmatically with
    the process.

.. py:function:: fn(data, stdin_queue, process)
    :noindex:

    In addition to the previous signature, the function takes a
    :class:`weakref.ref` to the :ref:`OProc <oproc_class>` object.

.. seealso:: :ref:`callbacks`

.. seealso:: :ref:`tutorial2`