From 7e2ef3a05917359e728f9c58e08a3792a4621d1b Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Sat, 4 Sep 2021 18:34:09 +0000 Subject: [PATCH] subclass pwfile_reuse_warning from oneshot_warning --- mmgen/util.py | 40 +++++++++++++++++++----------------- mmgen/wallet.py | 4 ++-- test/misc/oneshot_warning.py | 16 ++++++++++++++- test/test_py_d/ts_misc.py | 21 ++++++++++++------- 4 files changed, 52 insertions(+), 29 deletions(-) diff --git a/mmgen/util.py b/mmgen/util.py index 8f71fe08..e8be7e59 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -747,40 +747,42 @@ def get_data_from_file(infile,desc='data',dash=False,silent=False,binary=False,q class oneshot_warning: - def __init__(self,div=None,fmt_args=[]): - self.do(type(self),div,fmt_args) + color = 'nocolor' - def do(self,wcls,div,fmt_args): + def __init__(self,div=None,fmt_args=[],reverse=False): + self.do(type(self),div,fmt_args,reverse) + + def do(self,wcls,div,fmt_args,reverse): def do_warning(): message = getattr(wcls,'message') color = globals()[getattr(wcls,'color')] msg(color('WARNING: ' + message.format(*fmt_args))) - flag = 'warning_shown' + if not hasattr(wcls,'data'): + setattr(wcls,'data',[]) - if not hasattr(wcls,flag): - setattr(wcls,flag,[]) + data = getattr(wcls,'data') + condition = (div in data) if reverse else (not div in data) - attr = getattr(wcls,flag) + if not div in data: + data.append(div) - if not div in attr: + if condition: do_warning() - attr.append(div) + self.warning_shown = True + else: + self.warning_shown = False class oneshot_warning_group(oneshot_warning): - def __init__(self,wcls,div=None,fmt_args=[]): - self.do(getattr(self,wcls),div,fmt_args) + def __init__(self,wcls,div=None,fmt_args=[],reverse=False): + self.do(getattr(self,wcls),div,fmt_args,reverse) -passwd_files_used = {} - -def pwfile_reuse_warning(passwd_file): - if passwd_file in passwd_files_used: - qmsg(f'Reusing passphrase from file {passwd_file!r} at user request') - return True - passwd_files_used[passwd_file] = True - return False +class pwfile_reuse_warning(oneshot_warning): + message = 'Reusing passphrase from file {!r} at user request' + def __init__(self,fn): + oneshot_warning.__init__(self,div=fn,fmt_args=[fn],reverse=True) def my_raw_input(prompt,echo=True,insert_txt='',use_readline=True): diff --git a/mmgen/wallet.py b/mmgen/wallet.py index b1f84cd3..98a0bffc 100755 --- a/mmgen/wallet.py +++ b/mmgen/wallet.py @@ -329,7 +329,7 @@ class WalletEnc(Wallet): pw = ' '.join(get_words_from_file( self.passwd_file, desc, - quiet = pwfile_reuse_warning(self.passwd_file) )) + quiet = pwfile_reuse_warning(self.passwd_file).warning_shown )) else: qmsg('\n'+fmt(self.msg['choose_passphrase'].format(self.desc,self.ssdata.hash_preset),indent=' ')) if opt.echo_passphrase: @@ -362,7 +362,7 @@ class WalletEnc(Wallet): ret = ' '.join(get_words_from_file( self.passwd_file, desc, - quiet = pwfile_reuse_warning(self.passwd_file) )) + quiet = pwfile_reuse_warning(self.passwd_file).warning_shown )) else: ret = ' '.join(get_words_from_user(f'Enter {desc}: ')) self.ssdata.passwd = ret diff --git a/test/misc/oneshot_warning.py b/test/misc/oneshot_warning.py index 5d33a052..26e0ba57 100755 --- a/test/misc/oneshot_warning.py +++ b/test/misc/oneshot_warning.py @@ -29,11 +29,25 @@ class wg(oneshot_warning_group): message = 'baz variant {} selected' for i in (1,2,3): + + msg('\npw') + for k in ('A','B'): + assert pwfile_reuse_warning(k).warning_shown == (i != 1), 'warning_shown incorrect' + + msg('wg1') wg('foo') + msg('wg2') wg('bar',fmt_args=['dangerous','computer']) + msg('wg3') wg('baz',div='alpha',fmt_args=['alpha']) + msg('wg4') wg('baz',div='beta',fmt_args=['beta']) + + msg('w1') foo(div='alpha',fmt_args=['alpha']) + msg('w2') foo(div='beta',fmt_args=['beta']) + msg('w3') bar() - msg('loop') + + msg('bottom') diff --git a/test/test_py_d/ts_misc.py b/test/test_py_d/ts_misc.py index 91805212..ad97f810 100755 --- a/test/test_py_d/ts_misc.py +++ b/test/test_py_d/ts_misc.py @@ -123,15 +123,22 @@ class TestSuiteOutput(TestSuiteBase): def oneshot_warning(self): t = self.spawn('test/misc/oneshot_warning.py',cmd_dir='.') + t.expect('pw\nwg1') for s in ( 'foo is experimental', - 'The bar command is dangerous', - 'baz variant alpha', - 'baz variant beta', - 'foo variant alpha', - 'foo variant beta', - 'bar is experimental', - 'loop', 'loop', 'loop', + 'wg2', 'The bar command is dangerous', + 'wg3', 'baz variant alpha', + 'wg4', 'baz variant beta', + 'w1', 'foo variant alpha', + 'w2', 'foo variant beta', + 'w3', 'bar is experimental', + 'bottom', + "passphrase from file 'A'", + "passphrase from file 'B'", + 'bottom', + "passphrase from file 'A'", + "passphrase from file 'B'", + 'bottom', ): t.expect(s) return t