Browse Source

Altcoin module loading magic

MMGen 6 years ago
parent
commit
c9bc01cbb1
4 changed files with 20 additions and 24 deletions
  1. 1 0
      mmgen/globalvars.py
  2. 6 18
      mmgen/tw.py
  3. 4 6
      mmgen/tx.py
  4. 9 0
      mmgen/util.py

+ 1 - 0
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
 	# Constant vars - some of these might be overriden in opts.py, but they don't change thereafter
 
 
 	coin                 = 'BTC'
 	coin                 = 'BTC'
+	token                = ''
 	debug                = False
 	debug                = False
 	debug_opts           = False
 	debug_opts           = False
 	debug_rpc            = False
 	debug_rpc            = False

+ 6 - 18
mmgen/tw.py

@@ -28,6 +28,9 @@ CUR_HOME,ERASE_ALL = '\033[H','\033[0J'
 
 
 class TwUnspentOutputs(MMGenObject):
 class TwUnspentOutputs(MMGenObject):
 
 
+	def __new__(cls,*args,**kwargs):
+		return MMGenObject.__new__(altcoin_subclass(cls,'tw','TwUnspentOutputs'),*args,**kwargs)
+
 	txid_w = 64
 	txid_w = 64
 	show_txid = True
 	show_txid = True
 	can_group = 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())
 """.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):
 	def __init__(self,minconf=1):
 		self.unspent      = self.MMGenTwOutputList()
 		self.unspent      = self.MMGenTwOutputList()
 		self.fmt_display  = ''
 		self.fmt_display  = ''
@@ -350,10 +347,7 @@ watch-only wallet using '{}-addrimport' and then re-run this program.
 class TwAddrList(MMGenDict):
 class TwAddrList(MMGenDict):
 
 
 	def __new__(cls,*args,**kwargs):
 	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):
 	def __init__(self,usr_addr_list,minconf,showempty,showbtcaddrs,all_labels):
 
 
@@ -474,10 +468,7 @@ class TwAddrList(MMGenDict):
 class TrackingWallet(MMGenObject):
 class TrackingWallet(MMGenObject):
 
 
 	def __new__(cls,*args,**kwargs):
 	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):
 	def import_label(self,coinaddr,lbl):
 		# NOTE: this works because importaddress() removes the old account before
 		# 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'
 	fs = '{w:13} {u:<16} {p:<16} {c}\n'
 
 
 	def __new__(cls,*args,**kwargs):
 	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):
 	def __init__(self,minconf,quiet):
 
 

+ 4 - 6
mmgen/tx.py

@@ -219,6 +219,10 @@ txio_attrs = {
 }
 }
 
 
 class MMGenTX(MMGenObject):
 class MMGenTX(MMGenObject):
+
+	def __new__(cls,*args,**kwargs):
+		return MMGenObject.__new__(altcoin_subclass(cls,'tx','MMGenTX'),*args,**kwargs)
+
 	ext      = 'rawtx'
 	ext      = 'rawtx'
 	raw_ext  = 'rawtx'
 	raw_ext  = 'rawtx'
 	sig_ext  = 'sigtx'
 	sig_ext  = 'sigtx'
@@ -269,12 +273,6 @@ class MMGenTX(MMGenObject):
 		desc = 'transaction outputs'
 		desc = 'transaction outputs'
 		member_type = 'MMGenTxOutput'
 		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):
 	def __init__(self,filename=None,coin_sym_only=False,caller=None,silent_open=False):
 		self.inputs      = self.MMGenTxInputList()
 		self.inputs      = self.MMGenTxInputList()
 		self.outputs     = self.MMGenTxOutputList()
 		self.outputs     = self.MMGenTxOutputList()

+ 9 - 0
mmgen/util.py

@@ -891,3 +891,12 @@ def format_par(s,indent=0,width=80,as_list=False):
 			line += ('',' ')[bool(line)] + words.pop(0)
 			line += ('',' ')[bool(line)] + words.pop(0)
 		lines.append(' '*indent + line)
 		lines.append(' '*indent + line)
 	return lines if as_list else '\n'.join(lines) + '\n'
 	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