From 0ea32364c519ebedba99a1b178212d38179e34f9 Mon Sep 17 00:00:00 2001 From: Moul <moul@moul.re> Date: Fri, 14 Oct 2022 18:37:16 +0200 Subject: [PATCH] Move MutuallyExclusiveOption from cli_tools to tools (#330) --- silkaj/cli_tools.py | 40 ---------------------------------------- silkaj/money/transfer.py | 12 ++++++------ silkaj/tools.py | 25 +++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 46 deletions(-) delete mode 100644 silkaj/cli_tools.py diff --git a/silkaj/cli_tools.py b/silkaj/cli_tools.py deleted file mode 100644 index 2268e58d..00000000 --- a/silkaj/cli_tools.py +++ /dev/null @@ -1,40 +0,0 @@ -# Copyright 2016-2022 Maël Azimi <m.a@moul.re> -# -# Silkaj is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Silkaj is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with Silkaj. If not, see <https://www.gnu.org/licenses/>. - -from typing import Any - -from click import Context, Option, UsageError - - -# pylint: disable=too-few-public-methods -class MutuallyExclusiveOption(Option): - def __init__(self, *args: Any, **kwargs: Any) -> None: - self.mutually_exclusive = set(kwargs.pop("mutually_exclusive", [])) - _help = kwargs.get("help", "") - if self.mutually_exclusive: - ex_str = ", ".join(self.mutually_exclusive) - kwargs[ - "help" - ] = f"{_help} NOTE: This argument is mutually exclusive with arguments: [{ex_str}]." - super().__init__(*args, **kwargs) - - def handle_parse_result(self, ctx: Context, opts: Any, args: Any) -> Any: - if self.mutually_exclusive.intersection(opts) and self.name in opts: - arguments = ", ".join(self.mutually_exclusive) - raise UsageError( - f"Usage: `{self.name}` is mutually exclusive with arguments `{arguments}`." - ) - - return super().handle_parse_result(ctx, opts, args) diff --git a/silkaj/money/transfer.py b/silkaj/money/transfer.py index c1b89059..e7af1ab6 100644 --- a/silkaj/money/transfer.py +++ b/silkaj/money/transfer.py @@ -31,7 +31,7 @@ from duniterpy.documents import ( ) from duniterpy.key import SigningKey -from silkaj import auth, cli_tools, network, public_key, tools, tui +from silkaj import auth, network, public_key, tools, tui from silkaj.blockchain import tools as bc_tools from silkaj.constants import ( CENT_MULT_TO_UNIT, @@ -67,7 +67,7 @@ NBR_ISSUERS = 1 multiple=True, type=click.FloatRange(MINIMAL_ABSOLUTE_TX_AMOUNT), help=f"Quantitative amount(s):\n-a <amount>\nMinimum amount is {MINIMAL_ABSOLUTE_TX_AMOUNT}.", - cls=cli_tools.MutuallyExclusiveOption, + cls=tools.MutuallyExclusiveOption, mutually_exclusive=["amountsud", "allsources", "file_path"], ) @click.option( @@ -77,14 +77,14 @@ NBR_ISSUERS = 1 multiple=True, type=click.FloatRange(MINIMAL_RELATIVE_TX_AMOUNT), help=f"Relative amount(s):\n-d <amount_UD>\nMinimum amount is {MINIMAL_RELATIVE_TX_AMOUNT}", - cls=cli_tools.MutuallyExclusiveOption, + cls=tools.MutuallyExclusiveOption, mutually_exclusive=["amounts", "allsources", "file_path"], ) @click.option( "--allSources", is_flag=True, help="Send all sources to one recipient", - cls=cli_tools.MutuallyExclusiveOption, + cls=tools.MutuallyExclusiveOption, mutually_exclusive=["amounts", "amountsud", "file_path"], ) @click.option( @@ -96,7 +96,7 @@ NBR_ISSUERS = 1 Sending to many recipients is possible:\n\ * With one amount, all will receive the amount\n\ * With many amounts (one per recipient)", - cls=cli_tools.MutuallyExclusiveOption, + cls=tools.MutuallyExclusiveOption, mutually_exclusive=["file_path"], ) @click.option( @@ -105,7 +105,7 @@ Sending to many recipients is possible:\n\ "-f", help="File’s path containing a list of amounts in absolute or \ relative reference and recipients’ pubkeys", - cls=cli_tools.MutuallyExclusiveOption, + cls=tools.MutuallyExclusiveOption, mutually_exclusive=["recipients", "amounts", "amountsUD", "allsources"], ) @click.option("--comment", "-c", default="", help="Comment") diff --git a/silkaj/tools.py b/silkaj/tools.py index 99b31f2f..1a5c810c 100644 --- a/silkaj/tools.py +++ b/silkaj/tools.py @@ -15,6 +15,9 @@ import functools import sys +from typing import Any + +import click from silkaj.blockchain.tools import get_blockchain_parameters from silkaj.constants import FAILURE_EXIT_STATUS, G1_SYMBOL, GTEST_SYMBOL @@ -31,3 +34,25 @@ def get_currency_symbol() -> str: def message_exit(message: str) -> None: print(message) sys.exit(FAILURE_EXIT_STATUS) + + +# pylint: disable=too-few-public-methods +class MutuallyExclusiveOption(click.Option): + def __init__(self, *args: Any, **kwargs: Any) -> None: + self.mutually_exclusive = set(kwargs.pop("mutually_exclusive", [])) + _help = kwargs.get("help", "") + if self.mutually_exclusive: + ex_str = ", ".join(self.mutually_exclusive) + kwargs[ + "help" + ] = f"{_help} NOTE: This argument is mutually exclusive with arguments: [{ex_str}]." + super().__init__(*args, **kwargs) + + def handle_parse_result(self, ctx: click.Context, opts: Any, args: Any) -> Any: + if self.mutually_exclusive.intersection(opts) and self.name in opts: + arguments = ", ".join(self.mutually_exclusive) + raise click.UsageError( + f"Usage: `{self.name}` is mutually exclusive with arguments `{arguments}`." + ) + + return super().handle_parse_result(ctx, opts, args) -- GitLab