diff --git a/chia/_tests/cmds/test_click_types.py b/chia/_tests/cmds/test_click_types.py index 37af33bf6d07..f1bc2ddc1151 100644 --- a/chia/_tests/cmds/test_click_types.py +++ b/chia/_tests/cmds/test_click_types.py @@ -1,12 +1,11 @@ from __future__ import annotations -from dataclasses import dataclass, field from decimal import Decimal from pathlib import Path -from typing import Any, cast +import click import pytest -from click import BadParameter, Context +from click import BadParameter from chia.cmds.cmd_classes import ChiaCliContext from chia.cmds.param_types import ( @@ -39,9 +38,9 @@ overflow_decimal = Decimal(overflow_decimal_str) -@dataclass -class FakeContext: - obj: dict[Any, Any] = field(default_factory=dict) +@click.command() +def a_command_for_testing() -> None: + pass # pragma: no cover def test_click_tx_fee_type() -> None: @@ -115,11 +114,11 @@ def test_click_amount_type() -> None: def test_click_address_type() -> None: + context = click.Context(command=a_command_for_testing) + chia_context = ChiaCliContext.set_default(context) # this makes us not have to use a config file - context = cast( - Context, - FakeContext(obj=ChiaCliContext(expected_prefix="xch").to_click()), - ) + chia_context.expected_prefix = "xch" + std_cli_address = CliAddress(burn_ph, burn_address, AddressType.XCH) nft_cli_address = CliAddress(burn_ph, burn_nft_addr, AddressType.DID) # Test CliAddress (Generally is not used) @@ -137,7 +136,7 @@ def test_click_address_type() -> None: # check error handling with pytest.raises(BadParameter): AddressParamType().convert("test", None, None) - with pytest.raises(AttributeError): # attribute error because the context does not have a real error handler + with pytest.raises(click.BadParameter): AddressParamType().convert(burn_address_txch, None, context) with pytest.raises(BadParameter): AddressParamType().convert(burn_bad_prefix, None, None) @@ -153,11 +152,13 @@ def test_click_address_type() -> None: def test_click_address_type_config(root_path_populated_with_config: Path) -> None: + context = click.Context(command=a_command_for_testing) + chia_context = ChiaCliContext.set_default(context) # set a root path in context. - context = cast(Context, FakeContext(obj=ChiaCliContext(root_path=root_path_populated_with_config).to_click())) + chia_context.root_path = root_path_populated_with_config # run test that should pass assert AddressParamType().convert(burn_address, None, context) == CliAddress(burn_ph, burn_address, AddressType.XCH) - assert ChiaCliContext.from_click(context).expected_prefix == "xch" # validate that the prefix was set correctly + assert ChiaCliContext.set_default(context).expected_prefix == "xch" # validate that the prefix was set correctly # use txch address with pytest.raises(AttributeError): # attribute error because the context does not have a real error handler AddressParamType().convert(burn_address_txch, None, context) diff --git a/chia/cmds/beta.py b/chia/cmds/beta.py index 8ee257425e14..19c689416b5c 100644 --- a/chia/cmds/beta.py +++ b/chia/cmds/beta.py @@ -38,7 +38,7 @@ def beta_cmd() -> None: @click.option("-i", "--interval", help="System metrics will be logged based on this interval", type=int, required=False) @click.pass_context def configure(ctx: click.Context, path: Optional[str], interval: Optional[int]) -> None: - root_path = ChiaCliContext.from_click(ctx).root_path + root_path = ChiaCliContext.set_default(ctx).root_path with lock_and_load_config(root_path, "config.yaml") as config: if "beta" not in config: raise click.ClickException("beta test mode is not enabled, enable it first with `chia beta enable`") @@ -80,7 +80,7 @@ def configure(ctx: click.Context, path: Optional[str], interval: Optional[int]) @click.option("-p", "--path", help="The beta mode root path", type=str, required=False) @click.pass_context def enable_cmd(ctx: click.Context, force: bool, path: Optional[str]) -> None: - root_path = ChiaCliContext.from_click(ctx).root_path + root_path = ChiaCliContext.set_default(ctx).root_path with lock_and_load_config(root_path, "config.yaml") as config: if config.get("beta", {}).get("enabled", False): raise click.ClickException("beta test mode is already enabled") @@ -108,7 +108,7 @@ def enable_cmd(ctx: click.Context, force: bool, path: Optional[str]) -> None: @beta_cmd.command("disable", help="Disable beta test mode") @click.pass_context def disable_cmd(ctx: click.Context) -> None: - root_path = ChiaCliContext.from_click(ctx).root_path + root_path = ChiaCliContext.set_default(ctx).root_path with lock_and_load_config(root_path, "config.yaml") as config: if not config.get("beta", {}).get("enabled", False): raise click.ClickException("beta test mode is not enabled") @@ -122,7 +122,7 @@ def disable_cmd(ctx: click.Context) -> None: @beta_cmd.command("prepare_submission", help="Prepare the collected log data for submission") @click.pass_context def prepare_submission_cmd(ctx: click.Context) -> None: - with lock_and_load_config(ChiaCliContext.from_click(ctx).root_path, "config.yaml") as config: + with lock_and_load_config(ChiaCliContext.set_default(ctx).root_path, "config.yaml") as config: beta_root_path = config.get("beta", {}).get("path", None) if beta_root_path is None: raise click.ClickException("beta test mode not enabled. Run `chia beta enable` first.") @@ -174,7 +174,7 @@ def add_files(paths: list[Path]) -> int: @beta_cmd.command("status", help="Show the current beta configuration") @click.pass_context def status(ctx: click.Context) -> None: - with lock_and_load_config(ChiaCliContext.from_click(ctx).root_path, "config.yaml") as config: + with lock_and_load_config(ChiaCliContext.set_default(ctx).root_path, "config.yaml") as config: beta_config = config.get("beta") if beta_config is None: raise click.ClickException("beta test mode is not enabled, enable it first with `chia beta enable`") diff --git a/chia/cmds/chia.py b/chia/cmds/chia.py index 803d6ea0145a..d697be1d8052 100644 --- a/chia/cmds/chia.py +++ b/chia/cmds/chia.py @@ -64,8 +64,8 @@ def cli( ) -> None: from pathlib import Path - ctx.ensure_object(dict) - ctx.obj.update(ChiaCliContext(root_path=Path(root_path)).to_click()) + context = ChiaCliContext.set_default(ctx=ctx) + context.root_path = Path(root_path) # keys_root_path and passphrase_file will be None if the passphrase options have been # scrubbed from the CLI options @@ -117,7 +117,7 @@ def run_daemon_cmd(ctx: click.Context, wait_for_unlock: bool) -> None: wait_for_unlock = wait_for_unlock and Keychain.is_keyring_locked() - asyncio.run(async_run_daemon(ChiaCliContext.from_click(ctx).root_path, wait_for_unlock=wait_for_unlock)) + asyncio.run(async_run_daemon(ChiaCliContext.set_default(ctx).root_path, wait_for_unlock=wait_for_unlock)) cli.add_command(keys_cmd) diff --git a/chia/cmds/cmd_classes.py b/chia/cmds/cmd_classes.py index 7fd6150db1e5..0a864453c80e 100644 --- a/chia/cmds/cmd_classes.py +++ b/chia/cmds/cmd_classes.py @@ -14,7 +14,6 @@ Optional, Protocol, Union, - cast, final, get_args, get_origin, @@ -76,16 +75,11 @@ class ChiaCliContext: expected_currency_prefix: Optional[str] = None @classmethod - def from_click(cls, ctx: click.Context) -> ChiaCliContext: - if ctx.obj is None: - # TODO: should we set it up on the ctx here? - return cls() - - existing = cast(Optional[ChiaCliContext], ctx.obj.get(cls.context_dict_key)) - if existing is None: - return cls() - - return existing + def set_default(cls, ctx: click.Context) -> ChiaCliContext: + ctx.ensure_object(dict) + self = ctx.obj.setdefault(cls.context_dict_key, cls()) + assert isinstance(self, cls) + return self def to_click(self) -> dict[str, object]: return {self.context_dict_key: self} @@ -157,7 +151,7 @@ def apply_decorators(self, cmd: SyncCmd) -> SyncCmd: def strip_click_context(func: SyncCmd) -> SyncCmd: def _inner(ctx: click.Context, **kwargs: Any) -> None: - context = ChiaCliContext.from_click(ctx) + context = ChiaCliContext.set_default(ctx) func(context=context, **kwargs) return _inner diff --git a/chia/cmds/configure.py b/chia/cmds/configure.py index 83724db1b779..e91e21db0da5 100644 --- a/chia/cmds/configure.py +++ b/chia/cmds/configure.py @@ -314,7 +314,7 @@ def configure_cmd( seeder_nameserver: str, ) -> None: configure( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, set_farmer_peer, set_node_introducer, set_fullnode_port, diff --git a/chia/cmds/dao.py b/chia/cmds/dao.py index 44309876f9fd..1751488e3a6c 100644 --- a/chia/cmds/dao.py +++ b/chia/cmds/dao.py @@ -65,7 +65,7 @@ def dao_add_cmd( asyncio.run( add_dao_wallet( - ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, name, treasury_id, filter_amount + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, name, treasury_id, filter_amount ) ) @@ -189,7 +189,7 @@ def dao_create_cmd( return asyncio.run( create_dao_wallet( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, fee, @@ -240,7 +240,7 @@ def dao_get_id_cmd( ) -> None: from chia.cmds.dao_funcs import get_treasury_id - asyncio.run(get_treasury_id(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id)) + asyncio.run(get_treasury_id(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id)) @dao_cmd.command("add_funds", short_help="Send funds to a DAO treasury", no_args_is_help=True) @@ -291,7 +291,7 @@ def dao_add_funds_cmd( return asyncio.run( add_funds_to_treasury( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id, @@ -330,7 +330,9 @@ def dao_get_balance_cmd( ) -> None: from chia.cmds.dao_funcs import get_treasury_balance - asyncio.run(get_treasury_balance(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id)) + asyncio.run( + get_treasury_balance(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id) + ) @dao_cmd.command("rules", short_help="Get the current rules governing the DAO", no_args_is_help=True) @@ -352,7 +354,7 @@ def dao_rules_cmd( ) -> None: from chia.cmds.dao_funcs import get_rules - asyncio.run(get_rules(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id)) + asyncio.run(get_rules(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id)) # ---------------------------------------------------------------------------------------- @@ -390,7 +392,7 @@ def dao_list_proposals_cmd( asyncio.run( list_proposals( - ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id, include_closed + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id, include_closed ) ) @@ -423,7 +425,7 @@ def dao_show_proposal_cmd( from chia.cmds.dao_funcs import show_proposal asyncio.run( - show_proposal(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id, proposal_id) + show_proposal(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id, proposal_id) ) @@ -488,7 +490,7 @@ def dao_vote_cmd( return asyncio.run( vote_on_proposal( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id, @@ -561,7 +563,7 @@ def dao_close_proposal_cmd( return asyncio.run( close_proposal( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id, @@ -625,7 +627,7 @@ def dao_lockup_coins_cmd( return asyncio.run( lockup_coins( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id, @@ -676,7 +678,7 @@ def dao_release_coins_cmd( return asyncio.run( release_coins( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id, @@ -726,7 +728,7 @@ def dao_exit_lockup_cmd( return asyncio.run( exit_lockup( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id, @@ -830,7 +832,7 @@ def dao_create_spend_proposal_cmd( return asyncio.run( create_spend_proposal( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id, @@ -942,7 +944,7 @@ def dao_create_update_proposal_cmd( return asyncio.run( create_update_proposal( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id, @@ -1025,7 +1027,7 @@ def dao_create_mint_proposal_cmd( return asyncio.run( create_mint_proposal( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id, diff --git a/chia/cmds/db.py b/chia/cmds/db.py index cd38d53c9282..08734b01d64f 100644 --- a/chia/cmds/db.py +++ b/chia/cmds/db.py @@ -42,7 +42,7 @@ def db_upgrade_cmd( ) -> None: try: db_upgrade_func( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, None if in_db_path is None else Path(in_db_path), None if out_db_path is None else Path(out_db_path), no_update_config=no_update_config, @@ -64,7 +64,7 @@ def db_upgrade_cmd( def db_validate_cmd(ctx: click.Context, in_db_path: Optional[str], validate_blocks: bool) -> None: try: db_validate_func( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, None if in_db_path is None else Path(in_db_path), validate_blocks=validate_blocks, ) @@ -79,7 +79,7 @@ def db_validate_cmd(ctx: click.Context, in_db_path: Optional[str], validate_bloc def db_backup_cmd(ctx: click.Context, db_backup_file: Optional[str], no_indexes: bool) -> None: try: db_backup_func( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, None if db_backup_file is None else Path(db_backup_file), no_indexes=no_indexes, ) diff --git a/chia/cmds/farm.py b/chia/cmds/farm.py index 6d4a40aa68d5..931c6108a5b7 100644 --- a/chia/cmds/farm.py +++ b/chia/cmds/farm.py @@ -69,7 +69,7 @@ def summary_cmd( wallet_rpc_port, harvester_rpc_port, farmer_rpc_port, - root_path=ChiaCliContext.from_click(ctx).root_path, + root_path=ChiaCliContext.set_default(ctx).root_path, ) ) @@ -97,4 +97,4 @@ def challenges_cmd(ctx: click.Context, farmer_rpc_port: Optional[int], limit: in from chia.cmds.farm_funcs import challenges - asyncio.run(challenges(ChiaCliContext.from_click(ctx).root_path, farmer_rpc_port, limit)) + asyncio.run(challenges(ChiaCliContext.set_default(ctx).root_path, farmer_rpc_port, limit)) diff --git a/chia/cmds/init.py b/chia/cmds/init.py index 56c894fdb83c..41a61b99a81c 100644 --- a/chia/cmds/init.py +++ b/chia/cmds/init.py @@ -56,7 +56,7 @@ def init_cmd( init( Path(create_certs) if create_certs is not None else None, - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, fix_ssl_permissions, testnet, v1_db, diff --git a/chia/cmds/keys.py b/chia/cmds/keys.py index 1856e92cb334..6efe1218af7b 100644 --- a/chia/cmds/keys.py +++ b/chia/cmds/keys.py @@ -13,7 +13,7 @@ @click.pass_context def keys_cmd(ctx: click.Context) -> None: """Create, delete, view and use your key pairs""" - root_path = ChiaCliContext.from_click(ctx).root_path + root_path = ChiaCliContext.set_default(ctx).root_path if not root_path.is_dir(): raise RuntimeError("Please initialize (or migrate) your config directory with chia init") @@ -33,7 +33,7 @@ def generate_cmd(ctx: click.Context, label: Optional[str]) -> None: from chia.cmds.keys_funcs import generate_and_add generate_and_add(label) - check_keys(ChiaCliContext.from_click(ctx).root_path) + check_keys(ChiaCliContext.set_default(ctx).root_path) @keys_cmd.command("show", help="Displays all the keys in keychain or the key with the given fingerprint") @@ -77,7 +77,7 @@ def show_cmd( from chia.cmds.keys_funcs import show_keys show_keys( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, show_mnemonic_seed, non_observer_derivation, json, @@ -115,7 +115,7 @@ def add_cmd(ctx: click.Context, filename: str, label: Optional[str]) -> None: mnemonic_or_pk = Path(filename).read_text().rstrip() query_and_add_key_info(mnemonic_or_pk, label) - check_keys(ChiaCliContext.from_click(ctx).root_path) + check_keys(ChiaCliContext.set_default(ctx).root_path) @keys_cmd.group("label", help="Manage your key labels") @@ -161,7 +161,7 @@ def delete_cmd(ctx: click.Context, fingerprint: int) -> None: from chia.cmds.keys_funcs import delete delete(fingerprint) - check_keys(ChiaCliContext.from_click(ctx).root_path) + check_keys(ChiaCliContext.set_default(ctx).root_path) @keys_cmd.command("delete_all", help="Delete all private keys in keychain") @@ -281,7 +281,7 @@ def verify_cmd(message: str, public_key: str, signature: str, as_bytes: bool, js ) @click.pass_context def derive_cmd(ctx: click.Context, fingerprint: Optional[int], filename: Optional[str]) -> None: - context = ChiaCliContext.from_click(ctx) + context = ChiaCliContext.set_default(ctx) context.keys_fingerprint = fingerprint context.keys_filename = filename @@ -339,7 +339,7 @@ def search_cmd( from chia.cmds.keys_funcs import resolve_derivation_master_key, search_derive - context = ChiaCliContext.from_click(ctx) + context = ChiaCliContext.set_default(ctx) fingerprint: Optional[int] = context.keys_fingerprint filename: Optional[str] = context.keys_filename @@ -418,7 +418,7 @@ def wallet_address_cmd( ) -> None: from chia.cmds.keys_funcs import derive_wallet_address - context = ChiaCliContext.from_click(ctx) + context = ChiaCliContext.set_default(ctx) fingerprint = context.keys_fingerprint filename = context.keys_filename @@ -428,7 +428,7 @@ def wallet_address_cmd( return derive_wallet_address( - ChiaCliContext.from_click(ctx).root_path, + context.root_path, fingerprint, index, count, @@ -504,7 +504,7 @@ def child_key_cmd( if key_type is None and derive_from_hd_path is None: ctx.fail("--type or --derive-from-hd-path is required") - context = ChiaCliContext.from_click(ctx) + context = ChiaCliContext.set_default(ctx) fingerprint = context.keys_fingerprint filename = context.keys_filename diff --git a/chia/cmds/netspace.py b/chia/cmds/netspace.py index 010701bad13c..c7c03ec0fab1 100644 --- a/chia/cmds/netspace.py +++ b/chia/cmds/netspace.py @@ -47,4 +47,4 @@ def netspace_cmd(ctx: click.Context, rpc_port: Optional[int], delta_block_height from chia.cmds.netspace_funcs import netstorge_async - asyncio.run(netstorge_async(ChiaCliContext.from_click(ctx).root_path, rpc_port, delta_block_height, start)) + asyncio.run(netstorge_async(ChiaCliContext.set_default(ctx).root_path, rpc_port, delta_block_height, start)) diff --git a/chia/cmds/param_types.py b/chia/cmds/param_types.py index 021aefead1b7..ac8818f713f8 100644 --- a/chia/cmds/param_types.py +++ b/chia/cmds/param_types.py @@ -180,7 +180,7 @@ def convert(self, value: Any, param: Optional[click.Parameter], ctx: Optional[cl root_path = DEFAULT_ROOT_PATH if ctx is not None: - context = ChiaCliContext.from_click(ctx) + context = ChiaCliContext.set_default(ctx) root_path = context.root_path expected_prefix = context.expected_prefix diff --git a/chia/cmds/passphrase.py b/chia/cmds/passphrase.py index cdd3fefcfb25..bad08ffbfdc5 100644 --- a/chia/cmds/passphrase.py +++ b/chia/cmds/passphrase.py @@ -71,7 +71,7 @@ def set_cmd( if success: # Attempt to update the daemon's passphrase cache - root_path = ChiaCliContext.from_click(ctx).root_path + root_path = ChiaCliContext.set_default(ctx).root_path config = load_config(root_path, "config.yaml") sys.exit(asyncio.run(async_update_daemon_passphrase_cache_if_running(root_path, config))) @@ -99,7 +99,7 @@ def remove_cmd(ctx: click.Context, current_passphrase_file: Optional[TextIOWrapp if remove_passphrase(current_passphrase): # Attempt to update the daemon's passphrase cache - root_path = ChiaCliContext.from_click(ctx).root_path + root_path = ChiaCliContext.set_default(ctx).root_path config = load_config(root_path, "config.yaml") sys.exit(asyncio.run(async_update_daemon_passphrase_cache_if_running(root_path, config))) diff --git a/chia/cmds/peer.py b/chia/cmds/peer.py index d06e17929f81..ae31d2a29430 100644 --- a/chia/cmds/peer.py +++ b/chia/cmds/peer.py @@ -43,7 +43,7 @@ def peer_cmd( peer_async( node_type, rpc_port, - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, connections, add_connection, remove_connection, diff --git a/chia/cmds/plots.py b/chia/cmds/plots.py index ba0c055ef4a3..3a75b3f0f3bf 100644 --- a/chia/cmds/plots.py +++ b/chia/cmds/plots.py @@ -34,7 +34,7 @@ def plots_cmd(ctx: click.Context) -> None: """Create, add, remove and check your plots""" from chia.util.chia_logging import initialize_logging - root_path = ChiaCliContext.from_click(ctx).root_path + root_path = ChiaCliContext.set_default(ctx).root_path if not root_path.is_dir(): raise RuntimeError("Please initialize (or migrate) your config directory with 'chia init'") initialize_logging("", {"log_level": "INFO", "log_stdout": True}, root_path) @@ -133,7 +133,7 @@ def create_cmd( nobitfield=nobitfield, ) - root_path = ChiaCliContext.from_click(ctx).root_path + root_path = ChiaCliContext.set_default(ctx).root_path try: validate_plot_size(root_path, size, override_k) except ValueError as e: @@ -179,7 +179,7 @@ def check_cmd( from chia.plotting.check_plots import check_plots check_plots( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, num, challenge_start, grep_string, @@ -202,7 +202,7 @@ def add_cmd(ctx: click.Context, final_dir: str) -> None: from chia.plotting.util import add_plot_directory try: - add_plot_directory(ChiaCliContext.from_click(ctx).root_path, final_dir) + add_plot_directory(ChiaCliContext.set_default(ctx).root_path, final_dir) print(f"Successfully added: {final_dir}") except ValueError as e: print(e) @@ -221,10 +221,10 @@ def add_cmd(ctx: click.Context, final_dir: str) -> None: def remove_cmd(ctx: click.Context, final_dir: str) -> None: from chia.plotting.util import remove_plot_directory - remove_plot_directory(ChiaCliContext.from_click(ctx).root_path, final_dir) + remove_plot_directory(ChiaCliContext.set_default(ctx).root_path, final_dir) @plots_cmd.command("show", help="Shows the directory of current plots") @click.pass_context def show_cmd(ctx: click.Context) -> None: - show_plots(ChiaCliContext.from_click(ctx).root_path) + show_plots(ChiaCliContext.set_default(ctx).root_path) diff --git a/chia/cmds/plotters.py b/chia/cmds/plotters.py index 7b22e01c99ea..0761090a10ce 100644 --- a/chia/cmds/plotters.py +++ b/chia/cmds/plotters.py @@ -15,4 +15,4 @@ @click.pass_context @click.argument("args", nargs=-1) def plotters_cmd(ctx: click.Context, args: tuple[click.Argument]) -> None: - call_plotters(ChiaCliContext.from_click(ctx).root_path, args) + call_plotters(ChiaCliContext.set_default(ctx).root_path, args) diff --git a/chia/cmds/rpc.py b/chia/cmds/rpc.py index 92cfd43ca41c..3bdaef97ea0f 100644 --- a/chia/cmds/rpc.py +++ b/chia/cmds/rpc.py @@ -104,7 +104,7 @@ def rpc_cmd() -> None: @click.argument("service", type=click.Choice(services)) @click.pass_context def endpoints_cmd(ctx: click.Context, service: str) -> None: - root_path = ChiaCliContext.from_click(ctx).root_path + root_path = ChiaCliContext.set_default(ctx).root_path config = load_config(root_path, "config.yaml") try: routes = get_routes(service, config, root_path=root_path) @@ -120,7 +120,7 @@ def endpoints_cmd(ctx: click.Context, service: str) -> None: def status_cmd(ctx: click.Context, json_output: bool) -> None: import json - root_path = ChiaCliContext.from_click(ctx).root_path + root_path = ChiaCliContext.set_default(ctx).root_path config = load_config(root_path, "config.yaml") def print_row(c0: str, c1: str) -> None: @@ -178,7 +178,7 @@ def rpc_client_cmd( json_file: Optional[TextIO], service: str = service, ) -> None: - root_path: Path = ChiaCliContext.from_click(ctx).root_path + root_path: Path = ChiaCliContext.set_default(ctx).root_path config = load_config(root_path, "config.yaml") if request is not None and json_file is not None: sys.exit( diff --git a/chia/cmds/show.py b/chia/cmds/show.py index 705c95189747..1199b042b8f9 100644 --- a/chia/cmds/show.py +++ b/chia/cmds/show.py @@ -63,7 +63,7 @@ def show_cmd( asyncio.run( show_async( rpc_port, - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, fee, state, block_header_hash_by_height, diff --git a/chia/cmds/sim.py b/chia/cmds/sim.py index fd7390a6d544..1da926f74b32 100644 --- a/chia/cmds/sim.py +++ b/chia/cmds/sim.py @@ -35,14 +35,9 @@ ) @click.pass_context def sim_cmd(ctx: click.Context, rpc_port: Optional[int], root_path: str, simulator_name: str) -> None: - ctx.ensure_object(dict) - # TODO: maybe update instead of replacing? - ctx.obj.update( - ChiaCliContext( - root_path=Path(root_path) / simulator_name, - rpc_port=rpc_port, - ).to_click() - ) + context = ChiaCliContext.set_default(ctx) + context.root_path = Path(root_path) / simulator_name + context.rpc_port = rpc_port @sim_cmd.command("create", help="Guides you through the process of setting up a Chia Simulator") @@ -78,7 +73,7 @@ def create_simulator_config( docker_mode: bool, no_bitfield: bool, ) -> None: - root_path = ChiaCliContext.from_click(ctx).root_path + root_path = ChiaCliContext.set_default(ctx).root_path print(f"Using this Directory: {root_path}\n") if fingerprint and mnemonic: print("You can't use both a fingerprint and a mnemonic. Please choose one.") @@ -138,7 +133,7 @@ def status_cmd( include_rewards: bool, show_addresses: bool, ) -> None: - context = ChiaCliContext.from_click(ctx) + context = ChiaCliContext.set_default(ctx) asyncio.run( print_status( @@ -183,7 +178,7 @@ def revert_cmd( print("\nBlocks, '-b' must not be set if all blocks are selected by reset, '-r'. Exiting.\n") return - context = ChiaCliContext.from_click(ctx) + context = ChiaCliContext.set_default(ctx) asyncio.run( revert_block_height( @@ -203,7 +198,7 @@ def revert_cmd( @click.option("-a", "--target-address", type=str, default="", help="Block reward address") @click.pass_context def farm_cmd(ctx: click.Context, blocks: int, non_transaction: bool, target_address: str) -> None: - context = ChiaCliContext.from_click(ctx) + context = ChiaCliContext.set_default(ctx) asyncio.run( farm_blocks( context.rpc_port, @@ -220,7 +215,7 @@ def farm_cmd(ctx: click.Context, blocks: int, non_transaction: bool, target_addr @click.pass_context def autofarm_cmd(ctx: click.Context, set_autofarm: str) -> None: autofarm = bool(set_autofarm == "on") - context = ChiaCliContext.from_click(ctx) + context = ChiaCliContext.set_default(ctx) asyncio.run( set_auto_farm( context.rpc_port, diff --git a/chia/cmds/start.py b/chia/cmds/start.py index 90091dff291e..907ad6241626 100644 --- a/chia/cmds/start.py +++ b/chia/cmds/start.py @@ -18,7 +18,7 @@ def start_cmd(ctx: click.Context, restart: bool, skip_keyring: bool, group: tupl from chia.cmds.beta_funcs import warn_if_beta_enabled from chia.cmds.start_funcs import async_start - root_path = ChiaCliContext.from_click(ctx).root_path + root_path = ChiaCliContext.set_default(ctx).root_path config = load_config(root_path, "config.yaml") warn_if_beta_enabled(config) asyncio.run(async_start(root_path, config, group, restart, skip_keyring=skip_keyring)) diff --git a/chia/cmds/stop.py b/chia/cmds/stop.py index ec745bbf0225..cff1191100a3 100644 --- a/chia/cmds/stop.py +++ b/chia/cmds/stop.py @@ -55,7 +55,7 @@ async def async_stop(root_path: Path, config: dict[str, Any], group: tuple[str, def stop_cmd(ctx: click.Context, daemon: bool, group: tuple[str, ...]) -> None: from chia.cmds.beta_funcs import warn_if_beta_enabled - root_path = ChiaCliContext.from_click(ctx).root_path + root_path = ChiaCliContext.set_default(ctx).root_path config = load_config(root_path, "config.yaml") warn_if_beta_enabled(config) diff --git a/chia/cmds/wallet.py b/chia/cmds/wallet.py index 6912d9a25528..48784d6acb47 100644 --- a/chia/cmds/wallet.py +++ b/chia/cmds/wallet.py @@ -61,7 +61,7 @@ def get_transaction_cmd( asyncio.run( get_transaction( - root_path=ChiaCliContext.from_click(ctx).root_path, + root_path=ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port=wallet_rpc_port, fingerprint=fingerprint, tx_id=tx_id, @@ -149,7 +149,7 @@ def get_transactions_cmd( asyncio.run( get_transactions( - root_path=ChiaCliContext.from_click(ctx).root_path, + root_path=ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port=wallet_rpc_port, fp=fingerprint, wallet_id=id, @@ -244,7 +244,7 @@ def send_cmd( return asyncio.run( send( - root_path=ChiaCliContext.from_click(ctx).root_path, + root_path=ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port=wallet_rpc_port, fp=fingerprint, wallet_id=id, @@ -286,7 +286,7 @@ def show_cmd(ctx: click.Context, wallet_rpc_port: Optional[int], fingerprint: in asyncio.run( print_balances( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, WalletType[wallet_type.upper()] if wallet_type else None, @@ -320,7 +320,7 @@ def get_address_cmd( ) -> None: from chia.cmds.wallet_funcs import get_address - asyncio.run(get_address(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, id, new_address)) + asyncio.run(get_address(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, id, new_address)) @wallet_cmd.command( @@ -370,7 +370,7 @@ def clawback( return asyncio.run( spend_clawback( - root_path=ChiaCliContext.from_click(ctx).root_path, + root_path=ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port=wallet_rpc_port, fp=fingerprint, fee=fee, @@ -399,7 +399,7 @@ def delete_unconfirmed_transactions_cmd( from chia.cmds.wallet_funcs import delete_unconfirmed_transactions asyncio.run( - delete_unconfirmed_transactions(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, id) + delete_unconfirmed_transactions(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, id) ) @@ -416,7 +416,7 @@ def delete_unconfirmed_transactions_cmd( def get_derivation_index_cmd(ctx: click.Context, wallet_rpc_port: Optional[int], fingerprint: int) -> None: from chia.cmds.wallet_funcs import get_derivation_index - asyncio.run(get_derivation_index(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint)) + asyncio.run(get_derivation_index(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint)) @wallet_cmd.command("sign_message", help="Sign a message by a derivation address") @@ -439,7 +439,7 @@ def address_sign_message( asyncio.run( sign_message( - root_path=ChiaCliContext.from_click(ctx).root_path, + root_path=ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port=wallet_rpc_port, fp=fingerprint, addr_type=AddressType.XCH, @@ -469,7 +469,7 @@ def update_derivation_index_cmd( ) -> None: from chia.cmds.wallet_funcs import update_derivation_index - asyncio.run(update_derivation_index(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, index)) + asyncio.run(update_derivation_index(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, index)) @wallet_cmd.command("add_token", help="Add/Rename a CAT to the wallet by its asset ID") @@ -499,7 +499,9 @@ def add_token_cmd( ) -> None: from chia.cmds.wallet_funcs import add_token - asyncio.run(add_token(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, asset_id, token_name)) + asyncio.run( + add_token(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, asset_id, token_name) + ) @wallet_cmd.command("make_offer", help="Create an offer of XCH/CATs/NFTs for XCH/CATs/NFTs") @@ -563,7 +565,7 @@ def make_offer_cmd( asyncio.run( make_offer( - root_path=ChiaCliContext.from_click(ctx).root_path, + root_path=ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port=wallet_rpc_port, fp=fingerprint, fee=fee, @@ -615,7 +617,7 @@ def get_offers_cmd( asyncio.run( get_offers( - root_path=ChiaCliContext.from_click(ctx).root_path, + root_path=ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port=wallet_rpc_port, fp=fingerprint, offer_id=id, @@ -666,7 +668,7 @@ def take_offer_cmd( return asyncio.run( take_offer( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, fee, @@ -706,7 +708,7 @@ def cancel_offer_cmd( return asyncio.run( cancel_offer( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, fee, @@ -729,7 +731,7 @@ def check_wallet_cmd(ctx: click.Context, db_path: str, verbose: bool) -> None: from chia.cmds.check_wallet_db import scan - asyncio.run(scan(ChiaCliContext.from_click(ctx).root_path, db_path, verbose=verbose)) + asyncio.run(scan(ChiaCliContext.set_default(ctx).root_path, db_path, verbose=verbose)) @wallet_cmd.group("did", help="DID related actions") @@ -772,7 +774,7 @@ def did_create_wallet_cmd( return asyncio.run( create_did_wallet( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, fee, @@ -803,7 +805,7 @@ def did_sign_message( asyncio.run( sign_message( - root_path=ChiaCliContext.from_click(ctx).root_path, + root_path=ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port=wallet_rpc_port, fp=fingerprint, addr_type=AddressType.DID, @@ -830,7 +832,7 @@ def did_wallet_name_cmd( ) -> None: from chia.cmds.wallet_funcs import did_set_wallet_name - asyncio.run(did_set_wallet_name(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, id, name)) + asyncio.run(did_set_wallet_name(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, id, name)) @did_cmd.command("get_did", help="Get DID from wallet") @@ -847,7 +849,7 @@ def did_wallet_name_cmd( def did_get_did_cmd(ctx: click.Context, wallet_rpc_port: Optional[int], fingerprint: int, id: int) -> None: from chia.cmds.wallet_funcs import get_did - asyncio.run(get_did(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, id)) + asyncio.run(get_did(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, id)) @did_cmd.command("get_details", help="Get more details of any DID") @@ -867,7 +869,7 @@ def did_get_details_cmd( ) -> None: from chia.cmds.wallet_funcs import get_did_info - asyncio.run(get_did_info(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, coin_id, latest)) + asyncio.run(get_did_info(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, coin_id, latest)) @did_cmd.command("update_metadata", help="Update the metadata of a DID") @@ -903,7 +905,7 @@ def did_update_metadata_cmd( return asyncio.run( update_did_metadata( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, id, @@ -955,7 +957,7 @@ def did_find_lost_cmd( asyncio.run( find_lost_did( - root_path=ChiaCliContext.from_click(ctx).root_path, + root_path=ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port=wallet_rpc_port, fp=fingerprint, coin_id=coin_id, @@ -1027,7 +1029,7 @@ def did_message_spend_cmd( return asyncio.run( did_message_spend( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, id, @@ -1079,7 +1081,7 @@ def did_transfer_did( return asyncio.run( transfer_did( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, id, @@ -1120,7 +1122,9 @@ def nft_wallet_create_cmd( ) -> None: from chia.cmds.wallet_funcs import create_nft_wallet - asyncio.run(create_nft_wallet(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, did_id, name)) + asyncio.run( + create_nft_wallet(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, did_id, name) + ) @nft_cmd.command("sign_message", help="Sign a message by a NFT") @@ -1142,7 +1146,7 @@ def nft_sign_message( asyncio.run( sign_message( - root_path=ChiaCliContext.from_click(ctx).root_path, + root_path=ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port=wallet_rpc_port, fp=fingerprint, addr_type=AddressType.NFT, @@ -1226,7 +1230,7 @@ def nft_mint_cmd( return asyncio.run( mint_nft( - root_path=ChiaCliContext.from_click(ctx).root_path, + root_path=ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port=wallet_rpc_port, fp=fingerprint, wallet_id=id, @@ -1292,7 +1296,7 @@ def nft_add_uri_cmd( return asyncio.run( add_uri_to_nft( - root_path=ChiaCliContext.from_click(ctx).root_path, + root_path=ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port=wallet_rpc_port, fp=fingerprint, wallet_id=id, @@ -1346,7 +1350,7 @@ def nft_transfer_cmd( return asyncio.run( transfer_nft( - root_path=ChiaCliContext.from_click(ctx).root_path, + root_path=ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port=wallet_rpc_port, fp=fingerprint, wallet_id=id, @@ -1378,7 +1382,9 @@ def nft_list_cmd( ) -> None: from chia.cmds.wallet_funcs import list_nfts - asyncio.run(list_nfts(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, id, num, start_index)) + asyncio.run( + list_nfts(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, id, num, start_index) + ) @nft_cmd.command("set_did", help="Set a DID on an NFT") @@ -1419,7 +1425,7 @@ def nft_set_did_cmd( return asyncio.run( set_nft_did( - root_path=ChiaCliContext.from_click(ctx).root_path, + root_path=ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port=wallet_rpc_port, fp=fingerprint, wallet_id=id, @@ -1453,7 +1459,7 @@ def nft_get_info_cmd( ) -> None: from chia.cmds.wallet_funcs import get_nft_info - asyncio.run(get_nft_info(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, nft_coin_id)) + asyncio.run(get_nft_info(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, nft_coin_id)) # Keep at bottom. @@ -1506,7 +1512,7 @@ def send_notification_cmd( message_bytes: bytes = bytes(message, "utf8") return asyncio.run( send_notification( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, fee, @@ -1543,7 +1549,7 @@ def get_notifications_cmd( from chia.cmds.wallet_funcs import get_notifications asyncio.run( - get_notifications(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, id, start, end) + get_notifications(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, id, start, end) ) @@ -1568,7 +1574,7 @@ def delete_notifications_cmd( ) -> None: from chia.cmds.wallet_funcs import delete_notifications - asyncio.run(delete_notifications(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, id, all)) + asyncio.run(delete_notifications(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, id, all)) @wallet_cmd.group("vcs", short_help="Verifiable Credential related actions") @@ -1610,7 +1616,7 @@ def mint_vc_cmd( return asyncio.run( mint_vc( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, did, @@ -1647,7 +1653,7 @@ def get_vcs_cmd( ) -> None: # pragma: no cover from chia.cmds.wallet_funcs import get_vcs - asyncio.run(get_vcs(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, start, count)) + asyncio.run(get_vcs(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, start, count)) @vcs_cmd.command("update_proofs", short_help="Update a VC's proofs if you have the provider DID") @@ -1699,7 +1705,7 @@ def spend_vc_cmd( return asyncio.run( spend_vc( - root_path=ChiaCliContext.from_click(ctx).root_path, + root_path=ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port=wallet_rpc_port, fp=fingerprint, vc_id=vc_id, @@ -1735,7 +1741,7 @@ def add_proof_reveal_cmd( from chia.cmds.wallet_funcs import add_proof_reveal asyncio.run( - add_proof_reveal(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, proof, root_only) + add_proof_reveal(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, proof, root_only) ) @@ -1758,7 +1764,9 @@ def get_proofs_for_root_cmd( ) -> None: # pragma: no cover from chia.cmds.wallet_funcs import get_proofs_for_root - asyncio.run(get_proofs_for_root(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, proof_hash)) + asyncio.run( + get_proofs_for_root(ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, proof_hash) + ) @vcs_cmd.command("revoke", short_help="Revoke any VC if you have the proper DID and the VCs parent coin") @@ -1808,7 +1816,7 @@ def revoke_vc_cmd( return asyncio.run( revoke_vc( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, parent_coin_id, @@ -1878,7 +1886,7 @@ def approve_r_cats_cmd( return asyncio.run( approve_r_cats( - ChiaCliContext.from_click(ctx).root_path, + ChiaCliContext.set_default(ctx).root_path, wallet_rpc_port, fingerprint, uint32(id),