Internals¶
Imports¶
Those elements are imported when you use from chut import *
:
from subprocess import PIPE
from subprocess import STDOUT
from copy import deepcopy
from ConfigObject import ConfigObject
from contextlib import contextmanager
try:
from fabric import api as fabric
except ImportError:
HAS_FABRIC = False
else: # pragma: no cover
if 'nosetests' in sys.argv[0]:
HAS_FABRIC = False
else:
HAS_FABRIC = True
Also noticed that commands which don’t use pipes are listed here.
Pipe¶
-
class
chut.
Pipe
(*args, **kwargs)[source]¶ A pipe object. Represent a set of one or more commands.
-
failed
¶ True if one or more process failed
-
classmethod
map
(args, pool_size=None, stop_on_failure=False, **kwargs)[source]¶ Run a batch of the same command and manage a pool of processes for you
-
returncodes
¶ A list of return codes of all processes launched by the pipe
-
stderr
¶ combined stderr of all processes
-
stdout
¶ standard output of the pipe. A file descriptor or an iteraror
-
succeeded
¶ True if all processes succeeded
-
Input¶
You can use a python string as input:
>>> print(sh.stdin(b'gawel\nfoo') | grep('gawel'))
gawel
The input can be a file but the file is not streamed by stdin()
.
Notice that the file must be open in binary mode (rb
):
>>> print(sh.stdin(open('README.rst', 'rb'))
... | grep('Chut') | sh.head('-n1'))
Chut!
Output¶
You can get the output as string (see Stdout
):
>>> output = str(cat('README.rst') | grep('Chut'))
>>> output = (cat('README.rst') | grep('Chut'))()
As an iterator (iterate over each lines of the output):
>>> chut_stdout = cat('README.rst') | grep('Chut') | sh.head('-n1')
And can use some redirection:
>>> ret = chut_stdout > '/tmp/chut.txt'
>>> ret.succeeded
True
>>> print(cat('/tmp/chut.txt'))
Chut!
>>> ret = chut_stdout >> '/tmp/chut.txt'
>>> ret.succeeded
True
>>> print(cat('/tmp/chut.txt'))
Chut!
Chut!
Parentheses are needed with >>
(due to the way the python operator work):
cat('README.rst') | grep >> '/tmp/chut.txt' # wont work
(cat('README.rst') | grep) >> '/tmp/chut.txt' # work
Ini files¶
-
chut.
ini
(filename, **defaults)[source]¶ Load a .ini file in a ConfigObject. Dont raise if the file does not exist
Example:
>>> from chut import ini
>>> config = ini('/tmp/chut.ini')
>>> config.my = dict(key='value')
>>> config.write()
>>> config = ini('/tmp/chut.ini')
>>> print(config.my.key)
value