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):
Unified interface for printing data in CLI.
Add column of data thet will be displayed
Parameters: |
|
---|
Print one record
Parameters: | item (dict) – data to be printed |
---|
Print list of records
Parameters: | items (list of dicts) – data to be printed |
---|
Sets label for a fancy header that is printed above the data.
Parameters: | heading (string) – |
---|
Sets formatting strategy
Parameters: | strategy (PrinterStrategy) – strategy that is used for formatting the output. |
---|
Printer strategies have to implement methods print_item and print_items that take care of printing a single record or a list of them. They both take heading label, definition of columns (list of dictionaries) and the data.
Two strategies are used in CLI:
Strategy of formatting the data and printing them on the output.
Print one item
Parameters: |
|
---|
Print list of items
Parameters: |
|
---|
Column Parameters Both print strategies support following column parameters:
Name | Type | Description |
---|---|---|
attr_name | string | Mandatory param, key to the data dictionary. |
name | string | Label for the column. By default it is generated from the attr_name. Eg. “product_id” is translated to “Product Id”. |
multiline | bool | Flag to mark values that can possibly hold strings with more lines. Strategies can handle them differently then. |
formatter | function | A filter function for pre-formatting of values. Must take only one parameter which is the value that should be formatted. It is expected to return a string. |
item_formatter | function | A filter function simmilar to formatter. The difference is in the parameter. This one takes the whole data dictionary. But still it must return single string. |
value | string | Can be used to force static value. |
show_with | strategy or a tuple of strategies | Allows to restrict what strategies the column can be printed with. |
Prints data into a grid that can be grepped easily. String to divide the columns can be set optionally.
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 |
|