From 4b07f8fac81c9aa02b2004512bd64f38632ea325 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Wed, 16 Nov 2022 17:56:04 +0000 Subject: [PATCH] tx.process_cmd_arg(), tx.add_comment(): cleanups --- mmgen/tx/base.py | 25 +++++++++++++------------ mmgen/tx/new.py | 28 ++++++++++++++++------------ 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/mmgen/tx/base.py b/mmgen/tx/base.py index 27b795d5..3d5db4eb 100755 --- a/mmgen/tx/base.py +++ b/mmgen/tx/base.py @@ -155,23 +155,24 @@ class Base(MMGenObject): def add_blockcount(self): self.blockcount = self.rpc.blockcount - # returns true if comment added or changed + # returns True if comment added or changed, False otherwise def add_comment(self,infile=None): if infile: from ..fileutil import get_data_from_file self.comment = MMGenTxComment(get_data_from_file(infile,'transaction comment')) - else: # get comment from user, or edit existing comment - m = ('Add a comment to transaction?','Edit transaction comment?')[bool(self.comment)] + else: from ..ui import keypress_confirm,line_input - if keypress_confirm(m,default_yes=False): - while True: - s = MMGenTxComment(line_input('Comment: ',insert_txt=self.comment)) - if not s: - ymsg('Warning: comment is empty') - save = self.comment - self.comment = s - return (True,False)[save == self.comment] - return False + if keypress_confirm( + prompt = 'Edit transaction comment?' if self.comment else 'Add a comment to transaction?', + default_yes = False ): + res = MMGenTxComment(line_input('Comment: ',insert_txt=self.comment)) + if not res: + ymsg('Warning: comment is empty') + changed = res != self.comment + self.comment = res + return changed + else: + return False def get_non_mmaddrs(self,desc): return remove_dups( diff --git a/mmgen/tx/new.py b/mmgen/tx/new.py index e209785a..69139cf9 100755 --- a/mmgen/tx/new.py +++ b/mmgen/tx/new.py @@ -169,21 +169,25 @@ class New(Base): def process_cmd_arg(self,arg,ad_f,ad_w): - def add_output_chk(addr,amt,err_desc): - if not amt and self.get_chg_output_idx() != None: - die(2,'ERROR: More than one change address listed on command line') - if is_mmgen_id(self.proto,addr) or is_coin_addr(self.proto,addr): - coin_addr = ( mmaddr2coinaddr(addr,ad_w,ad_f,self.proto) if is_mmgen_id(self.proto,addr) - else CoinAddr(self.proto,addr) ) - self.add_output(coin_addr,self.proto.coin_amt(amt or '0'),is_chg=not amt) - else: - die(2,f'{addr}: invalid {err_desc} {{!r}}'.format(f'{addr},{amt}' if amt else addr)) - if ',' in arg: addr,amt = arg.split(',',1) - add_output_chk(addr,amt,'coin argument in command-line argument') + err_desc = 'coin argument in command-line argument' else: - add_output_chk(arg,None,'command-line argument') + addr,amt = (arg,None) + err_desc = 'command-line argument' + + if is_mmgen_id(self.proto,addr): + coin_addr = mmaddr2coinaddr(addr,ad_w,ad_f,self.proto) + elif is_coin_addr(self.proto,addr): + coin_addr = CoinAddr(self.proto,addr) + else: + die(2,f'{addr}: invalid {err_desc} {{!r}}'.format(f'{addr},{amt}' if amt else addr)) + + if not amt and self.get_chg_output_idx() is not None: + die(2,'ERROR: More than one change address {} on command line'.format( + 'requested' if self.chg_autoselected else 'listed')) + + self.add_output(coin_addr,self.proto.coin_amt(amt or '0'),is_chg=not amt) def process_cmd_args(self,cmd_args,ad_f,ad_w):