From 819dd700b06c424da47a91e878d4381c01e62419 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 15 May 2020 11:49:51 +0000 Subject: [PATCH] protocol.py: make g a class instance; minor fixes, cleanups --- mmgen/globalvars.py | 47 +++++++++++++++++++++++---------------------- mmgen/obj.py | 1 + 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/mmgen/globalvars.py b/mmgen/globalvars.py index 42023e3c..c1a2ada2 100755 --- a/mmgen/globalvars.py +++ b/mmgen/globalvars.py @@ -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() diff --git a/mmgen/obj.py b/mmgen/obj.py index 4d91bb11..d0270476 100755 --- a/mmgen/obj.py +++ b/mmgen/obj.py @@ -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':