py3port: fix altcoin_subclass module loading magic

This commit is contained in:
The MMGen Project 2018-10-31 15:52:05 +00:00
commit b41d6ae3eb
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
5 changed files with 21 additions and 14 deletions

View file

@ -877,7 +877,7 @@ re-import your addresses.
}
def __new__(cls,*args,**kwargs):
return MMGenObject.__new__(altcoin_subclass(cls,'tw','AddrData'),*args,**kwargs)
return MMGenObject.__new__(altcoin_subclass(cls,'tw','AddrData'))
def __init__(self,source=None):
self.al_ids = {}

View file

@ -479,7 +479,7 @@ def init_genonly_altcoins(usr_coin,trust_level=None):
data = {}
for k in ('mainnet','testnet'):
data[k] = [e for e in ci.coin_constants[k] if e[6] >= trust_level]
exec(make_init_genonly_altcoins_str(data))
exec(make_init_genonly_altcoins_str(data),globals(),globals())
return trust_level
def make_init_genonly_altcoins_str(data):
@ -490,16 +490,18 @@ def make_init_genonly_altcoins_str(data):
if proto[0] in '0123456789': proto = 'X_'+proto
if proto in globals(): return ''
if coin.lower() in CoinProtocol.coins: return ''
def num2hexstr(n):
return '{:0{}x}'.format(n,2 if n < 256 else 4)
def num2hexbytes(n):
return "b'{:0{}x}'".format(n,(4,2)[n < 256])
o = ['class {}(Bitcoin{}ProtocolAddrgen):'.format(proto,tn_str)]
o += ["base_coin = '{}'".format(coin)]
o += ["name = '{}'".format(e[0].lower())]
o += ["nameCaps = '{}'".format(e[0])]
a = "addr_ver_num = {{ 'p2pkh': ({!r},{!r})".format(num2hexstr(e[3][0]),e[3][1])
b = ", 'p2sh': ({!r},{!r})".format(num2hexstr(e[4][0]),e[4][1]) if e[4] else ''
a = "addr_ver_num = {{ 'p2pkh': ({},{!r})".format(num2hexbytes(e[3][0]),e[3][1])
b = ", 'p2sh': ({},{!r})".format(num2hexbytes(e[4][0]),e[4][1]) if e[4] else ''
o += [a+b+' }']
o += ["wif_ver_num = {{ 'std': {!r} }}".format(num2hexstr(e[2]))]
o += ["wif_ver_num = {{ 'std': {} }}".format(num2hexbytes(e[2]))]
o += ["mmtypes = ('L','C'{})".format(",'S'" if e[5] else '')]
o += ["dfl_mmtype = '{}'".format('L')]
return '\n\t'.join(o) + '\n'

View file

@ -30,7 +30,7 @@ def CUR_RIGHT(n): return '\033[{}C'.format(n)
class TwUnspentOutputs(MMGenObject):
def __new__(cls,*args,**kwargs):
return MMGenObject.__new__(altcoin_subclass(cls,'tw','TwUnspentOutputs'),*args,**kwargs)
return MMGenObject.__new__(altcoin_subclass(cls,'tw','TwUnspentOutputs'))
txid_w = 64
disp_type = 'btc'
@ -508,7 +508,7 @@ class TwAddrList(MMGenDict):
class TrackingWallet(MMGenObject):
def __new__(cls,*args,**kwargs):
return MMGenObject.__new__(altcoin_subclass(cls,'tw','TrackingWallet'),*args,**kwargs)
return MMGenObject.__new__(altcoin_subclass(cls,'tw','TrackingWallet'))
mode = 'r'
caps = ('rescan','batch')
@ -609,7 +609,7 @@ class TwGetBalance(MMGenObject):
fs = '{w:13} {u:<16} {p:<16} {c}\n'
def __new__(cls,*args,**kwargs):
return MMGenObject.__new__(altcoin_subclass(cls,'tw','TwGetBalance'),*args,**kwargs)
return MMGenObject.__new__(altcoin_subclass(cls,'tw','TwGetBalance'))
def __init__(self,minconf,quiet):

View file

@ -205,7 +205,7 @@ txio_attrs = {
class MMGenTX(MMGenObject):
def __new__(cls,*args,**kwargs):
return MMGenObject.__new__(altcoin_subclass(cls,'tx','MMGenTX'),*args,**kwargs)
return MMGenObject.__new__(altcoin_subclass(cls,'tx','MMGenTX'))
ext = 'rawtx'
raw_ext = 'rawtx'

View file

@ -930,9 +930,14 @@ def altcoin_subclass(cls,mod_id,cls_name):
pname = g.proto.class_pfx if hasattr(g.proto,'class_pfx') else capfirst(g.proto.name)
tname = 'Token' if g.token else ''
e1 = 'from mmgen.altcoins.{}.{} import {}{}{}'.format(mod_dir,mod_id,pname,tname,cls_name)
e2 = 'cls = {}{}{}'.format(pname,tname,cls_name)
try: exec(e1); exec(e2); return cls
except ImportError: return cls
e2 = 'alt_cls = {}{}{}'.format(pname,tname,cls_name)
gl = globals()
try:
exec(e1,gl,gl)
exec(e2,gl,gl)
return alt_cls
except ImportError:
return cls
# decorator for TrackingWallet
def write_mode(orig_func):