runops

This module gathers list/line operations

mrun

class textops.mrun(context={})

Run multiple commands from the input text and return execution output

This works like textops.run except that each line of the input text will be used as a command.
The input text must be a list of strings (list, generator, or newline separated), not a list of lists. Commands will be executed inside a shell.
If a context dict is specified, commands are formatted with that context (str.format)
Parameters:context (dict) – The context to format the command to run
Yields:str – the execution output

Examples

>>> cmds = 'mkdir -p /tmp/textops_tests_run\n'
>>> cmds+= 'cd /tmp/textops_tests_run;touch f1 f2 f3\n'
>>> cmds+= 'ls /tmp/textops_tests_run'
>>> print(cmds | mrun().tostr())
f1
f2
f3
>>> cmds=['mkdir -p /tmp/textops_tests_run',
... 'cd /tmp/textops_tests_run; touch f1 f2 f3']
>>> cmds.append('ls /tmp/textops_tests_run')
>>> print(cmds | mrun().tostr())
f1
f2
f3
>>> print(cmds >> mrun())
['f1', 'f2', 'f3']
>>> cmds = ['ls {path}', 'echo "Cool !"']
>>> print(cmds | mrun({'path':'/tmp/textops_tests_run'}).tostr())
f1
f2
f3
Cool !

run

class textops.run(context={})

Run the command from the input text and return execution output

This text operation use subprocess.Popen to call the command.
If the command is a string, it will be executed within a shell.
If the command is a list (the command and its arguments), the command is executed without a shell.
If a context dict is specified, the command is formatted with that context (str.format)
Parameters:context (dict) – The context to format the command to run
Yields:str – the execution output

Examples

>>> cmd = 'mkdir -p /tmp/textops_tests_run;\
... cd /tmp/textops_tests_run; touch f1 f2 f3; ls'
>>> print(cmd | run().tostr())
f1
f2
f3
>>> print(cmd >> run())
['f1', 'f2', 'f3']
>>> print(['ls', '/tmp/textops_tests_run'] | run().tostr())
f1
f2
f3
>>> print(['ls', '{path}'] | run({'path':'/tmp/textops_tests_run'}).tostr())
f1
f2
f3

xrun

class textops.xrun(cmd, context={}, defvalue='unknwon')

Run the command formatted with the context taken from the input text

Parameters:
  • command (str) – The command pattern to run (formatted against the context)
  • context (dict) – additional context dictionary
  • defvalue (str or callable) – default string to display when a key or an index is unreachable.
Yields:

str – the execution output

Examples

to come…