From b00c11b578e8db8c755141545e0d3fda35c27ffd Mon Sep 17 00:00:00 2001 From: MMGen Date: Tue, 16 Oct 2018 16:28:33 +0000 Subject: [PATCH] confirm_or_exit() -> confirm_or_raise() --- mmgen/exception.py | 1 + mmgen/main.py | 6 ++++-- mmgen/main_addrimport.py | 2 +- mmgen/main_wallet.py | 2 +- mmgen/seed.py | 2 +- mmgen/tw.py | 8 ++++++-- mmgen/tx.py | 2 +- mmgen/util.py | 10 +++++----- 8 files changed, 20 insertions(+), 13 deletions(-) diff --git a/mmgen/exception.py b/mmgen/exception.py index 2b83c84d..7fb09219 100755 --- a/mmgen/exception.py +++ b/mmgen/exception.py @@ -23,3 +23,4 @@ mmgen.exception: Exception classes for the MMGen suite class UnrecognizedTokenSymbol(Exception): pass class TokenNotInBlockchain(Exception): pass +class UserNonConfirmation(Exception): pass diff --git a/mmgen/main.py b/mmgen/main.py index 743b1d5d..446cb6d9 100755 --- a/mmgen/main.py +++ b/mmgen/main.py @@ -59,5 +59,7 @@ def launch(what): except: try: m = e.message.decode('utf8') except: m = repr(e.message) - from mmgen.util import ydie - ydie(2,u'\nERROR: ' + m) + + from mmgen.util import die,ydie + if type(e).__name__ == 'UserNonConfirmation': die(1,m) + else: ydie(2,u'\nERROR: ' + m) diff --git a/mmgen/main_addrimport.py b/mmgen/main_addrimport.py index 7a5e5a88..1ea691d2 100755 --- a/mmgen/main_addrimport.py +++ b/mmgen/main_addrimport.py @@ -115,7 +115,7 @@ if opt.rescan and not 'rescan' in tw.caps: opt.rescan = False if opt.rescan and not opt.quiet: - confirm_or_exit(ai_msgs('rescan'),'continue',expect='YES') + confirm_or_raise(ai_msgs('rescan'),'continue',expect='YES') if opt.batch and not 'batch' in tw.caps: msg("'--batch' ignored: not supported by {}".format(type(tw).__name__)) diff --git a/mmgen/main_wallet.py b/mmgen/main_wallet.py index 5b35e68d..e81d90a1 100755 --- a/mmgen/main_wallet.py +++ b/mmgen/main_wallet.py @@ -145,7 +145,7 @@ if invoked_as == 'passchg': if invoked_as == 'passchg' and ss_in.infile.dirname == g.data_dir: m1 = yellow('Confirmation of default wallet update') m2 = 'update the default wallet' - confirm_or_exit(m1,m2,exit_msg='Password not changed') + confirm_or_raise(m1,m2,exit_msg='Password not changed') ss_out.write_to_file(desc='New wallet',outdir=g.data_dir) msg('Securely deleting old wallet') from subprocess import check_output,CalledProcessError diff --git a/mmgen/seed.py b/mmgen/seed.py index 76966235..83b623e4 100755 --- a/mmgen/seed.py +++ b/mmgen/seed.py @@ -1041,7 +1041,7 @@ harder to find, you're advised to choose a much larger file size than this. if check_offset: self._check_valid_offset(f,'write') if not opt.quiet: - confirm_or_exit('',"alter file '{}'".format(f.name)) + confirm_or_raise('',"alter file '{}'".format(f.name)) flgs = os.O_RDWR|os.O_BINARY if g.platform == 'win' else os.O_RDWR fh = os.open(f.name,flgs) diff --git a/mmgen/tw.py b/mmgen/tw.py index b0271ded..d441de62 100755 --- a/mmgen/tw.py +++ b/mmgen/tw.py @@ -360,8 +360,12 @@ watch-only wallet using '{}-addrimport' and then re-run this program. of = '{}-{}[{}].out'.format(self.dump_fn_pfx,g.dcoin, ','.join(self.sort_info(include_group=False)).lower()) msg('') - write_data_to_file(of,self.format_for_printing(),desc='{} listing'.format(self.desc)) - oneshot_msg = yellow("Data written to '{}'\n\n".format(of)) + try: + write_data_to_file(of,self.format_for_printing(),desc='{} listing'.format(self.desc)) + except UserNonConfirmation as e: + oneshot_msg = red("File '{}' not overwritten by user request\n\n".format(of)) + else: + oneshot_msg = yellow("Data written to '{}'\n\n".format(of)) elif action in ('a_view','a_view_wide'): do_pager(self.fmt_display if action == 'a_view' else self.format_for_printing(color=True)) if g.platform == 'linux' and oneshot_msg == None: diff --git a/mmgen/tx.py b/mmgen/tx.py index 2f6f4e49..4044c6a3 100755 --- a/mmgen/tx.py +++ b/mmgen/tx.py @@ -888,7 +888,7 @@ Selected non-{pnm} inputs: {{}}""".strip().format(pnm=g.proj_name,pnl=g.proj_nam m1 = ("Once this transaction is sent, there's no taking it back!",'')[bool(opt.quiet)] m2 = 'broadcast this transaction to the {} network'.format(g.chain.upper()) m3 = ('YES, I REALLY WANT TO DO THIS','YES')[bool(opt.quiet or opt.yes)] - confirm_or_exit(m1,m2,m3) + confirm_or_raise(m1,m2,m3) msg('Sending transaction') def send(self,prompt_user=True,exit_on_fail=False): diff --git a/mmgen/util.py b/mmgen/util.py index cf1b56fa..c650dcfc 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -521,13 +521,13 @@ def get_new_passphrase(desc,passchg=False): if pw == '': qmsg('WARNING: Empty passphrase') return pw -def confirm_or_exit(message,q,expect='YES',exit_msg='Exiting at user request'): +def confirm_or_raise(message,q,expect='YES',exit_msg='Exiting at user request'): m = message.strip() if m: msg(m) a = q+' ' if q[0].isupper() else 'Are you sure you want to {}?\n'.format(q) b = "Type uppercase '{}' to confirm: ".format(expect) if my_raw_input(a+b).strip() != expect: - die(1,exit_msg) + raise UserNonConfirmation,exit_msg def write_data_to_file( outfile,data,desc='data', ask_write=False, @@ -557,7 +557,7 @@ def write_data_to_file( outfile,data,desc='data', if no_tty: die(2,'Printing {} to screen is not allowed'.format(desc)) if (ask_tty and not opt.quiet) or binary: - confirm_or_exit('','output {} to screen'.format(desc)) + confirm_or_raise('','output {} to screen'.format(desc)) else: try: of = os.readlink('/proc/{}/fd/1'.format(os.getpid())) # Linux except: of = None # Windows @@ -567,7 +567,7 @@ def write_data_to_file( outfile,data,desc='data', if no_tty: die(2,'Writing {} to pipe is not allowed'.format(desc)) if ask_tty and not opt.quiet: - confirm_or_exit('','output {} to pipe'.format(desc)) + confirm_or_raise('','output {} to pipe'.format(desc)) msg('') of2,pd = os.path.relpath(of),os.path.pardir msg(u"Redirecting output to file '{}'".format((of2,of)[of2[:len(pd)] == pd])) @@ -593,7 +593,7 @@ def write_data_to_file( outfile,data,desc='data', hush = False if file_exists(outfile) and ask_overwrite: q = u"File '{}' already exists\nOverwrite?".format(outfile) - confirm_or_exit('',q) + confirm_or_raise('',q) msg(u"Overwriting file '{}'".format(outfile)) hush = True