Coverage for silkaj/tools.py: 100%

28 statements  

« prev     ^ index     » next       coverage.py v7.6.3, created at 2024-10-18 04:28 +0000

1# Copyright 2016-2024 Maël Azimi <m.a@moul.re> 

2# 

3# Silkaj is free software: you can redistribute it and/or modify 

4# it under the terms of the GNU Affero General Public License as published by 

5# the Free Software Foundation, either version 3 of the License, or 

6# (at your option) any later version. 

7# 

8# Silkaj is distributed in the hope that it will be useful, 

9# but WITHOUT ANY WARRANTY; without even the implied warranty of 

10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

11# GNU Affero General Public License for more details. 

12# 

13# You should have received a copy of the GNU Affero General Public License 

14# along with Silkaj. If not, see <https://www.gnu.org/licenses/>. 

15 

16import functools 

17import sys 

18from typing import Any 

19 

20import rich_click as click 

21 

22from silkaj.blockchain.tools import get_blockchain_parameters 

23from silkaj.constants import FAILURE_EXIT_STATUS, G1_SYMBOL, GTEST_SYMBOL 

24 

25 

26@functools.lru_cache(maxsize=1) 

27def get_currency_symbol() -> str: 

28 params = get_blockchain_parameters() 

29 if params["currency"] == "g1": 

30 return G1_SYMBOL 

31 return GTEST_SYMBOL 

32 

33 

34def message_exit(message: str) -> None: 

35 print(message) 

36 sys.exit(FAILURE_EXIT_STATUS) 

37 

38 

39class MutuallyExclusiveOption(click.Option): 

40 def __init__(self, *args: Any, **kwargs: Any) -> None: 

41 self.mutually_exclusive = set(kwargs.pop("mutually_exclusive", [])) 

42 _help = kwargs.get("help", "") 

43 if self.mutually_exclusive: 

44 ex_str = ", ".join(self.mutually_exclusive) 

45 kwargs["help"] = ( 

46 f"{_help} NOTE: This argument is mutually exclusive with arguments: [{ex_str}]." 

47 ) 

48 super().__init__(*args, **kwargs) 

49 

50 def handle_parse_result(self, ctx: click.Context, opts: Any, args: Any) -> Any: 

51 if self.mutually_exclusive.intersection(opts) and self.name in opts: 

52 arguments = ", ".join(self.mutually_exclusive) 

53 raise click.UsageError( 

54 message=f"Usage: `{self.name}` is mutually exclusive with arguments `{arguments}`.", 

55 ) 

56 

57 return super().handle_parse_result(ctx, opts, args)