protocol.py: make g a class instance; minor fixes, cleanups

This commit is contained in:
The MMGen Project 2020-05-15 11:49:51 +00:00
commit 819dd700b0
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
2 changed files with 26 additions and 24 deletions

View file

@ -25,27 +25,20 @@ from decimal import Decimal
from collections import namedtuple
from .devtools import *
# Global vars are set to dfl values in class g.
# They're overridden in this order:
# 1 - config file
# 2 - environmental vars
# 3 - command line
class g(object):
def die(ev=0,s=''):
if s: sys.stderr.write(s+'\n')
sys.exit(ev)
for k in ('linux','win','msys'):
if sys.platform[:len(k)] == k:
platform = { 'linux':'linux', 'win':'win', 'msys':'win' }[k]
break
else:
die(1,"'{}': platform not supported by {}\n".format(sys.platform,proj_name))
def die(exit_val,s=''):
if s:
sys.stderr.write(s+'\n')
sys.exit(exit_val)
class GlobalContext:
"""
Set global vars to default values
Globals are overridden in this order:
1 - config file
2 - environmental vars
3 - command line
"""
# Constants:
version = '0.12.099'
release_date = 'May 2020'
@ -125,13 +118,19 @@ class g(object):
terminal_width = 0
mnemonic_entry_modes = {}
color = sys.stdout.isatty()
if os.getenv('HOME'): # Linux or MSYS
for k in ('linux','win','msys'):
if sys.platform[:len(k)] == k:
platform = { 'linux':'linux', 'win':'win', 'msys':'win' }[k]
break
else:
die(1,f'{sys.platform!r}: platform not supported by {proj_name}')
if os.getenv('HOME'): # Linux or MSYS2
home_dir = os.getenv('HOME')
elif platform == 'win': # non-MSYS Windows - not supported
die(1,'$HOME not set! {} for Windows must be run in MSYS environment'.format(proj_name))
elif platform == 'win': # Windows without MSYS2 - not supported
die(1,f'$HOME not set! {proj_name} for Windows must be run in MSYS2 environment')
else:
die(2,'$HOME is not set! Unable to determine home directory')
@ -282,3 +281,5 @@ class g(object):
for name in env_opts:
if name[:11] == 'MMGEN_DEBUG':
os.environ[name] = '1'
g = GlobalContext()

View file

@ -541,6 +541,7 @@ class CoinAddr(str,Hilite,InitErrors,MMGenObject):
if type(g.proto).__name__.startswith('Ethereum'):
return True
from mmgen.protocol import CoinProtocol
proto = CoinProtocol.get_protocol_by_chain(chain)
if self.addr_fmt == 'bech32':