From 7184016bd4edb05959eadcaea30f198ce3cde7a8 Mon Sep 17 00:00:00 2001 From: Pallab Pain Date: Sat, 27 May 2023 01:16:13 +0530 Subject: [PATCH] feat(static-route): updates spinner and other implementations --- riocli/static_route/__init__.py | 5 ++-- riocli/static_route/create.py | 28 +++++++++++++++------- riocli/static_route/delete.py | 42 +++++++++++++++++++++++---------- riocli/static_route/inspect.py | 24 ++++++++++++++----- riocli/static_route/list.py | 36 ++++++++++++++++++++++++---- riocli/static_route/model.py | 9 ++++--- riocli/static_route/open.py | 13 +++++++--- riocli/static_route/util.py | 25 +++----------------- 8 files changed, 119 insertions(+), 63 deletions(-) diff --git a/riocli/static_route/__init__.py b/riocli/static_route/__init__.py index 5d318f65..490b728d 100644 --- a/riocli/static_route/__init__.py +++ b/riocli/static_route/__init__.py @@ -14,6 +14,7 @@ import click from click_help_colors import HelpColorsGroup +from riocli.constants import Colors from riocli.static_route.create import create_static_route from riocli.static_route.delete import delete_static_route from riocli.static_route.inspect import inspect_static_route @@ -24,8 +25,8 @@ @click.group( invoke_without_command=False, cls=HelpColorsGroup, - help_headers_color='yellow', - help_options_color='green', + help_headers_color=Colors.YELLOW, + help_options_color=Colors.GREEN, ) def static_route() -> None: """ diff --git a/riocli/static_route/create.py b/riocli/static_route/create.py index 2acfb9e9..a70c0af5 100644 --- a/riocli/static_route/create.py +++ b/riocli/static_route/create.py @@ -1,4 +1,4 @@ -# Copyright 2021 Rapyuta Robotics +# Copyright 2023 Rapyuta Robotics # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -12,22 +12,32 @@ # See the License for the specific language governing permissions and # limitations under the License. import click -from click_spinner import spinner +from click_help_colors import HelpColorsCommand from riocli.config import new_client +from riocli.constants import Colors, Symbols +from riocli.utils.spinner import with_spinner -@click.command('create') +@click.command( + 'create', + cls=HelpColorsCommand, + help_headers_color=Colors.YELLOW, + help_options_color=Colors.GREEN, +) @click.argument('prefix', type=str) -def create_static_route(prefix: str) -> None: +@with_spinner(text="Creating static route...") +def create_static_route(prefix: str, spinner=None) -> None: """ - Creates a new instance of static route + Creates a new static route """ try: client = new_client() - with spinner(): - route = client.create_static_route(prefix) - click.secho("Static Route created successfully for URL {}".format(route.urlString), fg='green') + route = client.create_static_route(prefix) + spinner.text = click.style( + 'Static Route created successfully for URL {}'.format(route.urlString), fg=Colors.GREEN) + spinner.green.ok(Symbols.SUCCESS) except Exception as e: - click.secho(str(e), fg='red') + spinner.text = click.style('Failed to create static route: {}'.format(e), fg=Colors.RED) + spinner.red.fail(Symbols.ERROR) raise SystemExit(1) diff --git a/riocli/static_route/delete.py b/riocli/static_route/delete.py index a858ae92..45f39e95 100644 --- a/riocli/static_route/delete.py +++ b/riocli/static_route/delete.py @@ -1,4 +1,4 @@ -# Copyright 2021 Rapyuta Robotics +# Copyright 2023 Rapyuta Robotics # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -12,30 +12,46 @@ # See the License for the specific language governing permissions and # limitations under the License. import click -from click_spinner import spinner +from click_help_colors import HelpColorsCommand from riocli.config import new_client +from riocli.constants import Colors, Symbols from riocli.static_route.util import name_to_guid +from riocli.utils.spinner import with_spinner -@click.command('delete') +@click.command( + 'delete', + cls=HelpColorsCommand, + help_headers_color=Colors.YELLOW, + help_options_color=Colors.GREEN, +) @click.option('--force', '-f', is_flag=True, default=False, help='Skip confirmation') @click.argument('static-route', type=str) @name_to_guid -def delete_static_route(static_route: str, static_route_guid: str, force: bool) -> None: +@with_spinner(text="Deleting static route...") +def delete_static_route( + static_route: str, + static_route_guid: str, + force: bool, + spinner=None, +) -> None: """ - Deletes the static route resource from the Platform + Deletes a static route """ - - if not force: - click.confirm('Deleting static route {} ({})'.format(static_route, static_route_guid), - abort=True) + with spinner.hidden(): + if not force: + click.confirm( + 'Deleting static route {} ({})'.format( + static_route, static_route_guid), abort=True) try: client = new_client() - with spinner(): - client.delete_static_route(static_route_guid) - click.secho('Static Route deleted successfully!', fg='green') + client.delete_static_route(static_route_guid) + spinner.text = click.style( + 'Static Route deleted successfully ', fg=Colors.GREEN) + spinner.green.ok(Symbols.SUCCESS) except Exception as e: - click.secho(str(e), fg='red') + spinner.text = click.style('Failed to delete static route: {}'.format(e), fg=Colors.RED) + spinner.red.fail(Symbols.ERROR) raise SystemExit(1) diff --git a/riocli/static_route/inspect.py b/riocli/static_route/inspect.py index 5cd65174..a6b10a20 100644 --- a/riocli/static_route/inspect.py +++ b/riocli/static_route/inspect.py @@ -1,4 +1,4 @@ -# Copyright 2021 Rapyuta Robotics +# Copyright 2023 Rapyuta Robotics # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -12,21 +12,33 @@ # See the License for the specific language governing permissions and # limitations under the License. import click +from click_help_colors import HelpColorsCommand from rapyuta_io.clients.static_route import StaticRoute from riocli.config import new_client +from riocli.constants import Colors from riocli.static_route.util import name_to_guid from riocli.utils import inspect_with_format -@click.command('inspect') +@click.command( + 'inspect', + cls=HelpColorsCommand, + help_headers_color=Colors.YELLOW, + help_options_color=Colors.GREEN, +) @click.option('--format', '-f', 'format_type', - type=click.Choice(['json', 'yaml'], case_sensitive=True), default='yaml') + type=click.Choice(['json', 'yaml'], case_sensitive=True), + default='yaml') @click.argument('static-route', type=str) @name_to_guid -def inspect_static_route(format_type: str, static_route: str, static_route_guid: str) -> None: +def inspect_static_route( + format_type: str, + static_route: str, + static_route_guid: str +) -> None: """ - Inspect the static route resource + Inspect a static route """ try: client = new_client() @@ -34,7 +46,7 @@ def inspect_static_route(format_type: str, static_route: str, static_route_guid: data = make_static_route_inspectable(route) inspect_with_format(data, format_type) except Exception as e: - click.secho(str(e), fg='red') + click.secho(str(e), fg=Colors.RED) raise SystemExit(1) diff --git a/riocli/static_route/list.py b/riocli/static_route/list.py index ab77cde2..b4317fa4 100644 --- a/riocli/static_route/list.py +++ b/riocli/static_route/list.py @@ -1,4 +1,4 @@ -# Copyright 2021 Rapyuta Robotics +# Copyright 2023 Rapyuta Robotics # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -11,13 +11,23 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +from typing import List + import click +from click_help_colors import HelpColorsCommand +from rapyuta_io.clients.static_route import StaticRoute from riocli.config import new_client -from riocli.static_route.util import repr_static_routes +from riocli.constants import Colors +from riocli.utils import tabulate_data -@click.command('list') +@click.command( + 'list', + cls=HelpColorsCommand, + help_headers_color=Colors.YELLOW, + help_options_color=Colors.GREEN, +) def list_static_routes() -> None: """ List the static routes in the selected project @@ -25,7 +35,23 @@ def list_static_routes() -> None: try: client = new_client() routes = client.get_all_static_routes() - repr_static_routes(routes) + _display_routes_list(routes) except Exception as e: - click.secho(str(e), fg='red') + click.secho(str(e), fg=Colors.RED) raise SystemExit(1) + + +def _display_routes_list(routes: List[StaticRoute]) -> None: + headers = ['Route ID', 'Name', 'URL', 'Creator', 'CreatedAt'] + + data = [] + for route in routes: + data.append([ + route.guid, + route.urlPrefix, + route.urlString, + route.creator, + route.CreatedAt, + ]) + + tabulate_data(data, headers) diff --git a/riocli/static_route/model.py b/riocli/static_route/model.py index cf09dc21..698b2001 100644 --- a/riocli/static_route/model.py +++ b/riocli/static_route/model.py @@ -1,4 +1,4 @@ -# Copyright 2022 Rapyuta Robotics +# Copyright 2023 Rapyuta Robotics # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -25,8 +25,11 @@ def __init__(self, *args, **kwargs): self.update(*args, **kwargs) def find_object(self, client: Client) -> bool: - _, static_route = self.rc.find_depends({'kind': 'staticroute', - 'nameOrGUID': self.metadata.name}) + _, static_route = self.rc.find_depends({ + 'kind': 'staticroute', + 'nameOrGUID': self.metadata.name + }) + if not static_route: return False diff --git a/riocli/static_route/open.py b/riocli/static_route/open.py index 348596b4..3162ae0b 100644 --- a/riocli/static_route/open.py +++ b/riocli/static_route/open.py @@ -1,4 +1,4 @@ -# Copyright 2021 Rapyuta Robotics +# Copyright 2023 Rapyuta Robotics # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -12,12 +12,19 @@ # See the License for the specific language governing permissions and # limitations under the License. import click +from click_help_colors import HelpColorsCommand from riocli.config import new_client +from riocli.constants import Colors from riocli.static_route.util import name_to_guid -@click.command('open') +@click.command( + 'open', + cls=HelpColorsCommand, + help_headers_color=Colors.YELLOW, + help_options_color=Colors.GREEN, +) @click.argument('static-route', type=str) @name_to_guid def open_static_route(static_route, static_route_guid) -> None: @@ -29,5 +36,5 @@ def open_static_route(static_route, static_route_guid) -> None: route = client.get_static_route(static_route_guid) click.launch(url='https://{}'.format(route.urlString), wait=False) except Exception as e: - click.secho(str(e), fg='red') + click.secho(str(e), fg=Colors.RED) raise SystemExit(1) diff --git a/riocli/static_route/util.py b/riocli/static_route/util.py index 5cefecb5..c0b702fb 100644 --- a/riocli/static_route/util.py +++ b/riocli/static_route/util.py @@ -1,4 +1,4 @@ -# Copyright 2021 Rapyuta Robotics +# Copyright 2023 Rapyuta Robotics # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,9 +14,7 @@ import functools import typing -import click from rapyuta_io import Client -from rapyuta_io.clients.static_route import StaticRoute from riocli.config import new_client @@ -59,23 +57,6 @@ def find_static_route_guid(client: Client, name: str) -> str: raise StaticRouteNotFound() -def repr_static_routes(routes: typing.List[StaticRoute]) -> None: - header = '{:<36} {:<25} {:36} {:36} {:32}'.format( - 'Static Route ID', - 'Name', - 'Full URL', - 'Creator', - 'Created At', - ) - click.echo(click.style(header, fg='yellow')) - for route in routes: - click.secho( - '{:<36} {:<25} {:36} {:36} {:32}'. - format(route.guid, route.urlPrefix, route.urlString, route.creator, - route.CreatedAt)) - - class StaticRouteNotFound(Exception): - def __init__(self, message='secret not found'): - self.message = message - super().__init__(self.message) + def __init__(self): + super().__init__('static route not found')