subclass pwfile_reuse_warning from oneshot_warning

This commit is contained in:
The MMGen Project 2021-09-04 18:34:09 +00:00
commit 7e2ef3a059
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
4 changed files with 52 additions and 29 deletions

View file

@ -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):

View file

@ -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

View file

@ -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')

View file

@ -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