From 903062baf196560e55ad47efd9204db763ecacb7 Mon Sep 17 00:00:00 2001 From: Sylvain Gaudan Date: Fri, 27 Sep 2024 17:15:02 +0200 Subject: [PATCH] factorize print table --- arlas/cli/collections.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/arlas/cli/collections.py b/arlas/cli/collections.py index f3077fc..52b71c9 100644 --- a/arlas/cli/collections.py +++ b/arlas/cli/collections.py @@ -20,9 +20,7 @@ def configuration(config: str = typer.Option(help="Name of the ARLAS configurati def list_collections(): config = variables["arlas"] collections = Service.list_collections(config) - tab = PrettyTable(collections[0], sortby="name", align="l") - tab.add_rows(collections[1:]) - print(tab) + __print_table(collections[0], collections[1:], sortby="name") @collections.command(help="Count the number of hits within a collection (or all collection if not provided)") @@ -31,9 +29,7 @@ def count( ): config = variables["arlas"] count = Service.count_collection(config, collection) - tab = PrettyTable(count[0], sortby="collection name", align="l") - tab.add_rows(count[1:]) - print(tab) + __print_table(count[0], count[1:], sortby="collection name") @collections.command(help="Describe a collection") @@ -42,14 +38,10 @@ def describe( ): config = variables["arlas"] fields = Service.describe_collection(config, collection) - tab = PrettyTable(fields[0], sortby="field name", align="l") - tab.add_rows(fields[1:]) - print(tab) + __print_table(fields[0], fields[1:], sortby="field name") fields = Service.metadata_collection(config, collection) - tab = PrettyTable(fields[0], align="l") - tab.add_rows(fields[1:]) - print(tab) + __print_table(fields[0], fields[1:], sortby=None) @collections.command(help="Set collection visibility to public") @@ -108,9 +100,7 @@ def set_field_display_name( ): config = variables["arlas"] fields = Service.set_collection_field_display_name(config, collection, field_path, display_name) - tab = PrettyTable(fields[0], align="l") - tab.add_rows(fields[1:]) - print(tab) + __print_table(fields[0], fields[1:], sortby=None) @collections.command(help="Display a sample of a collection") @@ -179,3 +169,10 @@ def create( geometry_path=geometry_path, date_path=date_path) print("Collection {} created on {}".format(collection, config)) + + +def __print_table(field_names: list[str], rows, sortby: str = None): + tab = PrettyTable(field_names, sortby=sortby, align="l") + tab.add_rows(rows) + print(tab) +