From c9bc01cbb10d3ecb0acfa739653f337302b24eec Mon Sep 17 00:00:00 2001 From: MMGen Date: Fri, 8 Jun 2018 10:20:33 +0000 Subject: [PATCH] Altcoin module loading magic --- mmgen/globalvars.py | 1 + mmgen/tw.py | 24 ++++++------------------ mmgen/tx.py | 10 ++++------ mmgen/util.py | 9 +++++++++ 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/mmgen/globalvars.py b/mmgen/globalvars.py index fed00ad7..01e12b48 100755 --- a/mmgen/globalvars.py +++ b/mmgen/globalvars.py @@ -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 diff --git a/mmgen/tw.py b/mmgen/tw.py index c0972d36..0618d4a6 100755 --- a/mmgen/tw.py +++ b/mmgen/tw.py @@ -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): diff --git a/mmgen/tx.py b/mmgen/tx.py index b49d4637..97ada6b9 100755 --- a/mmgen/tx.py +++ b/mmgen/tx.py @@ -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() diff --git a/mmgen/util.py b/mmgen/util.py index f5bdb629..82cffc99 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -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