90 lines
3.0 KiB
Python
90 lines
3.0 KiB
Python
# standard imports
|
|
import os
|
|
#import argparse
|
|
import logging
|
|
import importlib
|
|
|
|
# external imports
|
|
import confini
|
|
import chainlib.eth.cli
|
|
from chainlib.chain import ChainSpec
|
|
|
|
# local imports
|
|
import clicada.cli.user as cmd_user
|
|
|
|
script_dir = os.path.dirname(os.path.realpath(__file__))
|
|
data_dir = os.path.join(script_dir, '..', 'data')
|
|
base_config_dir = os.path.join(data_dir, 'config')
|
|
|
|
|
|
class CmdCtrl:
|
|
|
|
__cmd_alias = {
|
|
'u': 'user',
|
|
}
|
|
|
|
def __init__(self, argv=None, description=None, logger=None, *args, **kwargs):
|
|
#self.argparser = argparse.ArgumentParser(description=description, *args, **kwargs)
|
|
self.argparser = chainlib.eth.cli.ArgumentParser(chainlib.eth.cli.argflag_std_read)
|
|
|
|
#self.argparser.add_argument('-c', type=str, help='Configuration override directory path')
|
|
#self.argparser.add_argument('-v', action='store_true', help='Be verbose')
|
|
#self.argparser.add_argument('-vv', action='store_true', help='Be very verbose')
|
|
|
|
sub = self.argparser.add_subparsers()
|
|
sub.dest = 'command'
|
|
sub_user = sub.add_parser('user', aliases=['u'], help='retrieve transactions for a user')
|
|
cmd_user.process_args(sub_user)
|
|
|
|
self.cmd_args = self.argparser.parse_args(argv)
|
|
|
|
if logger == None:
|
|
logger = logging.getLogger()
|
|
if self.cmd_args.vv:
|
|
logger.setLevel(logging.DEBUG)
|
|
elif self.cmd_args.v:
|
|
logger.setLevel(logging.INFO)
|
|
|
|
self.cmd_string = self.cmd_args.command
|
|
cmd_string_translate = self.__cmd_alias.get(self.cmd_string)
|
|
if cmd_string_translate != None:
|
|
self.cmd_string = cmd_string_translate
|
|
|
|
if self.cmd_string == None:
|
|
raise ValueError('Subcommand missing')
|
|
|
|
modname = 'clicada.cli.{}'.format(self.cmd_string)
|
|
logger.debug('using module {}'.format(modname))
|
|
self.cmd_mod = importlib.import_module(modname)
|
|
|
|
# if self.cmd_args.c:
|
|
# self.config = confini.Config(base_config_dir, override_dirs=self.cmd_args.c)
|
|
# else:
|
|
# self.config = confini.Config(base_config_dir)
|
|
extra_args = self.cmd_mod.extra_args()
|
|
logger.debug('using extra args {}'.format(extra_args))
|
|
if self.cmd_args.config:
|
|
self.config = chainlib.eth.cli.Config.from_args(self.cmd_args, base_config_dir=base_config_dir, extra_args=extra_args, override_dirs=self.cmd_args.c)
|
|
else:
|
|
self.config = chainlib.eth.cli.Config.from_args(self.cmd_args, base_config_dir=base_config_dir, extra_args=extra_args)
|
|
|
|
self.config.add(False, '_SEQ')
|
|
logger.debug('loaded config:\n{}'.format(self.config))
|
|
|
|
self.chain_spec = ChainSpec.from_chain_str(self.config.get('CHAIN_SPEC'))
|
|
|
|
self.rpc = chainlib.eth.cli.Rpc()
|
|
self.__conn = self.rpc.connect_by_config(self.config)
|
|
|
|
|
|
def get(self, k):
|
|
return self.config.get(k)
|
|
|
|
|
|
def chain(self):
|
|
return self.chain_spec
|
|
|
|
|
|
def conn(self):
|
|
return self.__conn
|