Browse Source

tool: unify class initialization with 'need_proto' and 'need_addrtype'

The MMGen Project 3 years ago
parent
commit
f4282cd214

+ 1 - 1
mmgen/main_tool.py

@@ -342,7 +342,7 @@ if g.prog_name == 'mmgen-tool' and not opt._lock:
 
 	args,kwargs = process_args(cmd,cmd_args,cls)
 
-	ret = getattr(cls(),cmd)(*args,**kwargs)
+	ret = getattr(cls(cmdname=cmd),cmd)(*args,**kwargs)
 
 	if type(ret).__name__ == 'coroutine':
 		ret = run_session(ret)

+ 3 - 0
mmgen/tool/api.py

@@ -60,6 +60,9 @@ class tool_api(
 		wif,addr = tool.randpair()
 	"""
 
+	need_proto = True
+	need_addrtype = True
+
 	def __init__(self):
 		"""
 		Initializer - takes no arguments

+ 2 - 16
mmgen/tool/coin.py

@@ -40,22 +40,8 @@ class tool_cmd(tool_cmd_base):
 			mmgen-tool --coin=zec --type=zcash_z randpair
 	"""
 
-	def __init__(self,proto=None,mmtype=None):
-
-		if proto:
-			self.proto = proto
-		else:
-			from ..protocol import init_proto_from_opts
-			self.proto = init_proto_from_opts()
-
-		from ..opts import opt
-		self.mmtype = MMGenAddrType(
-			self.proto,
-			mmtype or opt.type or self.proto.dfl_mmtype )
-
-		from ..globalvars import g
-		if g.token:
-			self.proto.tokensym = g.token.upper()
+	need_proto = True
+	need_addrtype = True
 
 	def _init_generators(self,arg=None):
 		return generator_data(

+ 22 - 3
mmgen/tool/common.py

@@ -25,9 +25,28 @@ def options_annot_str(l):
 
 class tool_cmd_base:
 
-	def __init__(self,proto=None,mmtype=None):
-		pass
+	need_proto = False
+	need_addrtype = False
+
+	def __init__(self,cmdname=None,proto=None,mmtype=None):
+
+		if self.need_proto:
+			if proto:
+				self.proto = proto
+			else:
+				from ..protocol import init_proto_from_opts
+				self.proto = init_proto_from_opts()
+			from ..globalvars import g
+			if g.token:
+				self.proto.tokensym = g.token.upper()
+
+		if self.need_addrtype:
+			from ..opts import opt
+			from ..addr import MMGenAddrType
+			self.mmtype = MMGenAddrType(
+				self.proto,
+				mmtype or opt.type or self.proto.dfl_mmtype )
 
 	@property
 	def user_commands(self):
-		return {k:v for k,v in type(self).__dict__.items() if not k.startswith('_')}
+		return {k:v for k,v in type(self).__dict__.items() if callable(v) and not k.startswith('_')}

+ 1 - 6
mmgen/tool/file.py

@@ -25,12 +25,7 @@ from .common import tool_cmd_base,options_annot_str
 class tool_cmd(tool_cmd_base):
 	"utilities for viewing/checking MMGen address and transaction files"
 
-	def __init__(self,proto=None,mmtype=None):
-		if proto:
-			self.proto = proto
-		else:
-			from ..protocol import init_proto_from_opts
-			self.proto = init_proto_from_opts()
+	need_proto = True
 
 	def _file_chksum(self,mmgen_addrfile,objname):
 		from ..opts import opt

+ 1 - 11
mmgen/tool/rpc.py

@@ -26,17 +26,7 @@ from ..tw import TwCommon
 class tool_cmd(tool_cmd_base):
 	"tracking wallet commands using the JSON-RPC interface"
 
-	def __init__(self,proto=None,mmtype=None):
-
-		if proto:
-			self.proto = proto
-		else:
-			from ..protocol import init_proto_from_opts
-			self.proto = init_proto_from_opts()
-
-		from ..globalvars import g
-		if g.token:
-			self.proto.tokensym = g.token.upper()
+	need_proto = True
 
 	async def daemon_version(self):
 		"print coin daemon version"

+ 4 - 6
mmgen/tool/wallet.py

@@ -31,12 +31,10 @@ from ..wallet import Wallet
 class tool_cmd(tool_cmd_base):
 	"key, address or subseed generation from an MMGen wallet"
 
-	def __init__(self,proto=None,mmtype=None):
-		if proto:
-			self.proto = proto
-		else:
-			from ..protocol import init_proto_from_opts
-			self.proto = init_proto_from_opts()
+	def __init__(self,cmdname=None,proto=None,mmtype=None):
+		if cmdname in ('gen_key','gen_addr'):
+			self.need_proto = True
+		super().__init__(cmdname=cmdname,proto=proto,mmtype=mmtype)
 
 	def get_subseed(self,subseed_idx:str,wallet=''):
 		"get the Seed ID of a single subseed by Subseed Index for default or specified wallet"

+ 1 - 1
test/test_py_d/ts_ethdev.py

@@ -839,7 +839,7 @@ class TestSuiteEthdev(TestSuiteBase,TestSuiteShared):
 		sid = dfl_sid
 		from mmgen.tool.wallet import tool_cmd
 		usr_mmaddrs = [f'{sid}:E:{i}' for i in (11,21)]
-		usr_addrs = [tool_cmd(proto=self.proto).gen_addr(addr,dfl_words_file) for addr in usr_mmaddrs]
+		usr_addrs = [tool_cmd(cmdname='gen_addr',proto=self.proto).gen_addr(addr,dfl_words_file) for addr in usr_mmaddrs]
 
 		from mmgen.altcoins.eth.contract import TokenResolve
 		from mmgen.altcoins.eth.tx import EthereumMMGenTX as etx

+ 1 - 1
test/tooltest2.py

@@ -896,7 +896,7 @@ async def run_test(cls,gid,cmd_name):
 			if stdin_input and g.platform == 'win':
 				msg('Skipping for MSWin - no os.fork()')
 				continue
-			method = getattr(cls(proto=proto,mmtype=mmtype),cmd_name)
+			method = getattr(cls(cmdname=cmd_name,proto=proto,mmtype=mmtype),cmd_name)
 			cmd_out = await call_method(cls,method,cmd_name,args,out,opts,mmtype,stdin_input)
 
 		try: