From 224b6a70d0776f20a60ec8caf45690924819ea69 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 11 Feb 2022 13:40:35 +0000 Subject: [PATCH] confirm_or_raise() cleanups --- mmgen/fileutil.py | 12 +++++++++--- mmgen/main_addrimport.py | 5 ++++- mmgen/tx/online.py | 6 +++--- mmgen/util.py | 9 +++++---- mmgen/wallet/incog_hidden.py | 4 +++- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/mmgen/fileutil.py b/mmgen/fileutil.py index 7a1a6861..fc2ad0b0 100755 --- a/mmgen/fileutil.py +++ b/mmgen/fileutil.py @@ -177,7 +177,9 @@ def write_data_to_file( outfile,data,desc='data', if no_tty: die(2,f'Printing {desc} to screen is not allowed') if (ask_tty and not opt.quiet) or binary: - confirm_or_raise('',f'output {desc} to screen') + confirm_or_raise( + message = '', + action = f'output {desc} to screen' ) else: try: of = os.readlink(f'/proc/{os.getpid()}/fd/1') # Linux except: of = None # Windows @@ -187,7 +189,9 @@ def write_data_to_file( outfile,data,desc='data', if no_tty: die(2,f'Writing {desc} to pipe is not allowed') if ask_tty and not opt.quiet: - confirm_or_raise('',f'output {desc} to pipe') + confirm_or_raise( + message = '', + action = f'output {desc} to pipe' ) msg('') of2,pd = os.path.relpath(of),os.path.pardir msg('Redirecting output to file {!r}'.format(of if of2[:len(pd)] == pd else of2)) @@ -217,7 +221,9 @@ def write_data_to_file( outfile,data,desc='data', hush = False if os.path.lexists(outfile) and ask_overwrite: - confirm_or_raise('',f'File {outfile!r} already exists\nOverwrite?') + confirm_or_raise( + message = '', + action = f'File {outfile!r} already exists\nOverwrite?' ) msg(f'Overwriting file {outfile!r}') hush = True diff --git a/mmgen/main_addrimport.py b/mmgen/main_addrimport.py index 9b7ea085..d718db25 100755 --- a/mmgen/main_addrimport.py +++ b/mmgen/main_addrimport.py @@ -109,7 +109,10 @@ def check_opts(tw): rescan = False if rescan and not opt.quiet: - confirm_or_raise(ai_msgs('rescan'),'continue',expect='YES') + confirm_or_raise( + message = ai_msgs('rescan'), + action = 'continue', + expect = 'YES' ) if batch and not 'batch' in tw.caps: msg(f"'--batch' ignored: not supported by {type(tw).__name__}") diff --git a/mmgen/tx/online.py b/mmgen/tx/online.py index 138b3ed4..ba262378 100755 --- a/mmgen/tx/online.py +++ b/mmgen/tx/online.py @@ -25,7 +25,7 @@ class OnlineSigned(Signed): def confirm_send(self): confirm_or_raise( - ('' if opt.quiet else "Once this transaction is sent, there's no taking it back!"), - f'broadcast this transaction to the {self.proto.coin} {self.proto.network.upper()} network', - ('YES' if opt.quiet or opt.yes else 'YES, I REALLY WANT TO DO THIS') ) + message = '' if opt.quiet else 'Once this transaction is sent, there’s no taking it back!', + action = f'broadcast this transaction to the {self.proto.coin} {self.proto.network.upper()} network', + expect = 'YES' if opt.quiet or opt.yes else 'YES, I REALLY WANT TO DO THIS' ) msg('Sending transaction') diff --git a/mmgen/util.py b/mmgen/util.py index c07dbcda..418deef0 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -459,12 +459,13 @@ def check_wallet_extension(fn): def make_full_path(outdir,outfile): return os.path.normpath(os.path.join(outdir, os.path.basename(outfile))) -def confirm_or_raise(message,q,expect='YES',exit_msg='Exiting at user request'): +def confirm_or_raise(message,action,expect='YES',exit_msg='Exiting at user request'): if message.strip(): msg(message.strip()) - a = f'{q} ' if q[0].isupper() else f'Are you sure you want to {q}?\n' - b = f'Type uppercase {expect!r} to confirm: ' - if line_input(a+b).strip() != expect: + if line_input( + (f'{action} ' if action[0].isupper() else f'Are you sure you want to {action}?\n') + + f'Type uppercase {expect!r} to confirm: ' + ).strip() != expect: die( 'UserNonConfirmation', exit_msg ) def get_words_from_user(prompt): diff --git a/mmgen/wallet/incog_hidden.py b/mmgen/wallet/incog_hidden.py index 2976ac5b..668c9fd1 100755 --- a/mmgen/wallet/incog_hidden.py +++ b/mmgen/wallet/incog_hidden.py @@ -136,7 +136,9 @@ class wallet(wallet): if check_offset: self._check_valid_offset(f,'write') if not opt.quiet: - confirm_or_raise( '', f'alter file {f.name!r}' ) + confirm_or_raise( + message = '', + action = f'alter file {f.name!r}' ) flgs = os.O_RDWR|os.O_BINARY if g.platform == 'win' else os.O_RDWR fh = os.open(f.name,flgs)