Exscript.util.template module

Executing Exscript templates on a connection.

Exscript.util.template.eval(conn, string, strip_command=True, **kwargs)[source]

Compiles the given template and executes it on the given connection. Raises an exception if the compilation fails.

if strip_command is True, the first line of each response that is received after any command sent by the template is stripped. For example, consider the following template:

ls -1{extract /(\S+)/ as filenames}
{loop filenames as filename}
    touch $filename
{end}

If strip_command is False, the response, (and hence, the `filenames’ variable) contains the following:

ls -1
myfile
myfile2
[...]

By setting strip_command to True, the first line is ommitted.

Parameters:
  • conn (Exscript.protocols.Protocol) – The connection on which to run the template.
  • string (string) – The template to compile.
  • strip_command (bool) – Whether to strip the command echo from the response.
  • kwargs (dict) – Variables to define in the template.
Return type:

dict

Returns:

The variables that are defined after execution of the script.

Exscript.util.template.eval_file(conn, filename, strip_command=True, **kwargs)[source]

Convenience wrapper around eval() that reads the template from a file instead.

Parameters:
  • conn (Exscript.protocols.Protocol) – The connection on which to run the template.
  • filename (string) – The name of the template file.
  • strip_command (bool) – Whether to strip the command echo from the response.
  • kwargs (dict) – Variables to define in the template.
Exscript.util.template.paste(conn, string, **kwargs)[source]

Compiles the given template and executes it on the given connection. This function differs from eval() such that it does not wait for a prompt after sending each command to the connected host. That means that the script can no longer read the response of the host, making commands such as `extract’ or `set_prompt’ useless.

The function raises an exception if the compilation fails, or if the template contains a command that requires a response from the host.

Parameters:
  • conn (Exscript.protocols.Protocol) – The connection on which to run the template.
  • string (string) – The template to compile.
  • kwargs (dict) – Variables to define in the template.
Return type:

dict

Returns:

The variables that are defined after execution of the script.

Exscript.util.template.paste_file(conn, filename, **kwargs)[source]

Convenience wrapper around paste() that reads the template from a file instead.

Parameters:
  • conn (Exscript.protocols.Protocol) – The connection on which to run the template.
  • filename (string) – The name of the template file.
  • kwargs (dict) – Variables to define in the template.
Exscript.util.template.test(string, **kwargs)[source]

Compiles the given template, and raises an exception if that failed. Does nothing otherwise.

Parameters:
  • string (string) – The template to compile.
  • kwargs (dict) – Variables to define in the template.
Exscript.util.template.test_file(filename, **kwargs)[source]

Convenience wrapper around test() that reads the template from a file instead.

Parameters:
  • filename (string) – The name of the template file.
  • kwargs (dict) – Variables to define in the template.
Exscript.util.template.test_secure(string, **kwargs)[source]

Like test(), but makes sure that each function that is used in the template has the Exscript.stdlib.util.safe_function decorator. Raises Exscript.interpreter.Exception.PermissionError if any function lacks the decorator.

Parameters:
  • string (string) – The template to compile.
  • kwargs (dict) – Variables to define in the template.