common

calabash.common defines some helpful pipeline components, borrowed straight from UNIX but adapted to work with arbitrary Python objects instead of just simple text streams.

Defined in this module:

calabash.common.cat(*args, **kwargs)[source]

Read a file. Passes directly through to a call to open().

>>> src_file = __file__.replace('.pyc', '.py')
>>> for line in cat(src_file):
...     if line.startswith('def cat'):
...          print repr(line)
'def cat(*args, **kwargs):\n'
calabash.common.curl(*args, **kwargs)[source]

Fetch a URL, yielding output line-by-line.

>>> UNLICENSE = 'http://unlicense.org/UNLICENSE'
>>> for line in curl(UNLICENSE): 
...     print line,
This is free and unencumbered software released into the public domain.
...
calabash.common.echo(*args, **kwargs)[source]

Yield a single item. Equivalent to iter([item]), but nicer-looking.

>>> list(echo(1))
[1]
>>> list(echo('hello'))
['hello']
calabash.common.filter(*args, **kwargs)[source]

Only pass through items for which predicate(item) is truthy.

>>> list(xrange(5) | filter(lambda x: x % 2 == 0))
[0, 2, 4]
calabash.common.grep(*args, **kwargs)[source]

Filter strings on stdin for the given regex (uses re.search()).

>>> list(iter(['cat', 'cabbage', 'conundrum', 'cathedral']) | grep(r'^ca'))
['cat', 'cabbage', 'cathedral']
calabash.common.map(*args, **kwargs)[source]

Map each item on stdin through the given function.

>>> list(xrange(5) | map(lambda x: x + 2))
[2, 3, 4, 5, 6]
calabash.common.pretty_printer(*args, **kwargs)[source]

Pretty print each item on stdin and pass it straight through.

>>> for item in iter([{'a': 1}, ['b', 'c', 3]]) | pretty_printer():
...     pass
{'a': 1}
['b', 'c', 3]
calabash.common.sed(*args, **kwargs)[source]

Apply re.sub() to each line on stdin with the given pattern/repl.

>>> list(iter(['cat', 'cabbage']) | sed(r'^ca', 'fu'))
['fut', 'fubbage']

Upon encountering a non-matching line of input, sed() will pass it through as-is. If you want to change this behaviour to only yield lines which match the given pattern, pass exclusive=True:

>>> list(iter(['cat', 'nomatch']) | sed(r'^ca', 'fu'))
['fut', 'nomatch']
>>> list(iter(['cat', 'nomatch']) | sed(r'^ca', 'fu', exclusive=True))
['fut']
calabash.common.sh(*args, **kwargs)[source]

Run a shell command, send it input, and produce its output.

>>> print ''.join(echo("h\ne\nl\nl\no") | sh('sort -u'))
e
h
l
o
<BLANKLINE>
>>> for line in sh('echo Hello World'):
...     print line,
Hello World
>>> for line in sh('false', check_success=True):
...     print line, 
Traceback (most recent call last):
...
CalledProcessError: Command '['false']' returned non-zero exit status 1

Related Topics

This Page