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

This commit is contained in:
The MMGen Project 2022-01-27 11:08:08 +00:00
commit f4282cd214
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
9 changed files with 36 additions and 45 deletions

View file

@ -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)

View file

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

View file

@ -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(

View file

@ -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('_')}

View file

@ -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

View file

@ -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"

View file

@ -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"

View file

@ -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

View file

@ -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: