From 6a2061553fc3df1c344fe49b01f544c805e70a64 Mon Sep 17 00:00:00 2001 From: nolash Date: Tue, 7 Sep 2021 09:59:54 +0200 Subject: [PATCH] Add send script, input processor --- chaind_eth/cli/csv.py | 27 ++++++++++++ chaind_eth/cli/process.py | 29 +++++++++++++ chaind_eth/runnable/retry.py | 2 +- chaind_eth/runnable/send.py | 80 ++++++++++++++++++++++++++++++++++++ requirements.txt | 4 +- setup.cfg | 2 +- 6 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 chaind_eth/cli/csv.py create mode 100644 chaind_eth/cli/process.py create mode 100644 chaind_eth/runnable/send.py diff --git a/chaind_eth/cli/csv.py b/chaind_eth/cli/csv.py new file mode 100644 index 0000000..eb72292 --- /dev/null +++ b/chaind_eth/cli/csv.py @@ -0,0 +1,27 @@ +# standard imports +import logging + +logg = logging.getLogger(__name__) + + +class CSVProcessor: + + def process(self, s): + contents = [] + f = None + try: + f = open(s, 'r') + except FileNotFoundError: + return None + + import csv # only import if needed + fr = csv.reader(f) + for r in fr: + contents.append(r) + l = len(contents) + logg.info('successfully parsed source as csv, found {} records'.format(l)) + return contents + + + def __str__(self): + return 'csv processor' diff --git a/chaind_eth/cli/process.py b/chaind_eth/cli/process.py new file mode 100644 index 0000000..448c620 --- /dev/null +++ b/chaind_eth/cli/process.py @@ -0,0 +1,29 @@ +# external imports +from chaind.error import TxSourceError + + +class Processor: + + def __init__(self, source): + self.source = source + self.processor = [] + self.content = [] + + + def add_processor(self, processor): + self.processor.append(processor) + + + def process(self): + for processor in self.processor: + r = processor.process(self.source) + if r != None: + return r + raise TxSourceError() + + + def __str__(self): + names = [] + for s in self.processor: + names.append(str(s)) + return ','.join(names) diff --git a/chaind_eth/runnable/retry.py b/chaind_eth/runnable/retry.py index 8d91b2c..5b0ea70 100644 --- a/chaind_eth/runnable/retry.py +++ b/chaind_eth/runnable/retry.py @@ -35,7 +35,7 @@ config_dir = os.path.join(script_dir, '..', 'data', 'config') arg_flags = chainlib.eth.cli.argflag_std_write argparser = chainlib.eth.cli.ArgumentParser(arg_flags) argparser.add_argument('--backend', type=str, default='sql', help='Backend to use (currently only "sql")') -argparser.add_positional('session_id', required=False, type=str, help='Ethereum address of recipient') +argparser.add_positional('session_id', required=False, type=str, help='Session id to connect to') args = argparser.parse_args() extra_args = { 'backend': None, diff --git a/chaind_eth/runnable/send.py b/chaind_eth/runnable/send.py new file mode 100644 index 0000000..c4f4906 --- /dev/null +++ b/chaind_eth/runnable/send.py @@ -0,0 +1,80 @@ +# standard imports +import os +import logging +import sys +import datetime +import enum +import re +import stat + +# external imports +import chainlib.eth.cli +from chaind import Environment + +# local imports +from chaind_eth.cli.process import Processor +from chaind_eth.cli.csv import CSVProcessor +from chaind.error import TxSourceError + +logging.basicConfig(level=logging.WARNING) +logg = logging.getLogger() + +script_dir = os.path.dirname(os.path.realpath(__file__)) +config_dir = os.path.join(script_dir, '..', 'data', 'config') + + +arg_flags = chainlib.eth.cli.argflag_std_write +argparser = chainlib.eth.cli.ArgumentParser(arg_flags) +argparser.add_argument('--socket', dest='socket', type=str, help='Socket to send transactions to') +argparser.add_positional('source', required=False, type=str, help='Transaction source file') +args = argparser.parse_args() + + + +extra_args = { + 'socket': None, + 'source': None, + } + +env = Environment(domain='eth', env=os.environ) +config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, base_config_dir=config_dir) + +class OpMode(enum.Enum): + STDOUT = 'standard output' + UNIX = 'unix socket' +mode = OpMode.STDOUT + +re_unix = r'^ipc:///.+' +if re.match(re_unix, config.get('_SOCKET', '')): + r = 0 + try: + stat_info = os.stat(config.get('_SOCKET')) + if not stat.S_ISSOCK(stat_info.st_mode): + r = 1 + except FileNotFoundError: + r = 1 + + if r > 0: + sys.stderr.write('{} is not a socket\n'.format(config.get('_SOCKET'))) + sys.exit(1) + + mode = OpMode.UNIX + +logg.info('using mode {}'.format(mode.value)) + +if config.get('_SOURCE') == None: + sys.stderr.write('source data missing') + sys.exit(1) + + +def main(): + processor = Processor(config.get('_SOURCE')) + processor.add_processor(CSVProcessor()) + try: + r = processor.process() + except TxSourceError as e: + sys.stderr.write('source still unknown after trying processors: {}\n'.format(str(processor))) + sys.exit(1) + +if __name__ == '__main__': + main() diff --git a/requirements.txt b/requirements.txt index 34700c2..fd11261 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -chaind<=0.0.3,>=0.0.3a3 +chaind<=0.0.3,>=0.0.3a5 hexathon~=0.0.1a8 -chainlib-eth<=0.1.0,>=0.0.9a5 +chainlib-eth<=0.1.0,>=0.0.9a9 diff --git a/setup.cfg b/setup.cfg index dd562d9..4087753 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = chaind-eth -version = 0.0.3a3 +version = 0.0.3a4 description = Queue server for ethereum author = Louis Holbrook author_email = dev@holbrook.no