From d1ce07b03b3e5e3c1728fe2518349dc03b26bc53 Mon Sep 17 00:00:00 2001 From: Rafael Sarmiento Date: Tue, 12 Sep 2023 13:28:45 +0200 Subject: [PATCH 1/5] add callbacl for yaml config --- firecrest/cli/__init__.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/firecrest/cli/__init__.py b/firecrest/cli/__init__.py index fd40c61..1d00048 100644 --- a/firecrest/cli/__init__.py +++ b/firecrest/cli/__init__.py @@ -6,6 +6,7 @@ # import logging import typer +import yaml import firecrest as fc @@ -97,6 +98,21 @@ def version_callback(value: bool): raise typer.Exit() +def config_callback(ctx: typer.Context, param: typer.CallbackParam, value: str): + if value: + try: + with open(value, 'r') as f: + config = yaml.safe_load(f) + + ctx.default_map = ctx.default_map or {} + ctx.default_map.update(config) + print(help(ctx.default_map.update)) + except Exception as ex: + raise typer.BadParameter(str(ex)) + + return value + + @app.command(rich_help_panel="Status commands") def services( name: Optional[str] = typer.Option( @@ -222,6 +238,11 @@ def tasks( @app.command(rich_help_panel="Utilities commands") def ls( + config: str = typer.Option("config.yaml", + callback=config_callback, + is_eager=True, + hidden=True + ), machine: str = typer.Argument( ..., help="The machine name where the filesystem belongs to." ), From 6a9c444821cbac054cd9ca53bf0a9bd83e8b78de Mon Sep 17 00:00:00 2001 From: Rafael Sarmiento Date: Mon, 20 Nov 2023 09:45:38 +0100 Subject: [PATCH 2/5] add env var --- firecrest/cli/__init__.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/firecrest/cli/__init__.py b/firecrest/cli/__init__.py index f43ee00..652c555 100644 --- a/firecrest/cli/__init__.py +++ b/firecrest/cli/__init__.py @@ -100,7 +100,6 @@ def version_callback(value: bool): def config_parent_load_callback(ctx: typer.Context, param: typer.CallbackParam, value: str): - print('getting config from parent') ctx.default_map = ctx.parent.default_map @@ -112,7 +111,6 @@ def config_callback(ctx: typer.Context, param: typer.CallbackParam, value: str): ctx.default_map = ctx.default_map or {} ctx.default_map.update(config) - print('loading config') except Exception as ex: raise typer.BadParameter(str(ex)) @@ -244,7 +242,7 @@ def tasks( @app.command(rich_help_panel="Utilities commands") def ls( - config: str = typer.Option(None, + config_from_parent: str = typer.Option(None, callback=config_parent_load_callback, is_eager=True, hidden=True @@ -1176,10 +1174,11 @@ def delete( @app.callback() def main( - config: str = typer.Option("config.yaml" if os.path.isfile("config.yaml") else None, + config: Optional[str] = typer.Option( + None, + envvar="FIRECREST_CONFIG", callback=config_callback, is_eager=True, - hidden=True ), version: Optional[bool] = typer.Option( None, From a16b00e9504f710d1503770ce71fb70baaae0992 Mon Sep 17 00:00:00 2001 From: Rafael Sarmiento Date: Mon, 20 Nov 2023 09:59:53 +0100 Subject: [PATCH 3/5] add config to all commands --- firecrest/cli/__init__.py | 135 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) diff --git a/firecrest/cli/__init__.py b/firecrest/cli/__init__.py index d04f485..3f6dc3d 100644 --- a/firecrest/cli/__init__.py +++ b/firecrest/cli/__init__.py @@ -293,6 +293,11 @@ def ls( @app.command(rich_help_panel="Utilities commands") def mkdir( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", @@ -317,6 +322,11 @@ def mkdir( @app.command(rich_help_panel="Utilities commands") def mv( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", @@ -337,6 +347,11 @@ def mv( @app.command(rich_help_panel="Utilities commands") def chmod( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", @@ -357,6 +372,11 @@ def chmod( @app.command(rich_help_panel="Utilities commands") def chown( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", @@ -381,6 +401,11 @@ def chown( @app.command(rich_help_panel="Utilities commands") def cp( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", @@ -401,6 +426,11 @@ def cp( @app.command(rich_help_panel="Utilities commands") def file( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", @@ -420,6 +450,11 @@ def file( @app.command(rich_help_panel="Utilities commands") def stat( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", @@ -509,6 +544,11 @@ def stat( @app.command(rich_help_panel="Utilities commands") def symlink( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", @@ -529,6 +569,11 @@ def symlink( @app.command(rich_help_panel="Utilities commands") def rm( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", @@ -557,6 +602,11 @@ def rm( @app.command(rich_help_panel="Utilities commands") def checksum( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", @@ -576,6 +626,11 @@ def checksum( @app.command(rich_help_panel="Utilities commands") def head( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", @@ -631,6 +686,11 @@ def head( @app.command(rich_help_panel="Utilities commands") def tail( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", @@ -686,6 +746,11 @@ def tail( @app.command(rich_help_panel="Utilities commands") def whoami( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( None, "-s", @@ -711,6 +776,11 @@ class TransferType(str, Enum): @app.command(rich_help_panel="Storage commands") def download( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", @@ -762,6 +832,11 @@ def download( @app.command(rich_help_panel="Storage commands") def upload( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", @@ -824,6 +899,11 @@ def upload( @app.command(rich_help_panel="Compute commands") def submit( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", "--system", help="The name of the system.", envvar="FIRECREST_SYSTEM" ), @@ -854,6 +934,11 @@ def submit( @submit_template_app.command("mv") def submit_mv( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", @@ -897,6 +982,11 @@ def submit_mv( @submit_template_app.command("cp") def submit_cp( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", @@ -940,6 +1030,11 @@ def submit_cp( @submit_template_app.command("rsync") def submit_rsync( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", @@ -983,6 +1078,11 @@ def submit_rsync( @submit_template_app.command("rm") def submit_rm( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", @@ -1023,6 +1123,11 @@ def submit_rm( @app.command(rich_help_panel="Compute commands") def poll( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", "--system", help="The name of the system.", envvar="FIRECREST_SYSTEM" ), @@ -1069,6 +1174,11 @@ def poll( @app.command(rich_help_panel="Compute commands") def poll_active( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", "--system", help="The name of the system.", envvar="FIRECREST_SYSTEM" ), @@ -1107,6 +1217,11 @@ def poll_active( @app.command(rich_help_panel="Compute commands") def cancel( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", "--system", help="The name of the system.", envvar="FIRECREST_SYSTEM" ), @@ -1122,6 +1237,11 @@ def cancel( @reservation_app.command(rich_help_panel="Reservation commands") def list( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", "--system", help="The name of the system.", envvar="FIRECREST_SYSTEM" ), @@ -1137,6 +1257,11 @@ def list( @reservation_app.command(rich_help_panel="Reservation commands") def create( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", "--system", help="The name of the system.", envvar="FIRECREST_SYSTEM" ), @@ -1167,6 +1292,11 @@ def create( @reservation_app.command(rich_help_panel="Reservation commands") def update( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", "--system", help="The name of the system.", envvar="FIRECREST_SYSTEM" ), @@ -1197,6 +1327,11 @@ def update( @reservation_app.command(rich_help_panel="Reservation commands") def delete( + config_from_parent: str = typer.Option(None, + callback=config_parent_load_callback, + is_eager=True, + hidden=True + ), system: str = typer.Option( ..., "-s", "--system", help="The name of the system.", envvar="FIRECREST_SYSTEM" ), From 3e44bf1526f9d122dde32253b91f0c0e81b23df9 Mon Sep 17 00:00:00 2001 From: Rafael Sarmiento Date: Mon, 20 Nov 2023 10:06:15 +0100 Subject: [PATCH 4/5] add yaml to deps --- pyproject.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 11d7198..09228aa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,8 @@ dependencies = [ "PyJWT>=2.4.0", "typer[all]~=0.7.0", "packaging>=21.0", - "httpx>=0.24.0" + "httpx>=0.24.0", + "PyYAML>=5.1" ] [project.urls] From 47fde206baee47d36bcbb0d8ced18d4b40c65297 Mon Sep 17 00:00:00 2001 From: Eirini Koutsaniti Date: Mon, 20 Nov 2023 10:33:14 +0100 Subject: [PATCH 5/5] Fix mypy error --- firecrest/cli/__init__.py | 3 +-- pyproject.toml | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/firecrest/cli/__init__.py b/firecrest/cli/__init__.py index 3f6dc3d..3907979 100644 --- a/firecrest/cli/__init__.py +++ b/firecrest/cli/__init__.py @@ -5,7 +5,6 @@ # SPDX-License-Identifier: BSD-3-Clause # import logging -import os import typer import yaml @@ -100,7 +99,7 @@ def version_callback(value: bool): def config_parent_load_callback(ctx: typer.Context, param: typer.CallbackParam, value: str): - ctx.default_map = ctx.parent.default_map + ctx.default_map = ctx.parent.default_map # type: ignore def config_callback(ctx: typer.Context, param: typer.CallbackParam, value: str): diff --git a/pyproject.toml b/pyproject.toml index 09228aa..928c4d4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,7 +48,8 @@ test = [ "mypy~=0.991", "types-requests~=2.28.11", "pytest-httpserver~=1.0.6", - "pytest-asyncio>=0.21.1" + "pytest-asyncio>=0.21.1", + "types-PyYAML>=5.1" ] docs = [ "sphinx>=4.0",