util.py: relocate base_proto_subclass() to protocol.py

This commit is contained in:
The MMGen Project 2022-10-17 18:37:21 +00:00
commit d355fe6e58
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
10 changed files with 32 additions and 35 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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