From 7be1114f3b9915ec18c37415d0460bb75d32e3b4 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 3 Sep 2021 20:17:24 +0000 Subject: [PATCH] new oneshot_warning_group class --- mmgen/rpc.py | 2 +- mmgen/util.py | 23 +++++++++++++-------- test/misc/oneshot_warning.py | 39 ++++++++++++++++++++++++++++++++++++ test/test_py_d/ts_misc.py | 19 +++++++++++++++++- 4 files changed, 73 insertions(+), 10 deletions(-) create mode 100755 test/misc/oneshot_warning.py diff --git a/mmgen/rpc.py b/mmgen/rpc.py index 1806e62b..7f2ebd05 100755 --- a/mmgen/rpc.py +++ b/mmgen/rpc.py @@ -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' diff --git a/mmgen/util.py b/mmgen/util.py index 2bcd12cf..8f71fe08 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -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): diff --git a/test/misc/oneshot_warning.py b/test/misc/oneshot_warning.py new file mode 100755 index 00000000..5d33a052 --- /dev/null +++ b/test/misc/oneshot_warning.py @@ -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') diff --git a/test/test_py_d/ts_misc.py b/test/test_py_d/ts_misc.py index 558ac09e..91805212 100755 --- a/test/test_py_d/ts_misc.py +++ b/test/test_py_d/ts_misc.py @@ -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