From d355fe6e585d335201cbff5da27b508361378891 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 17 Oct 2022 18:37:21 +0000 Subject: [PATCH] util.py: relocate base_proto_subclass() to protocol.py --- mmgen/addrdata.py | 4 ++-- mmgen/main_addrimport.py | 2 +- mmgen/protocol.py | 21 +++++++++++++++++++++ mmgen/tw/addrs.py | 4 ++-- mmgen/tw/bal.py | 3 +-- mmgen/tw/ctl.py | 3 +-- mmgen/tw/json.py | 3 +-- mmgen/tw/txhistory.py | 4 ++-- mmgen/tw/unspent.py | 3 +-- mmgen/util.py | 20 -------------------- 10 files changed, 32 insertions(+), 35 deletions(-) diff --git a/mmgen/addrdata.py b/mmgen/addrdata.py index 326a8fc2..e85dc74a 100755 --- a/mmgen/addrdata.py +++ b/mmgen/addrdata.py @@ -20,7 +20,7 @@ addrdata.py: MMGen AddrData and related classes """ -from .util import vmsg,base_proto_subclass,fmt,die +from .util import vmsg,fmt,die from .base_obj import AsyncInit from .obj import MMGenObject,MMGenDict,get_obj from .addr import MMGenID,AddrListID @@ -69,7 +69,7 @@ class AddrData(MMGenObject): class TwAddrData(AddrData,metaclass=AsyncInit): def __new__(cls,proto,*args,**kwargs): - return MMGenObject.__new__(base_proto_subclass(cls,proto,None,'addrdata')) + return MMGenObject.__new__(proto.base_proto_subclass(cls,None,'addrdata')) async def __init__(self,proto,wallet=None): from .rpc import rpc_init diff --git a/mmgen/main_addrimport.py b/mmgen/main_addrimport.py index ad91be72..2569fce2 100755 --- a/mmgen/main_addrimport.py +++ b/mmgen/main_addrimport.py @@ -134,7 +134,7 @@ def check_opts(tw): async def main(): from .tw.ctl import TrackingWallet if opt.token_addr: - proto.tokensym = 'foo' # hack to trigger 'Token' in base_proto_subclass() + proto.tokensym = 'foo' # hack to trigger 'Token' in proto.base_proto_subclass() tw = await TrackingWallet( proto = proto, diff --git a/mmgen/protocol.py b/mmgen/protocol.py index 4aa6774d..c6d5c109 100755 --- a/mmgen/protocol.py +++ b/mmgen/protocol.py @@ -166,6 +166,27 @@ class CoinProtocol(MMGenObject): def viewkey(self,viewkey_str): raise NotImplementedError(f'{self.name} protocol does not support view keys') + def base_proto_subclass(self,cls,subdir,modname,sub_clsname=None): + """ + magic module loading and class selection + """ + modpath = 'mmgen.proto.{}.{}{}'.format( + self.base_proto_coin.lower(), + subdir + '.' if subdir else '', + modname ) + + clsname = ( + self.mod_clsname + + ('Token' if self.tokensym else '') + + cls.__name__ ) + + import importlib + if sub_clsname: + return getattr(getattr(importlib.import_module(modpath),clsname),sub_clsname) + else: + return getattr(importlib.import_module(modpath),clsname) + + class Secp256k1(Base): """ Bitcoin and Ethereum protocols inherit from this class diff --git a/mmgen/tw/addrs.py b/mmgen/tw/addrs.py index b6d2be21..a4062406 100755 --- a/mmgen/tw/addrs.py +++ b/mmgen/tw/addrs.py @@ -21,7 +21,7 @@ twaddrs: Tracking wallet listaddresses class for the MMGen suite """ from ..color import green -from ..util import msg,die,base_proto_subclass +from ..util import msg,die from ..base_obj import AsyncInit from ..obj import MMGenDict,TwComment from ..addr import CoinAddr,MMGenID @@ -30,7 +30,7 @@ from .common import TwCommon class TwAddrList(MMGenDict,TwCommon,metaclass=AsyncInit): def __new__(cls,proto,*args,**kwargs): - return MMGenDict.__new__(base_proto_subclass(cls,proto,'tw','addrs'),*args,**kwargs) + return MMGenDict.__new__(proto.base_proto_subclass(cls,'tw','addrs'),*args,**kwargs) def raw_list(self): return [((k if k.type == 'mmgen' else 'Non-MMGen'),self[k]['addr'],self[k]['amt']) for k in self] diff --git a/mmgen/tw/bal.py b/mmgen/tw/bal.py index e2261734..9e831c50 100755 --- a/mmgen/tw/bal.py +++ b/mmgen/tw/bal.py @@ -21,7 +21,6 @@ twbal: Tracking wallet getbalance class for the MMGen suite """ from ..color import red,green -from ..util import base_proto_subclass from ..base_obj import AsyncInit from ..objmethods import MMGenObject from ..rpc import rpc_init @@ -29,7 +28,7 @@ from ..rpc import rpc_init class TwGetBalance(MMGenObject,metaclass=AsyncInit): def __new__(cls,proto,*args,**kwargs): - return MMGenObject.__new__(base_proto_subclass(cls,proto,'tw','bal')) + return MMGenObject.__new__(proto.base_proto_subclass(cls,'tw','bal')) async def __init__(self,proto,minconf,quiet): diff --git a/mmgen/tw/ctl.py b/mmgen/tw/ctl.py index 39f36e50..abc1fa51 100755 --- a/mmgen/tw/ctl.py +++ b/mmgen/tw/ctl.py @@ -31,7 +31,6 @@ from ..util import ( dmsg, suf, write_mode, - base_proto_subclass, die ) from ..base_obj import AsyncInit from ..objmethods import MMGenObject @@ -49,7 +48,7 @@ class TrackingWallet(MMGenObject,metaclass=AsyncInit): importing = False def __new__(cls,proto,*args,**kwargs): - return MMGenObject.__new__(base_proto_subclass(cls,proto,'tw','ctl')) + return MMGenObject.__new__(proto.base_proto_subclass(cls,'tw','ctl')) async def __init__(self,proto,mode='r',token_addr=None,rpc_ignore_wallet=False): diff --git a/mmgen/tw/json.py b/mmgen/tw/json.py index de568547..32c139a1 100755 --- a/mmgen/tw/json.py +++ b/mmgen/tw/json.py @@ -19,7 +19,6 @@ from ..util import ( msg, ymsg, fmt, - base_proto_subclass, die, make_timestamp, make_chksum_8, @@ -37,7 +36,7 @@ class TwJSON: fn_pfx = 'mmgen-tracking-wallet-dump' def __new__(cls,proto,*args,**kwargs): - return MMGenObject.__new__(base_proto_subclass(TwJSON,proto,'tw','json',cls.__name__)) + return MMGenObject.__new__(proto.base_proto_subclass(TwJSON,'tw','json',cls.__name__)) def __init__(self,proto): self.proto = proto diff --git a/mmgen/tw/txhistory.py b/mmgen/tw/txhistory.py index 58247724..68b4e9cb 100755 --- a/mmgen/tw/txhistory.py +++ b/mmgen/tw/txhistory.py @@ -14,7 +14,7 @@ tw.txhistory: Tracking wallet transaction history class for the MMGen suite from collections import namedtuple -from ..util import base_proto_subclass,fmt +from ..util import fmt from ..base_obj import AsyncInit from ..objmethods import MMGenObject from ..obj import CoinTxID,MMGenList,Int @@ -24,7 +24,7 @@ from .common import TwCommon class TwTxHistory(MMGenObject,TwCommon,metaclass=AsyncInit): def __new__(cls,proto,*args,**kwargs): - return MMGenObject.__new__(base_proto_subclass(cls,proto,'tw','txhistory')) + return MMGenObject.__new__(proto.base_proto_subclass(cls,'tw','txhistory')) txid_w = 64 show_txid = False diff --git a/mmgen/tw/unspent.py b/mmgen/tw/unspent.py index 1935b8e0..7a35c666 100755 --- a/mmgen/tw/unspent.py +++ b/mmgen/tw/unspent.py @@ -33,7 +33,6 @@ from ..util import ( fmt, keypress_confirm, line_input, - base_proto_subclass ) from ..base_obj import AsyncInit from ..objmethods import MMGenObject @@ -45,7 +44,7 @@ from .common import TwCommon,TwMMGenID,get_tw_label class TwUnspentOutputs(MMGenObject,TwCommon,metaclass=AsyncInit): def __new__(cls,proto,*args,**kwargs): - return MMGenObject.__new__(base_proto_subclass(cls,proto,'tw','unspent')) + return MMGenObject.__new__(proto.base_proto_subclass(cls,'tw','unspent')) txid_w = 64 print_hdr_fs = '{a} (block #{b}, {c} UTC)\n{d}Sort order: {e}\n{f}\n\nTotal {g}: {h}\n' diff --git a/mmgen/util.py b/mmgen/util.py index 79bece1c..1027ada5 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -672,26 +672,6 @@ def get_subclasses(cls,names=False): yield j return tuple((c.__name__ for c in gen(cls)) if names else gen(cls)) -def base_proto_subclass(cls,proto,subdir,modname,sub_clsname=None): - """ - magic module loading and class selection - """ - modpath = 'mmgen.proto.{}.{}{}'.format( - proto.base_proto_coin.lower(), - subdir + '.' if subdir else '', - modname ) - - clsname = ( - proto.mod_clsname - + ('Token' if proto.tokensym else '') - + cls.__name__ ) - - import importlib - if sub_clsname: - return getattr(getattr(importlib.import_module(modpath),clsname),sub_clsname) - else: - return getattr(importlib.import_module(modpath),clsname) - # decorator for TrackingWallet def write_mode(orig_func): def f(self,*args,**kwargs):