Add dumpconfig command flag

This commit is contained in:
nolash 2021-09-13 08:47:00 +02:00
parent b88758e48e
commit a3ddc31bfe
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 29 additions and 5 deletions

View File

@ -97,6 +97,9 @@ class ArgumentParser(argparse.ArgumentParser):
self.add_argument(arg[0], nargs='?', type=arg[1], help=arg[2]) self.add_argument(arg[0], nargs='?', type=arg[1], help=arg[2])
args = super(ArgumentParser, self).parse_args(args=argv) args = super(ArgumentParser, self).parse_args(args=argv)
if args.dumpconfig:
return args
if len(self.pos_args) == 1: if len(self.pos_args) == 1:
arg = self.pos_args[0] arg = self.pos_args[0]
argname = arg[0] argname = arg[0]
@ -131,6 +134,7 @@ class ArgumentParser(argparse.ArgumentParser):
if arg_flags & Flag.CONFIG: if arg_flags & Flag.CONFIG:
self.add_argument('-c', '--config', type=str, default=env.get('CONFINI_DIR'), help='Configuration directory') self.add_argument('-c', '--config', type=str, default=env.get('CONFINI_DIR'), help='Configuration directory')
self.add_argument('-n', '--namespace', type=str, help='Configuration namespace') self.add_argument('-n', '--namespace', type=str, help='Configuration namespace')
self.add_argument('--dumpconfig', action='store_true', help='Output configuration and quit. Use with --raw to omit values and output schema only.')
if arg_flags & Flag.WAIT: if arg_flags & Flag.WAIT:
self.add_argument('-w', action='store_true', help='Wait for the last transaction to be confirmed') self.add_argument('-w', action='store_true', help='Wait for the last transaction to be confirmed')
self.add_argument('-ww', action='store_true', help='Wait for every transaction to be confirmed') self.add_argument('-ww', action='store_true', help='Wait for every transaction to be confirmed')

View File

@ -1,6 +1,7 @@
# standard imports # standard imports
import logging import logging
import os import os
import sys
# external imports # external imports
import confini import confini
@ -35,7 +36,7 @@ class Config(confini.Config):
default_fee_limit = 0 default_fee_limit = 0
@classmethod @classmethod
def from_args(cls, args, arg_flags=0x0f, env=os.environ, extra_args={}, base_config_dir=None, default_config_dir=None, user_config_dir=None, default_fee_limit=None, logger=None, load_callback=logcallback): def from_args(cls, args, arg_flags=0x0f, env=os.environ, extra_args={}, base_config_dir=None, default_config_dir=None, user_config_dir=None, default_fee_limit=None, logger=None, load_callback=logcallback, dump_writer=sys.stdout):
"""Parses arguments in argparse.ArgumentParser instance, then match and override configuration values that match them. """Parses arguments in argparse.ArgumentParser instance, then match and override configuration values that match them.
The method processes all known argument flags from chainlib.cli.Flag passed in the "args" argument. The method processes all known argument flags from chainlib.cli.Flag passed in the "args" argument.
@ -155,6 +156,9 @@ class Config(confini.Config):
config = confini.Config(config_dir, env_prefix=env_prefix, override_dirs=override_config_dirs) config = confini.Config(config_dir, env_prefix=env_prefix, override_dirs=override_config_dirs)
config.process() config.process()
config.add(getattr(args, 'raw'), '_RAW')
args_override = {} args_override = {}
if arg_flags & Flag.PROVIDER: if arg_flags & Flag.PROVIDER:
@ -205,8 +209,6 @@ class Config(confini.Config):
if arg_flags & Flag.EXEC: if arg_flags & Flag.EXEC:
config.add(getattr(args, 'executable_address'), '_EXEC_ADDRESS') config.add(getattr(args, 'executable_address'), '_EXEC_ADDRESS')
config.add(getattr(args, 'raw'), '_RAW')
if arg_flags & Flag.CONFIG: if arg_flags & Flag.CONFIG:
config.add(getattr(args, 'namespace'), 'CONFIG_USER_NAMESPACE') config.add(getattr(args, 'namespace'), 'CONFIG_USER_NAMESPACE')
@ -227,6 +229,21 @@ class Config(confini.Config):
if existing_r == None or r != None: if existing_r == None or r != None:
config.add(r, v, exists_ok=True) config.add(r, v, exists_ok=True)
if getattr(args, 'dumpconfig'):
config_keys = config.all()
with_values = not config.get('_RAW')
for k in config_keys:
if k[0] == '_':
continue
s = k + '='
if with_values:
v = config.get(k)
if v != None:
s += str(v)
s += '\n'
dump_writer.write(s)
sys.exit(0)
if load_callback != None: if load_callback != None:
load_callback(config) load_callback(config)

View File

@ -47,6 +47,9 @@ class Rpc:
:rtype: chainlib.connection.RPCConnection :rtype: chainlib.connection.RPCConnection
:returns: An established rpc connection :returns: An established rpc connection
""" """
if config.get('RPC_SCHEME') != 'http':
raise NotImplementedError('Only http(s) scheme is implemented for RPC connections at this time')
auth = None auth = None
if config.get('RPC_AUTH') == 'basic': if config.get('RPC_AUTH') == 'basic':
from chainlib.auth import BasicAuth from chainlib.auth import BasicAuth
@ -58,7 +61,7 @@ class Rpc:
self.id_generator = IntSequenceGenerator() self.id_generator = IntSequenceGenerator()
self.chain_spec = config.get('CHAIN_SPEC') self.chain_spec = config.get('CHAIN_SPEC')
self.conn = self.constructor(url=config.get('RPC_HTTP_PROVIDER'), chain_spec=self.chain_spec, auth=auth) self.conn = self.constructor(url=config.get('RPC_PROVIDER'), chain_spec=self.chain_spec, auth=auth)
return self.conn return self.conn

View File

@ -1,6 +1,6 @@
[metadata] [metadata]
name = chainlib name = chainlib
version = 0.0.9a7 version = 0.0.9a9
description = Generic blockchain access library and tooling description = Generic blockchain access library and tooling
author = Louis Holbrook author = Louis Holbrook
author_email = dev@holbrook.no author_email = dev@holbrook.no