Printing unified output

For unified output of Katello CLI we use an instance of a printer class. Printer makes a common interface that supports multiple formatting strategies. Currently a verbose strategy and a grep friendly strategy is implemented. The first is by default used to display single dictionaries while the latter is used to print lists of dictionaries.

Before the records are printed it is necessary to restrict what columns will go to the output. This is done by function add_column which allows to set various column parameters.

Printer offers following methods (only the public ones listed):

Printer

class katello.client.utils.printer.Printer(strategy=None)

Unified interface for printing data in CLI.

add_column(attr_name, name=None, **kwargs)

Add column of data thet will be displayed

Parameters:
  • attr_name (string) – key to data hash
  • name (string) – label for the column. It is generated automatically from attr_name if it’s not set.
  • kwargs (dict) – other parameters that are passed to the printer strategy
print_item(item)

Print one record

Parameters:item (dict) – data to be printed
print_items(items)

Print list of records

Parameters:items (list of dicts) – data to be printed
set_header(heading)

Sets label for a fancy header that is printed above the data.

Parameters:heading (string) –
set_strategy(strategy)

Sets formatting strategy

Parameters:strategy (PrinterStrategy) – strategy that is used for formatting the output.

VerboseStrategy

class katello.client.utils.printer.VerboseStrategy

GrepStrategy

class katello.client.utils.printer.GrepStrategy(delimiter=None)

Prints data into a grid that can be grepped easily. String to divide the columns can be set optionally.

Usage example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from katello.client.utils.printer import Printer, VerboseStrategy, GrepStrategy

repo = get_repo() #returns a dictionary with repo data
# {
#   "id": 1,
#   "pulp_id": "ACME_Corporation-zoo-zoo",
#   "name": "zoo",
#   "package_count": 32,
#   "url": "http://tstrachota.fedorapeople.org/dummy_repos/zoo/",
#   "last_sync": "2012-04-16T00:27:37+02:00",
#   "sync_state": "finished"
# }

printer = Printer()
printer.set_strategy(VerboseStrategy()) # set a desired strategy
#printer.set_strategy(GrepStrategy(delimiter="|"))

printer.add_column('id') # 3 columns that only print the values
printer.add_column('name')
printer.add_column('package_count')
printer.add_column('url', show_with=VerboseStrategy) # this column will be printed only in the verbose mode
printer.add_column('last_sync', show_with=VerboseStrategy, formatter=format_sync_time) # only in verbose mode but preformatted by format_sync_time
printer.add_column('sync_state', name=_("Progress"), show_with=VerboseStrategy, formatter=format_sync_state) # same as above but with forced label

printer.set_header(_("Information About Repo %s") % repo['id'])

printer.print_item(repo)

Verbose strategy will print:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
-------------------------------------------------------------------------------------------------
Information About Repo 1
-------------------------------------------------------------------------------------------------

Id:            1
Name:          zoo
Package Count: 32
Url:           http://tstrachota.fedorapeople.org/dummy_repos/zoo/
Last Sync:     2012/04/16 00:27:37
Progress:      Finished

With grep strategy it will print:

1
2
3
4
5
6
-------------------------------------------------------------------------------------------------
Information About Repo 1

| Id  | Name  | Package Count  |
-------------------------------------------------------------------------------------------------
| 1   | zoo   | 32             |

Table Of Contents

This Page