new oneshot_warning_group class

This commit is contained in:
The MMGen Project 2021-09-03 20:17:24 +00:00
commit 7be1114f3b
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
4 changed files with 73 additions and 10 deletions

View file

@ -711,7 +711,7 @@ class MoneroWalletRPCClient(MoneroRPCClient):
'refresh', # start_height
)
class daemon_warning(oneshot_warning):
class daemon_warning(oneshot_warning_group):
class geth:
color = 'yellow'

View file

@ -747,25 +747,32 @@ def get_data_from_file(infile,desc='data',dash=False,silent=False,binary=False,q
class oneshot_warning:
def __init__(self,wcls,div=None,fmt_args=[]):
def __init__(self,div=None,fmt_args=[]):
self.do(type(self),div,fmt_args)
def do(self,wcls,div,fmt_args):
def do_warning():
cls = getattr(self,wcls)
message = getattr(cls,'message')
color = globals()[getattr(cls,'color')]
message = getattr(wcls,'message')
color = globals()[getattr(wcls,'color')]
msg(color('WARNING: ' + message.format(*fmt_args)))
flag = wcls+'_warning_shown'
flag = 'warning_shown'
if not hasattr(self,flag):
setattr(type(self),flag,[])
if not hasattr(wcls,flag):
setattr(wcls,flag,[])
attr = getattr(type(self),flag)
attr = getattr(wcls,flag)
if not div in attr:
do_warning()
attr.append(div)
class oneshot_warning_group(oneshot_warning):
def __init__(self,wcls,div=None,fmt_args=[]):
self.do(getattr(self,wcls),div,fmt_args)
passwd_files_used = {}
def pwfile_reuse_warning(passwd_file):

39
test/misc/oneshot_warning.py Executable file
View file

@ -0,0 +1,39 @@
#!/usr/bin/env python3
from mmgen.common import *
cmd_args = opts.init()
class foo(oneshot_warning):
color = 'purple'
message = 'foo variant {} selected'
class bar(oneshot_warning):
color = 'yellow'
message = 'bar is experimental software'
class wg(oneshot_warning_group):
class foo:
color = 'yellow'
message = 'foo is experimental software. Proceed at your own risk'
class bar:
color = 'red'
message = 'The bar command is {} and can break your {}!!!'
class baz:
color = 'orange'
message = 'baz variant {} selected'
for i in (1,2,3):
wg('foo')
wg('bar',fmt_args=['dangerous','computer'])
wg('baz',div='alpha',fmt_args=['alpha'])
wg('baz',div='beta',fmt_args=['beta'])
foo(div='alpha',fmt_args=['alpha'])
foo(div='beta',fmt_args=['beta'])
bar()
msg('loop')

View file

@ -106,8 +106,10 @@ class TestSuiteOutput(TestSuiteBase):
('output_gr', (1,"Greek text", [])),
('output_ru', (1,"Russian text", [])),
('output_zh', (1,"Chinese text", [])),
('output_jp', (1,"Japanese text", []))
('output_jp', (1,"Japanese text", [])),
('oneshot_warning', (1,"Oneshot warnings", []))
)
color = True
def screen_output(self,lang):
t = self.spawn('test/misc/utf8_output.py',[lang],cmd_dir='.')
@ -119,6 +121,21 @@ class TestSuiteOutput(TestSuiteBase):
def output_zh(self): return self.screen_output('zh')
def output_jp(self): return self.screen_output('jp')
def oneshot_warning(self):
t = self.spawn('test/misc/oneshot_warning.py',cmd_dir='.')
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',
):
t.expect(s)
return t
class TestSuiteRefTX(TestSuiteMain,TestSuiteBase):
'create a reference transaction file (administrative command)'
segwit_opts_ok = False