From df49bc851c0c95370192c841fee334746456ff54 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Tue, 23 Sep 2025 09:20:55 +0000 Subject: [PATCH] main_*: use match statement where practicable (6 files) --- mmgen/main_addrimport.py | 43 ++++++++++++------------- mmgen/main_autosign.py | 69 ++++++++++++++++++++-------------------- mmgen/main_msg.py | 25 ++++++++------- mmgen/main_tool.py | 37 ++++++++++----------- mmgen/main_txsend.py | 42 ++++++++++++------------ mmgen/main_xmrwallet.py | 43 +++++++++++++------------ 6 files changed, 131 insertions(+), 128 deletions(-) diff --git a/mmgen/main_addrimport.py b/mmgen/main_addrimport.py index 320d7178..149dc205 100755 --- a/mmgen/main_addrimport.py +++ b/mmgen/main_addrimport.py @@ -86,28 +86,27 @@ def parse_cmd_args(cmd_args): def import_mmgen_list(infile): return (AddrList, KeyAddrList)[bool(cfg.keyaddr_file)](cfg, proto, infile=infile) - if len(cmd_args) == 1: - infile = cmd_args[0] - from .fileutil import check_infile, get_lines_from_file - check_infile(infile) - if cfg.addrlist: - al = AddrList( - cfg = cfg, - proto = proto, - addrlist = get_lines_from_file( - cfg, - infile, - desc = f'non-{gc.proj_name} addresses', - trim_comments = True)) - else: - al = import_mmgen_list(infile) - elif len(cmd_args) == 0 and cfg.address: - al = AddrList(cfg, proto=proto, addrlist=[cfg.address]) - infile = 'command line' - else: - die(1, addrimport_msgs['bad_args']) - - return al, infile + match cmd_args: + case [infile]: + from .fileutil import check_infile, get_lines_from_file + check_infile(infile) + if cfg.addrlist: + return ( + AddrList( + cfg = cfg, + proto = proto, + addrlist = get_lines_from_file( + cfg, + infile, + desc = f'non-{gc.proj_name} addresses', + trim_comments = True)), + infile) + else: + return (import_mmgen_list(infile), infile) + case [] if cfg.address: + return (AddrList(cfg, proto=proto, addrlist=[cfg.address]), 'command line') + case _: + die(1, addrimport_msgs['bad_args']) def check_opts(twctl): batch = bool(cfg.batch) diff --git a/mmgen/main_autosign.py b/mmgen/main_autosign.py index 0013947d..cbbfe931 100755 --- a/mmgen/main_autosign.py +++ b/mmgen/main_autosign.py @@ -221,38 +221,39 @@ asi = Autosign(cfg, cmd=cmd) cfg._post_init() -if cmd == 'gen_key': - asi.gen_key() -elif cmd == 'setup': - asi.setup() - from .ui import keypress_confirm - if cfg.xmrwallets and keypress_confirm(cfg, '\nContinue with Monero setup?', default_yes=True): - msg('') +match cmd: + case 'gen_key': + asi.gen_key() + case 'setup': + asi.setup() + from .ui import keypress_confirm + if cfg.xmrwallets and keypress_confirm(cfg, '\nContinue with Monero setup?', default_yes=True): + msg('') + asi.xmr_setup() + asi.do_umount() + case 'xmr_setup': + if not cfg.xmrwallets: + die(1, 'Please specify a wallet or range of wallets with the --xmrwallets option') + asi.do_mount() asi.xmr_setup() - asi.do_umount() -elif cmd == 'xmr_setup': - if not cfg.xmrwallets: - die(1, 'Please specify a wallet or range of wallets with the --xmrwallets option') - asi.do_mount() - asi.xmr_setup() - asi.do_umount() -elif cmd.startswith('macos_ramdisk'): - if sys.platform != 'darwin': - die(1, f'The ‘{cmd}’ operation is for the macOS platform only') - getattr(asi, cmd)() -elif cmd == 'enable_swap': - asi.swap.enable() -elif cmd == 'disable_swap': - asi.swap.disable() -elif cmd == 'sign': - main(do_loop=False) -elif cmd == 'wait': - main(do_loop=True) -elif cmd == 'clean': - asi.do_mount() - asi.clean_old_files() - asi.do_umount() -elif cmd == 'wipe_key': - asi.do_mount() - asi.wipe_encryption_key() - asi.do_umount() + asi.do_umount() + case 'macos_ramdisk_setup' | 'macos_ramdisk_delete': + if sys.platform != 'darwin': + die(1, f'The ‘{cmd}’ operation is for the macOS platform only') + getattr(asi, cmd)() + case 'enable_swap': + asi.swap.enable() + case 'disable_swap': + asi.swap.disable() + case 'sign': + main(do_loop=False) + case 'wait': + main(do_loop=True) + case 'clean': + asi.do_mount() + asi.clean_old_files() + asi.do_umount() + case 'wipe_key': + asi.do_mount() + asi.wipe_encryption_key() + asi.do_umount() diff --git a/mmgen/main_msg.py b/mmgen/main_msg.py index fc58e8ff..f8f7c98e 100755 --- a/mmgen/main_msg.py +++ b/mmgen/main_msg.py @@ -213,17 +213,18 @@ if cfg.msghash_type and op != 'create': die(1, '--msghash-type option may only be used with the "create" command') async def main(): - if op == 'create': - if not cmd_args: - cfg._usage() - MsgOps.create(arg1, ' '.join(cmd_args)) - elif op == 'sign': - await MsgOps.sign(arg1, cmd_args[:]) - elif op in ('verify', 'export'): - if len(cmd_args) not in (0, 1): - cfg._usage() - await getattr(MsgOps, op)(arg1, addr=cmd_args[0] if cmd_args else None) - else: - die(1, f'{op!r}: unrecognized operation') + match op: + case 'create': + if not cmd_args: + cfg._usage() + MsgOps.create(arg1, ' '.join(cmd_args)) + case 'sign': + await MsgOps.sign(arg1, cmd_args[:]) + case 'verify' | 'export': + if len(cmd_args) not in (0, 1): + cfg._usage() + await getattr(MsgOps, op)(arg1, addr=cmd_args[0] if cmd_args else None) + case _: + die(1, f'{op!r}: unrecognized operation') async_run(main()) diff --git a/mmgen/main_tool.py b/mmgen/main_tool.py index 71d47d26..72fcfa4c 100755 --- a/mmgen/main_tool.py +++ b/mmgen/main_tool.py @@ -321,24 +321,25 @@ def process_result(ret, *, pager=False, print_result=False): else: return o - if ret is True: - return True - elif ret in (False, None): - die(2, f'tool command returned {ret!r}') - elif isinstance(ret, str): - return triage_result(ret) - elif isinstance(ret, int): - return triage_result(str(ret)) - elif isinstance(ret, tuple): - return triage_result('\n'.join([r.decode() if isinstance(r, bytes) else r for r in ret])) - elif isinstance(ret, bytes): - try: - return triage_result(ret.decode()) - except: - # don't add NL to binary data if it can't be converted to utf8 - return os.write(1, ret) if print_result else ret - else: - die(2, f'tool.py: can’t handle return value of type {type(ret).__name__!r}') + match ret: + case True: + return True + case False | None: + die(2, f'tool command returned {ret!r}') + case str(): + return triage_result(ret) + case int(): + return triage_result(str(ret)) + case tuple(): + return triage_result('\n'.join([r.decode() if isinstance(r, bytes) else r for r in ret])) + case bytes(): + try: + return triage_result(ret.decode()) + except: + # don't add NL to binary data if it can't be converted to utf8 + return os.write(1, ret) if print_result else ret + case _: + die(2, f'tool.py: can’t handle return value of type {type(ret).__name__!r}') def get_cmd_cls(cmd): for modname, cmdlist in mods.items(): diff --git a/mmgen/main_txsend.py b/mmgen/main_txsend.py index 0d8ee8dc..abd11fe8 100755 --- a/mmgen/main_txsend.py +++ b/mmgen/main_txsend.py @@ -90,27 +90,27 @@ if cfg.dump_hex and cfg.dump_hex != '-': asi = None -if len(cfg._args) == 1: - infile = cfg._args[0] - from .fileutil import check_infile - check_infile(infile) -elif not cfg._args and cfg.autosign: - from .tx.util import mount_removable_device - from .autosign import Signable - asi = mount_removable_device(cfg) - si = Signable.automount_transaction(asi) - if cfg.abort: - si.shred_abortable() # prompts user, then raises exception or exits - elif cfg.status or cfg.receipt: - if si.unsent: - die(1, 'Transaction is unsent') - if si.unsigned: - die(1, 'Transaction is unsigned') - else: - infile = si.get_unsent() - cfg._util.qmsg(f'Got signed transaction file ‘{infile}’') -else: - cfg._usage() +match cfg._args: + case [infile]: + from .fileutil import check_infile + check_infile(infile) + case [] if cfg.autosign: + from .tx.util import mount_removable_device + from .autosign import Signable + asi = mount_removable_device(cfg) + si = Signable.automount_transaction(asi) + if cfg.abort: + si.shred_abortable() # prompts user, then raises exception or exits + elif cfg.status or cfg.receipt: + if si.unsent: + die(1, 'Transaction is unsent') + if si.unsigned: + die(1, 'Transaction is unsigned') + else: + infile = si.get_unsent() + cfg._util.qmsg(f'Got signed transaction file ‘{infile}’') + case _: + cfg._usage() if not cfg.status: from .ui import do_license_msg diff --git a/mmgen/main_xmrwallet.py b/mmgen/main_xmrwallet.py index 029ef510..015d5995 100755 --- a/mmgen/main_xmrwallet.py +++ b/mmgen/main_xmrwallet.py @@ -121,27 +121,28 @@ op = cmd_args.pop(0) infile = cmd_args.pop(0) wallets = spec = None -if op in ('relay', 'submit', 'resubmit', 'abort'): - if len(cmd_args) != 0: - cfg._usage() -elif op in ('txview', 'txlist'): - infile = [infile] + cmd_args -elif op in ('create', 'sync', 'list', 'view', 'listview', 'dump', 'restore'): # kafile_arg_ops - if len(cmd_args) > 1: - cfg._usage() - wallets = cmd_args.pop(0) if cmd_args else None -elif op in ('new', 'transfer', 'sweep', 'sweep_all', 'label'): - if len(cmd_args) != 1: - cfg._usage() - spec = cmd_args[0] -elif op in ('export-outputs', 'export-outputs-sign', 'import-key-images'): - if not cfg.autosign: - die(1, f'--autosign must be used with command {op!r}') - if len(cmd_args) > 1: - cfg._usage() - wallets = cmd_args.pop(0) if cmd_args else None -else: - die(1, f'{op!r}: unrecognized operation') +match op: + case 'relay' | 'submit' | 'resubmit' | 'abort': + if len(cmd_args) != 0: + cfg._usage() + case 'txview' | 'txlist': + infile = [infile] + cmd_args + case 'create' | 'sync' | 'list' | 'view' | 'listview' | 'dump' | 'restore': # kafile_arg_ops + if len(cmd_args) > 1: + cfg._usage() + wallets = cmd_args.pop(0) if cmd_args else None + case 'new' | 'transfer' | 'sweep' | 'sweep_all' | 'label': + if len(cmd_args) != 1: + cfg._usage() + spec = cmd_args[0] + case 'export-outputs' | 'export-outputs-sign' | 'import-key-images': + if not cfg.autosign: + die(1, f'--autosign must be used with command {op!r}') + if len(cmd_args) > 1: + cfg._usage() + wallets = cmd_args.pop(0) if cmd_args else None + case _: + die(1, f'{op!r}: unrecognized operation') m = xmrwallet.op(op, cfg, infile, wallets, spec=spec)