Altcoin module loading magic

This commit is contained in:
The MMGen Project 2018-06-08 10:20:33 +00:00
commit c9bc01cbb1
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
4 changed files with 20 additions and 24 deletions

View file

@ -65,6 +65,7 @@ class g(object):
# Constant vars - some of these might be overriden in opts.py, but they don't change thereafter
coin = 'BTC'
token = ''
debug = False
debug_opts = False
debug_rpc = False

View file

@ -28,6 +28,9 @@ CUR_HOME,ERASE_ALL = '\033[H','\033[0J'
class TwUnspentOutputs(MMGenObject):
def __new__(cls,*args,**kwargs):
return MMGenObject.__new__(altcoin_subclass(cls,'tw','TwUnspentOutputs'),*args,**kwargs)
txid_w = 64
show_txid = True
can_group = True
@ -61,12 +64,6 @@ watch-only wallet using '{}-addrimport' and then re-run this program.
""".strip().format(g.proj_name.lower())
}
def __new__(cls,*args,**kwargs):
if g.coin == 'ETH':
from mmgen.altcoins.eth.tw import EthereumTwUnspentOutputs
cls = EthereumTwUnspentOutputs
return MMGenObject.__new__(cls,*args,**kwargs)
def __init__(self,minconf=1):
self.unspent = self.MMGenTwOutputList()
self.fmt_display = ''
@ -350,10 +347,7 @@ watch-only wallet using '{}-addrimport' and then re-run this program.
class TwAddrList(MMGenDict):
def __new__(cls,*args,**kwargs):
if g.coin == 'ETH':
from mmgen.altcoins.eth.tw import EthereumTwAddrList
cls = EthereumTwAddrList
return MMGenDict.__new__(cls,*args,**kwargs)
return MMGenDict.__new__(altcoin_subclass(cls,'tw','TwAddrList'),*args,**kwargs)
def __init__(self,usr_addr_list,minconf,showempty,showbtcaddrs,all_labels):
@ -474,10 +468,7 @@ class TwAddrList(MMGenDict):
class TrackingWallet(MMGenObject):
def __new__(cls,*args,**kwargs):
if g.coin == 'ETH':
from mmgen.altcoins.eth.tw import EthereumTrackingWallet
cls = EthereumTrackingWallet
return MMGenObject.__new__(cls,*args,**kwargs)
return MMGenObject.__new__(altcoin_subclass(cls,'tw','TrackingWallet'),*args,**kwargs)
def import_label(self,coinaddr,lbl):
# NOTE: this works because importaddress() removes the old account before
@ -546,10 +537,7 @@ class TwGetBalance(MMGenObject):
fs = '{w:13} {u:<16} {p:<16} {c}\n'
def __new__(cls,*args,**kwargs):
if g.coin == 'ETH':
from mmgen.altcoins.eth.tw import EthereumTwGetBalance
cls = EthereumTwGetBalance
return MMGenObject.__new__(cls,*args,**kwargs)
return MMGenObject.__new__(altcoin_subclass(cls,'tw','TwGetBalance'),*args,**kwargs)
def __init__(self,minconf,quiet):

View file

@ -219,6 +219,10 @@ txio_attrs = {
}
class MMGenTX(MMGenObject):
def __new__(cls,*args,**kwargs):
return MMGenObject.__new__(altcoin_subclass(cls,'tx','MMGenTX'),*args,**kwargs)
ext = 'rawtx'
raw_ext = 'rawtx'
sig_ext = 'sigtx'
@ -269,12 +273,6 @@ class MMGenTX(MMGenObject):
desc = 'transaction outputs'
member_type = 'MMGenTxOutput'
def __new__(cls,*args,**kwargs):
if g.coin == 'ETH':
from mmgen.altcoins.eth.tx import EthereumMMGenTX
cls = EthereumMMGenTX
return MMGenObject.__new__(cls,*args,**kwargs)
def __init__(self,filename=None,coin_sym_only=False,caller=None,silent_open=False):
self.inputs = self.MMGenTxInputList()
self.outputs = self.MMGenTxOutputList()

View file

@ -891,3 +891,12 @@ def format_par(s,indent=0,width=80,as_list=False):
line += ('',' ')[bool(line)] + words.pop(0)
lines.append(' '*indent + line)
return lines if as_list else '\n'.join(lines) + '\n'
def altcoin_subclass(cls,mod_id,cls_name):
if cls.__name__ != cls_name: return cls
pn = capfirst(g.proto.name)
tn = 'Token' if g.token else ''
e1 = 'from mmgen.altcoins.{}.{} import {}{}{}'.format(g.coin.lower(),mod_id,pn,tn,cls_name)
e2 = 'cls = {}{}{}'.format(pn,tn,cls_name)
try: exec e1; exec e2; return cls
except ImportError: return cls