Browse Source

add oneshot_warning class, daemon_warning subclass

The MMGen Project 3 years ago
parent
commit
8965c30aff
2 changed files with 33 additions and 9 deletions
  1. 12 9
      mmgen/rpc.py
  2. 21 0
      mmgen/util.py

+ 12 - 9
mmgen/rpc.py

@@ -625,7 +625,7 @@ class EthereumRPCClient(RPCClient,metaclass=aInitMeta):
 			self.chainID = None if ci == None else Int(ci,16) # parity/oe return chainID only for dev chain
 			self.chain = (await self.call('parity_chain')).replace(' ','_').replace('_testnet','')
 		elif self.daemon.id == 'erigon':
-			do_erigon_warning()
+			daemon_warning(self.daemon.id)
 			self.caps += ('full_node',)
 			self.chainID = Int(ci,16)
 			self.chain = self.proto.chain_ids[self.chainID]
@@ -716,16 +716,19 @@ class MoneroWalletRPCClient(MoneroRPCClient):
 		'refresh',       # start_height
 	)
 
-def do_erigon_warning(erigon_warning_shown=[]):
-	if not erigon_warning_shown:
-		rmsg(f'WARNING: Erigon support is EXPERIMENTAL.  Use at your own risk!!!')
-		erigon_warning_shown.append(1)
+class daemon_warning(oneshot_warning):
 
-def handle_unsupported_daemon_version(rpc,proto,ignore_daemon_version,unsupported_daemon_warning_shown=[]):
+	class erigon:
+		color = 'red'
+		message = 'Erigon support is EXPERIMENTAL.  Use at your own risk!!!'
+
+	class version:
+		color = 'yellow'
+		message = 'ignoring unsupported {} daemon version at user request'
+
+def handle_unsupported_daemon_version(rpc,proto,ignore_daemon_version):
 	if ignore_daemon_version or proto.ignore_daemon_version or g.ignore_daemon_version:
-		if not type(proto) in unsupported_daemon_warning_shown:
-			ymsg(f'WARNING: ignoring unsupported {rpc.daemon.coind_name} daemon version at user request')
-			unsupported_daemon_warning_shown.append(type(proto))
+		daemon_warning('version',div=proto.name,fmt_args=[rpc.daemon.coind_name])
 	else:
 		name = rpc.daemon.coind_name
 		rdie(1,'\n'+fmt(f"""

+ 21 - 0
mmgen/util.py

@@ -733,6 +733,27 @@ def get_data_from_file(infile,desc='data',dash=False,silent=False,binary=False,q
 
 passwd_files_used = {}
 
+class oneshot_warning:
+
+	def __init__(self,wcls,div=None,fmt_args=[]):
+
+		def do_warning():
+			cls = getattr(self,wcls)
+			message = getattr(cls,'message')
+			color = globals()[getattr(cls,'color')]
+			msg(color('WARNING: ' + message.format(*fmt_args)))
+
+		flag = wcls+'_warning_shown'
+
+		if not hasattr(self,flag):
+			setattr(type(self),flag,[])
+
+		attr = getattr(type(self),flag)
+
+		if not div in attr:
+			do_warning()
+			attr.append(div)
+
 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')