Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first pass at using @chia_command without a group #19040

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions chia/_tests/cmds/test_cmd_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def new_run(self: Any) -> None:
dict_compare_with_ignore_context(asdict(cmd), asdict(self)) # type: ignore[call-overload]

setattr(mock_type, "run", new_run)
chia_command(_cmd, "_", "", "")(mock_type)
chia_command(group=_cmd, name="_", short_help="", help="")(mock_type)

runner = CliRunner()
result = runner.invoke(_cmd, ["_", *args], catch_exceptions=False, obj=obj)
Expand All @@ -49,12 +49,12 @@ def test_cmd_bases() -> None:
def cmd() -> None:
pass

@chia_command(cmd, "temp_cmd", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd", short_help="blah", help="n/a")
class TempCMD:
def run(self) -> None:
print("syncronous")

@chia_command(cmd, "temp_cmd_async", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd_async", short_help="blah", help="n/a")
class TempCMDAsync:
async def run(self) -> None:
print("asyncronous")
Expand Down Expand Up @@ -96,15 +96,15 @@ def test_option_loading() -> None:
def cmd() -> None:
pass

@chia_command(cmd, "temp_cmd", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd", short_help="blah", help="n/a")
class TempCMD:
some_option: int = option("-o", "--some-option", required=True, type=int)
choices: list[str] = option("--choice", multiple=True, type=str)

def run(self) -> None:
print(self.some_option)

@chia_command(cmd, "temp_cmd_2", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd_2", short_help="blah", help="n/a")
class TempCMD2:
some_option: int = option("-o", "--some-option", required=True, type=int, default=13)

Expand Down Expand Up @@ -146,7 +146,7 @@ def test_context_requirement() -> None:
def cmd(ctx: click.Context) -> None:
ctx.obj = {"foo": "bar"}

@chia_command(cmd, "temp_cmd", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd", short_help="blah", help="n/a")
class TempCMD:
context: Context

Expand All @@ -164,7 +164,7 @@ def run(self) -> None:
# Test that other variables named context are disallowed
with pytest.raises(ValueError, match="context"):

@chia_command(cmd, "shouldnt_work", "blah", help="n/a")
@chia_command(group=cmd, name="shouldnt_work", short_help="blah", help="n/a")
class BadCMD:
context: int

Expand All @@ -176,7 +176,7 @@ def test_typing() -> None:
def cmd() -> None:
pass

@chia_command(cmd, "temp_cmd", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd", short_help="blah", help="n/a")
class TempCMD:
integer: int = option("--integer", default=1, required=False)
text: str = option("--text", default="1", required=False)
Expand Down Expand Up @@ -208,7 +208,7 @@ def run(self) -> None: ...
)

# Test optional
@chia_command(cmd, "temp_cmd_optional", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd_optional", short_help="blah", help="n/a")
class TempCMDOptional:
optional: Optional[int] = option("--optional", required=False)

Expand All @@ -220,28 +220,28 @@ def run(self) -> None: ...
# Test optional failure
with pytest.raises(TypeError):

@chia_command(cmd, "temp_cmd_optional_bad", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd_optional_bad", short_help="blah", help="n/a")
class TempCMDOptionalBad2:
optional: Optional[int] = option("--optional", required=True)

def run(self) -> None: ...

with pytest.raises(TypeError):

@chia_command(cmd, "temp_cmd_optional_bad", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd_optional_bad", short_help="blah", help="n/a")
class TempCMDOptionalBad3:
optional: Optional[int] = option("--optional", default="string", required=False)

def run(self) -> None: ...

@chia_command(cmd, "temp_cmd_optional_fine", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd_optional_fine", short_help="blah", help="n/a")
class TempCMDOptionalBad4:
optional: Optional[int] = option("--optional", default=None, required=False)

def run(self) -> None: ...

# Test multiple
@chia_command(cmd, "temp_cmd_sequence", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd_sequence", short_help="blah", help="n/a")
class TempCMDSequence:
sequence: Sequence[int] = option("--sequence", multiple=True)

Expand All @@ -253,31 +253,31 @@ def run(self) -> None: ...
# Test sequence failure
with pytest.raises(TypeError):

@chia_command(cmd, "temp_cmd_sequence_bad", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd_sequence_bad", short_help="blah", help="n/a")
class TempCMDSequenceBad:
sequence: Sequence[int] = option("--sequence")

def run(self) -> None: ...

with pytest.raises(TypeError):

@chia_command(cmd, "temp_cmd_sequence_bad", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd_sequence_bad", short_help="blah", help="n/a")
class TempCMDSequenceBad2:
sequence: int = option("--sequence", multiple=True)

def run(self) -> None: ...

with pytest.raises(ValueError):

@chia_command(cmd, "temp_cmd_sequence_bad", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd_sequence_bad", short_help="blah", help="n/a")
class TempCMDSequenceBad3:
sequence: Sequence[int] = option("--sequence", default=[1, 2, 3], multiple=True)

def run(self) -> None: ...

with pytest.raises(TypeError):

@chia_command(cmd, "temp_cmd_sequence_bad", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd_sequence_bad", short_help="blah", help="n/a")
class TempCMDSequenceBad4:
sequence: Sequence[int] = option("--sequence", default=(1, 2, "3"), multiple=True)

Expand All @@ -286,7 +286,7 @@ def run(self) -> None: ...
# Test invalid type
with pytest.raises(TypeError):

@chia_command(cmd, "temp_cmd_bad_type", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd_bad_type", short_help="blah", help="n/a")
class TempCMDBadType:
sequence: list[int] = option("--sequence")

Expand All @@ -295,20 +295,20 @@ def run(self) -> None: ...
# Test invalid default
with pytest.raises(TypeError):

@chia_command(cmd, "temp_cmd_bad_default", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd_bad_default", short_help="blah", help="n/a")
class TempCMDBadDefault:
integer: int = option("--int", default="string")

def run(self) -> None: ...

# Test bytes parsing
@chia_command(cmd, "temp_cmd_bad_bytes", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd_bad_bytes", short_help="blah", help="n/a")
class TempCMDBadBytes:
blob: bytes = option("--blob", required=True)

def run(self) -> None: ...

@chia_command(cmd, "temp_cmd_bad_bytes32", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd_bad_bytes32", short_help="blah", help="n/a")
class TempCMDBadBytes32:
blob32: bytes32 = option("--blob32", required=True)

Expand Down Expand Up @@ -354,7 +354,7 @@ async def test_wallet_rpc_helper(wallet_environments: WalletTestFramework) -> No
def cmd() -> None:
pass

@chia_command(cmd, "temp_cmd", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd", short_help="blah", help="n/a")
class TempCMD:
rpc_info: NeedsWalletRPC

Expand Down
10 changes: 5 additions & 5 deletions chia/_tests/wallet/test_signer_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ def test_transactions_in() -> None:
def cmd() -> None:
pass

@chia_command(cmd, "temp_cmd", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd", short_help="blah", help="n/a")
class TempCMD(TransactionsIn):
def run(self) -> None:
assert self.transaction_bundle == TransactionBundle([STD_TX])
Expand All @@ -771,7 +771,7 @@ def test_transactions_out() -> None:
def cmd() -> None:
pass

@chia_command(cmd, "temp_cmd", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd", short_help="blah", help="n/a")
class TempCMD(TransactionsOut):
def run(self) -> None:
self.handle_transaction_output([STD_TX])
Expand Down Expand Up @@ -824,7 +824,7 @@ def cmd() -> None:

coin = Coin(bytes32.zeros, bytes32.zeros, uint64(13))

@chia_command(cmd, "temp_cmd", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd", short_help="blah", help="n/a")
class TempCMD(SPIn):
def run(self) -> None:
assert self.read_sp_input(Coin) == [coin, coin]
Expand Down Expand Up @@ -881,7 +881,7 @@ def cmd() -> None:
coin = Coin(bytes32.zeros, bytes32.zeros, uint64(0))
coin_bytes = byte_serialize_clvm_streamable(coin)

@chia_command(cmd, "temp_cmd", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd", short_help="blah", help="n/a")
class TempCMD(SPOut):
def run(self) -> None:
self.handle_clvm_output([coin, coin])
Expand Down Expand Up @@ -930,7 +930,7 @@ def cmd() -> None:

bytes_to_encode = b"foo bar qat qux bam bat"

@chia_command(cmd, "temp_cmd", "blah", help="n/a")
@chia_command(group=cmd, name="temp_cmd", short_help="blah", help="n/a")
class TempCMD(QrCodeDisplay):
def run(self) -> None:
self.display_qr_codes([bytes_to_encode, bytes_to_encode])
Expand Down
2 changes: 0 additions & 2 deletions chia/cmds/chia.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ def run_daemon_cmd(ctx: click.Context, wait_for_unlock: bool) -> None:


def main() -> None:
import chia.cmds.signer # noqa

cli()


Expand Down
32 changes: 30 additions & 2 deletions chia/cmds/cmd_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ def _convert_class_to_function(cls: type[ChiaCommand]) -> SyncCmd:

@dataclass_transform()
def chia_command(
cmd: click.Group,
*,
group: Optional[click.Group] = None,
name: str,
short_help: str,
help: str,
Expand All @@ -246,12 +247,39 @@ def _chia_command(cls: type[ChiaCommand]) -> type[ChiaCommand]:
kw_only=True,
)(cls)

cmd.command(name, short_help=short_help, help=help)(_convert_class_to_function(wrapped_cls))
metadata = Metadata(
command=click.command(
name=name,
short_help=short_help,
help=help,
)(_convert_class_to_function(wrapped_cls))
)

setattr(wrapped_cls, _chia_command_metadata_attribute, metadata)
if group is not None:
group.add_command(metadata.command)

return wrapped_cls

return _chia_command


_chia_command_metadata_attribute = f"_{__name__.replace('.', '_')}_{chia_command.__qualname__}_metadata"


@dataclass(frozen=True)
class Metadata:
command: click.Command


def get_chia_command_metadata(cls: type[ChiaCommand]) -> Metadata:
metadata: Optional[Metadata] = getattr(cls, _chia_command_metadata_attribute, None)
if metadata is None:
raise Exception(f"Class is not a chia command: {cls}")

return metadata


@dataclass_transform()
def command_helper(cls: type[Any]) -> type[Any]:
if sys.version_info < (3, 10): # stuff below 3.10 doesn't support kw_only
Expand Down
34 changes: 17 additions & 17 deletions chia/cmds/plotnft.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def plotnft_cmd(ctx: click.Context) -> None:


@chia_command(
plotnft_cmd,
"show",
"Show plotnft information",
group=plotnft_cmd,
name="show",
short_help="Show plotnft information",
help="Show plotnft information",
)
class ShowPlotNFTCMD:
Expand All @@ -48,8 +48,8 @@ async def run(self) -> None:


@chia_command(
plotnft_cmd,
"get_login_link",
group=plotnft_cmd,
name="get_login_link",
short_help="Create a login link for a pool",
help="Create a login link for a pool. The farmer must be running. Use 'plotnft show' to get the launcher id.",
)
Expand All @@ -69,8 +69,8 @@ async def run(self) -> None:
# They will therefore not work with observer-only functionality
# NOTE: tx_endpoint (This creates wallet transactions and should be parametrized by relevant options)
@chia_command(
plotnft_cmd,
"create",
group=plotnft_cmd,
name="create",
short_help="Create a plot NFT",
help="Create a plot NFT.",
)
Expand Down Expand Up @@ -116,8 +116,8 @@ async def run(self) -> None:

# NOTE: tx_endpoint
@chia_command(
plotnft_cmd,
"join",
group=plotnft_cmd,
name="join",
short_help="Join a plot NFT to a Pool",
help="Join a plot NFT to a Pool.",
)
Expand Down Expand Up @@ -153,8 +153,8 @@ async def run(self) -> None:

# NOTE: tx_endpoint
@chia_command(
plotnft_cmd,
"leave",
group=plotnft_cmd,
name="leave",
short_help="Leave a pool and return to self-farming",
help="Leave a pool and return to self-farming.",
)
Expand Down Expand Up @@ -187,8 +187,8 @@ async def run(self) -> None:


@chia_command(
plotnft_cmd,
"inspect",
group=plotnft_cmd,
name="inspect",
short_help="Get Detailed plotnft information as JSON",
help="Get Detailed plotnft information as JSON",
)
Expand All @@ -207,8 +207,8 @@ async def run(self) -> None:

# NOTE: tx_endpoint
@chia_command(
plotnft_cmd,
"claim",
group=plotnft_cmd,
name="claim",
short_help="Claim rewards from a plot NFT",
help="Claim rewards from a plot NFT",
)
Expand Down Expand Up @@ -239,8 +239,8 @@ async def run(self) -> None:


@chia_command(
plotnft_cmd,
"change_payout_instructions",
group=plotnft_cmd,
name="change_payout_instructions",
short_help="Change the payout instructions for a pool.",
help="Change the payout instructions for a pool. Use 'plotnft show' to get the launcher id.",
)
Expand Down
Loading
Loading