Browse Source

new oneshot_warning_group class

The MMGen Project 3 years ago
parent
commit
7be1114f3b
4 changed files with 73 additions and 10 deletions
  1. 1 1
      mmgen/rpc.py
  2. 15 8
      mmgen/util.py
  3. 39 0
      test/misc/oneshot_warning.py
  4. 18 1
      test/test_py_d/ts_misc.py

+ 1 - 1
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'

+ 15 - 8
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):

+ 39 - 0
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')

+ 18 - 1
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