formatter

Subpackage with formatter classes used to render and output results.

Each formatter has a Formatter.produce_output() method taking one argument which gets rendered and printed to output stream. Each formatter expects different argument, please refer to doc string of particular class.

class lmi.scripts.common.formatter.CsvFormatter(stream, padding=0, no_headings=False)[source]

Renders data in a csv (Comma-separated values) format.

This formatter supports following commands:
class lmi.scripts.common.formatter.ErrorFormatter(stream, padding=4)[source]
Render error strings for particular host. Supported commands:
class lmi.scripts.common.formatter.Formatter(stream, padding=0, no_headings=False)[source]

Base formatter class.

It produces string representation of given argument and prints it.

This formatter supports following commands:

Parameters:
  • stream (file) – Output stream.
  • padding (integer) – Number of leading spaces to print at each line.
  • no_headings (boolean) – If table headings should be omitted.
encoding[source]

Try to determine encoding for output terminal.

Returns:Encoding used to encode unicode strings.
Return type:string
host_counter = None

counter of hosts printed

line_counter = None

counter of lines producted for current table

print_host(hostname)[source]

Prints header for new host.

Parameters:hostname (string) – Hostname to print.
print_line(line, *args, **kwargs)[source]

Prints single line. Output message is prefixed with padding spaces, formated and printed to output stream.

Parameters:
  • line (string) – Message to print, it can contain markers for other arguments to include according to format_spec language. Please refer to Format Specification Mini-Language in python documentation.
  • args (list) – Positional arguments to format() function of line argument.
  • kwargs (dictionary) – Keyword arguments to format() function.
produce_output(data)[source]

Render and print given data.

Data can be also instance of FormatterCommand, see documentation of this class for list of allowed commands.

This shall be overridden by subclasses.

Parameters:data – Any value to print. Subclasses may specify their requirements for this argument. It can be also am instance of FormatterCommand.
render_value(val)[source]

Rendering function for single value.

Parameters:val – Any value to render.
Returns:Value converted to its string representation.
Return type:str
table_counter = None

counter of tables produced for current host

class lmi.scripts.common.formatter.ListFormatter(stream, padding=0, no_headings=False)[source]

Base formatter taking care of list of items. It renders input data in a form of table with mandatory column names at the beginning followed by items, one occupying single line (row).

This formatter supports following commands:

The command must be provided as content of one row. This row is then not printed and the command is executed.

This class should be subclassed to provide nice output.

print_header()[source]

Print table header.

print_row(data)[source]

Print data row. Optionaly print header, if requested.

Parameters:data (tuple) – Data to print.
print_table_title(title)[source]

Prints title of next tale.

Parameters:title (string) – Title to print.
print_text_row(row)[source]

Print data row without any header.

Parameters:row (tuple) – Data to print.
produce_output(rows)[source]

Prints list of rows.

There can be a FormatterCommand instance instead of a row. See documentation of this class for list of allowed commands.

Parameters:rows (list, generator or command.FormatterCommand) – List of rows to print.
class lmi.scripts.common.formatter.ShellFormatter(stream, padding=0, no_headings=False)[source]

Specialization of SingleFormatter having its output executable as a shell script.

This formatter supports following commands:
class lmi.scripts.common.formatter.SingleFormatter(stream, padding=0, no_headings=False)[source]

Meant to render and print attributes of single object. Attributes are rendered as a list of assignments of values to variables (attribute names).

This formatter supports following commands:
produce_output(data)[source]

Render and print attributes of single item.

There can be a FormatterCommand instance instead of a data. See documentation of this class for list of allowed commands.

Parameters:data (tuple or dict) – Is either a pair of property names with list of values or a dictionary with property names as keys. Using the pair allows to order the data the way it should be printing. In the latter case the properties will be sorted by the property names.
class lmi.scripts.common.formatter.TableFormatter(stream, padding=0, no_headings=False, min_column_sizes=False)[source]

Print nice human-readable table to terminal.

To print the table nicely aligned, the whole table must be populated first. Therefore this formatter stores all rows locally and does not print them until the table is complete. Column sizes are computed afterwards and the table is printed at once.

This formatter supports following commands:

The command must be provided as content of one row. This row is then not printed and the command is executed.

Parameters:min_column_sizes (dictionary) – Dictionary of minimal column sizes, where keys are column numbers starting from 0, and values are minimal column sizes.
print_host(hostname)[source]

Prints header for new host.

Parameters:hostname (string) – Hostname to print.
print_row(data)[source]

Print data row.

Parameters:data (tuple) – Data to print.
print_table_title(title)[source]

Prints title of next tale.

Parameters:title (string) – Title to print.
produce_output(rows)[source]

Prints list of rows.

There can be FormatterCommand instance instead of a row. See documentation of this class for list of allowed commands.

Parameters:rows (list or generator) – List of rows to print.
lmi.scripts.common.formatter.get_terminal_width()[source]

Get the number of columns of current terminal if attached to it. It defaults to 79 characters.

Returns:Number of columns of attached terminal.
Return type:integer