Skip to content

Commit 5e67e00

Browse files
committed
refactor(query_group): simplify CLI argument construction
Refactored the construction of CLI arguments in several methods of `QueryGroup` to improve readability and reduce mutation of argument lists. Introduced local variables for input arguments and used unpacking to build final CLI command lists. Added input validation for missing arguments. This change makes the code more maintainable and less error-prone.
1 parent e3757ee commit 5e67e00

File tree

1 file changed

+49
-48
lines changed

1 file changed

+49
-48
lines changed

cardano_clusterlib/query_group.py

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,22 @@ def get_utxo(
8080
Returns:
8181
list[structs.UTXOData]: A list of UTxO data.
8282
"""
83-
cli_args = ["utxo", "--output-json"]
84-
8583
address_single = ""
8684
sort_results = False
8785
if address:
8886
if isinstance(address, str):
8987
address_single = address
90-
address = [address]
91-
cli_args.extend(helpers._prepend_flag("--address", address))
88+
addresses = [address]
89+
else:
90+
addresses = address
91+
input_args = helpers._prepend_flag("--address", addresses)
9292
elif txin:
93-
if isinstance(txin, str):
94-
txin = [txin]
95-
cli_args.extend(helpers._prepend_flag("--tx-in", txin))
93+
txins = [txin] if isinstance(txin, str) else txin
94+
input_args = helpers._prepend_flag("--tx-in", txins)
9695
elif utxo:
97-
if isinstance(utxo, structs.UTXOData):
98-
utxo = [utxo]
99-
utxo_formatted = [f"{u.utxo_hash}#{u.utxo_ix}" for u in utxo]
100-
cli_args.extend(helpers._prepend_flag("--tx-in", utxo_formatted))
96+
utxos = [utxo] if isinstance(utxo, structs.UTXOData) else utxo
97+
utxo_formatted = [f"{u.utxo_hash}#{u.utxo_ix}" for u in utxos]
98+
input_args = helpers._prepend_flag("--tx-in", utxo_formatted)
10199
elif tx_raw_output:
102100
sort_results = True
103101
change_txout_num = 1 if tx_raw_output.change_address else 0
@@ -109,16 +107,21 @@ def get_utxo(
109107
tx_body_file=tx_raw_output.out_file
110108
)
111109
utxo_formatted = [f"{utxo_hash}#{ix}" for ix in range(num_of_txouts)]
112-
cli_args.extend(helpers._prepend_flag("--tx-in", utxo_formatted))
110+
input_args = helpers._prepend_flag("--tx-in", utxo_formatted)
113111
else:
114112
msg = "Either `address`, `txin`, `utxo` or `tx_raw_output` need to be specified."
115113
raise ValueError(msg)
116114

115+
if not input_args:
116+
msg = "No input arguments for UTxO query."
117+
raise ValueError(msg)
118+
119+
cli_args = ["utxo", "--output-json", *input_args]
117120
utxo_dict = json.loads(self.query_cli(cli_args))
118-
utxos = txtools.get_utxo(utxo_dict=utxo_dict, address=address_single, coins=coins)
121+
out_utxos = txtools.get_utxo(utxo_dict=utxo_dict, address=address_single, coins=coins)
119122
if sort_results:
120-
utxos = sorted(utxos, key=lambda u: u.utxo_ix)
121-
return utxos
123+
out_utxos = sorted(out_utxos, key=lambda u: u.utxo_ix)
124+
return out_utxos
122125

123126
def get_tip(self) -> dict[str, tp.Any]:
124127
"""Return current tip - last block successfully applied to the ledger."""
@@ -190,14 +193,16 @@ def get_stake_snapshot(
190193
Returns:
191194
dict: A stake snapshot data.
192195
"""
193-
query_args = ["stake-snapshot"]
194-
195196
if all_stake_pools:
196-
query_args.extend(["--all-stake-pools"])
197+
query_args = ["--all-stake-pools"]
197198
elif stake_pool_ids:
198-
query_args.extend(helpers._prepend_flag("--stake-pool-id", stake_pool_ids))
199+
query_args = helpers._prepend_flag("--stake-pool-id", stake_pool_ids)
200+
else:
201+
msg = "Either `stake_pool_ids` or `all_stake_pools` need to be specified."
202+
raise ValueError(msg)
199203

200-
stake_snapshot: dict[str, tp.Any] = json.loads(self.query_cli(query_args))
204+
cli_args = ["stake-snapshot", *query_args]
205+
stake_snapshot: dict[str, tp.Any] = json.loads(self.query_cli(cli_args))
201206
return stake_snapshot
202207

203208
def get_pool_params(
@@ -372,34 +377,26 @@ def get_leadership_schedule(
372377
list[structs.LeadershipSchedule]: A list of `structs.LeadershipSchedule`, specifying
373378
slot and time.
374379
"""
375-
args = []
376-
377380
if stake_pool_vkey:
378-
args.extend(
379-
[
380-
"--stake-pool-verification-key",
381-
str(stake_pool_vkey),
382-
]
383-
)
381+
pool_args = [
382+
"--stake-pool-verification-key",
383+
str(stake_pool_vkey),
384+
]
384385
elif cold_vkey_file:
385-
args.extend(
386-
[
387-
"--cold-verification-key-file",
388-
str(cold_vkey_file),
389-
]
390-
)
386+
pool_args = [
387+
"--cold-verification-key-file",
388+
str(cold_vkey_file),
389+
]
391390
elif stake_pool_id:
392-
args.extend(
393-
[
394-
"--stake-pool-id",
395-
str(stake_pool_id),
396-
]
397-
)
391+
pool_args = [
392+
"--stake-pool-id",
393+
str(stake_pool_id),
394+
]
398395
else:
399396
msg = "Either `stake_pool_vkey`, `cold_vkey_file` or `stake_pool_id` is needed."
400397
raise ValueError(msg)
401398

402-
args.append("--next" if for_next else "--current")
399+
epoch_arg = "--next" if for_next else "--current"
403400

404401
unparsed = self.query_cli(
405402
[
@@ -409,7 +406,8 @@ def get_leadership_schedule(
409406
str(self._clusterlib_obj.genesis_json),
410407
"--vrf-signing-key-file",
411408
str(vrf_skey_file),
412-
*args,
409+
*pool_args,
410+
epoch_arg,
413411
]
414412
# Schedule values are displayed starting with line 2 of the command output
415413
).splitlines()[2:]
@@ -694,15 +692,18 @@ def get_proposals(
694692
msg = "Both `action_txid` and `action_ix` must be specified together."
695693
raise ValueError(msg)
696694

697-
query_args = ["proposals"]
698-
699695
if action_txid and action_ix is not None:
700-
query_args.extend(["--governance-action-tx-id", action_txid])
701-
query_args.extend(["--governance-action-index", str(action_ix)])
696+
query_args = [
697+
"--governance-action-tx-id",
698+
action_txid,
699+
"--governance-action-index",
700+
str(action_ix),
701+
]
702702
else:
703-
query_args.append("--all-proposals")
703+
query_args = ["--all-proposals"]
704704

705-
proposals: list[dict[str, tp.Any]] = json.loads(self.query_cli(query_args))
705+
cli_args = ["proposals", *query_args]
706+
proposals: list[dict[str, tp.Any]] = json.loads(self.query_cli(cli_args))
706707
return proposals
707708

708709
def get_treasury(self) -> int:

0 commit comments

Comments
 (0)