From 481bc13e52a6cf3dc2e285b1ac0254e85f2990fa Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Thu, 20 Jan 2022 12:04:52 +0000 Subject: [PATCH] UserOpts: set _default_to_none to True --- mmgen/addr.py | 2 +- mmgen/addrfile.py | 2 +- mmgen/altcoins/eth/tx.py | 4 ++-- mmgen/globalvars.py | 1 + mmgen/opts.py | 1 + mmgen/protocol.py | 2 +- mmgen/seed.py | 2 +- mmgen/tool.py | 6 ++++-- mmgen/tx.py | 2 +- mmgen/util.py | 1 + mmgen/wallet.py | 2 +- test/include/common.py | 14 +++++++------- 12 files changed, 22 insertions(+), 17 deletions(-) diff --git a/mmgen/addr.py b/mmgen/addr.py index 00d90528..2d4d5579 100755 --- a/mmgen/addr.py +++ b/mmgen/addr.py @@ -190,7 +190,7 @@ def KeyGenerator(proto,pubkey_type,backend=None,silent=False): pubkey_type_cls = getattr(keygen_backend,pubkey_type) from .opts import opt - backend = backend or getattr(opt,'keygen_backend',None) + backend = backend or opt.keygen_backend if backend: _check_backend(backend,pubkey_type) diff --git a/mmgen/addrfile.py b/mmgen/addrfile.py index 7410be7b..d5a8770d 100755 --- a/mmgen/addrfile.py +++ b/mmgen/addrfile.py @@ -167,7 +167,7 @@ class AddrFile(MMGenObject): if p.has_keys and not p.skip_ka_check: from .opts import opt - if getattr(opt,'yes',False) or keypress_confirm('Check key-to-address validity?'): + if opt.yes or keypress_confirm('Check key-to-address validity?'): from .addr import KeyGenerator,AddrGenerator kg = KeyGenerator(p.proto,p.al_id.mmtype.pubkey_type) ag = AddrGenerator(p.proto,p.al_id.mmtype) diff --git a/mmgen/altcoins/eth/tx.py b/mmgen/altcoins/eth/tx.py index 7dc4e906..3f3365e1 100755 --- a/mmgen/altcoins/eth/tx.py +++ b/mmgen/altcoins/eth/tx.py @@ -94,9 +94,9 @@ class EthereumMMGenTX: def __init__(self,*args,**kwargs): MMGenTX.New.__init__(self,*args,**kwargs) - if getattr(opt,'tx_gas',None): + if opt.tx_gas: self.tx_gas = self.start_gas = ETHAmt(int(opt.tx_gas),'wei') - if getattr(opt,'contract_data',None): + if opt.contract_data: m = "'--contract-data' option may not be used with token transaction" assert not 'Token' in type(self).__name__, m with open(opt.contract_data) as fp: diff --git a/mmgen/globalvars.py b/mmgen/globalvars.py index 680eedb2..9dc9b812 100755 --- a/mmgen/globalvars.py +++ b/mmgen/globalvars.py @@ -250,6 +250,7 @@ class GlobalContext(Lockable): auto_typeset_opts = { 'seed_len': int, 'subseeds': int, + 'vsize_adj': float, } min_screen_width = 80 diff --git a/mmgen/opts.py b/mmgen/opts.py index 9322884b..a420f193 100755 --- a/mmgen/opts.py +++ b/mmgen/opts.py @@ -28,6 +28,7 @@ import mmgen.share.Opts class UserOpts(Lockable): _autolock = False + _default_to_none = True _set_ok = ('usr_randchars',) _reset_ok = ('quiet','verbose','yes') diff --git a/mmgen/protocol.py b/mmgen/protocol.py index 2e4bbf5b..b3dbb3a6 100755 --- a/mmgen/protocol.py +++ b/mmgen/protocol.py @@ -458,7 +458,7 @@ class CoinProtocol(MMGenObject): def __init__(self,*args,**kwargs): super().__init__(*args,**kwargs) from .opts import opt - self.coin_id = 'ZEC-Z' if getattr(opt,'type',None) in ('zcash_z','Z') else 'ZEC-T' + self.coin_id = 'ZEC-Z' if opt.type in ('zcash_z','Z') else 'ZEC-T' def get_addr_len(self,addr_fmt): return (20,64)[addr_fmt in ('zcash_z','viewkey')] diff --git a/mmgen/seed.py b/mmgen/seed.py index 513a9cf9..5c6367b0 100755 --- a/mmgen/seed.py +++ b/mmgen/seed.py @@ -95,7 +95,7 @@ class Seed(SeedBase): from .opts import opt self._subseeds = SubSeedList( self, - length = self.nSubseeds or getattr(opt,'subseeds',None) ) + length = self.nSubseeds or opt.subseeds ) return self._subseeds def subseed(self,*args,**kwargs): diff --git a/mmgen/tool.py b/mmgen/tool.py index d8062895..35471094 100755 --- a/mmgen/tool.py +++ b/mmgen/tool.py @@ -280,7 +280,9 @@ class MMGenToolCmds(metaclass=MMGenToolCmdMeta): def __init__(self,proto=None,mmtype=None): from .protocol import init_proto_from_opts self.proto = proto or init_proto_from_opts() - self.mmtype = MMGenAddrType(self.proto,(mmtype or getattr(opt,'type',None) or self.proto.dfl_mmtype)) + self.mmtype = MMGenAddrType( + self.proto, + mmtype or opt.type or self.proto.dfl_mmtype ) if g.token: self.proto.tokensym = g.token.upper() @@ -1050,7 +1052,7 @@ class tool_api( """ import mmgen.opts opts.UserOpts._reset_ok += ('usr_randchars',) - if not hasattr(opt,'version'): + if not opt._lock: opts.init() super().__init__() diff --git a/mmgen/tx.py b/mmgen/tx.py index c96ed921..f8114e9e 100755 --- a/mmgen/tx.py +++ b/mmgen/tx.py @@ -465,7 +465,7 @@ class MMGenTX: dmsg(f' inputs size: {isize}, outputs size: {osize}, witness size: {wsize}') dmsg(f' size: {new_size}, vsize: {ret}, old_size: {old_size}') - return int(ret * float(opt.vsize_adj)) if hasattr(opt,'vsize_adj') and opt.vsize_adj else ret + return int(ret * (opt.vsize_adj or 1)) # convert absolute BTC fee to satoshis-per-byte using estimated size def fee_abs2rel(self,abs_fee,to_unit=None): diff --git a/mmgen/util.py b/mmgen/util.py index 8f25e321..50d9cfab 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -178,6 +178,7 @@ def warn_altcoins(coinsym,trust_level): def get_keccak(): from .opts import opt + # called in opts.init() via CoinProtocol, so must use getattr(): if getattr(opt,'use_internal_keccak_module',False): from .keccak import keccak_256 qmsg('Using internal keccak module by user request') diff --git a/mmgen/wallet.py b/mmgen/wallet.py index 3d4572a1..1e90adfe 100755 --- a/mmgen/wallet.py +++ b/mmgen/wallet.py @@ -78,7 +78,7 @@ class Wallet(MMGenObject,metaclass=WalletMeta): in_fmt = in_fmt or opt.in_fmt - if hasattr(opt,'out_fmt') and opt.out_fmt: + if opt.out_fmt: out_cls = cls.fmt_code_to_type(opt.out_fmt) if not out_cls: die(1,f'{opt.out_fmt!r}: unrecognized output format') diff --git a/test/include/common.py b/test/include/common.py index 9cb95247..cc55d1e8 100755 --- a/test/include/common.py +++ b/test/include/common.py @@ -161,12 +161,12 @@ def init_coverage(): return coverdir,acc_file def silence(): - if not (opt.verbose or getattr(opt,'exact_output',None)): + if not (opt.verbose or opt.exact_output): devnull_fn = ('/dev/null','null.out')[g.platform == 'win'] g.stdout = g.stderr = open(devnull_fn,'w') def end_silence(): - if not (opt.verbose or getattr(opt,'exact_output',None)): + if not (opt.verbose or opt.exact_output): g.stdout.close() g.stdout = sys.stdout g.stderr = sys.stderr @@ -178,10 +178,10 @@ def omsg_r(s): sys.stderr.flush() def imsg(s): - if opt.verbose or getattr(opt,'exact_output',None): + if opt.verbose or opt.exact_output: omsg(s) def imsg_r(s): - if opt.verbose or getattr(opt,'exact_output',None): + if opt.verbose or opt.exact_output: omsg_r(s) def iqmsg(s): @@ -192,10 +192,10 @@ def iqmsg_r(s): omsg_r(s) def oqmsg(s): - if not (opt.verbose or getattr(opt,'exact_output',None)): + if not (opt.verbose or opt.exact_output): omsg(s) def oqmsg_r(s): - if not (opt.verbose or getattr(opt,'exact_output',None)): + if not (opt.verbose or opt.exact_output): omsg_r(s) def end_msg(t): @@ -220,7 +220,7 @@ def restart_test_daemons(*network_ids,remove_datadir=False): def test_daemons_ops(*network_ids,op,remove_datadir=False): if not opt.no_daemon_autostart: from mmgen.daemon import CoinDaemon - silent = not opt.verbose and not getattr(opt,'exact_output',False) + silent = not (opt.verbose or opt.exact_output) ret = False for network_id in network_ids: d = CoinDaemon(network_id,test_suite=True,daemon_id=g.daemon_id)