Add send script, input processor

This commit is contained in:
nolash 2021-09-07 09:59:54 +02:00
parent 7f42c3d735
commit 6a2061553f
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
6 changed files with 140 additions and 4 deletions

27
chaind_eth/cli/csv.py Normal file
View File

@ -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'

29
chaind_eth/cli/process.py Normal file
View File

@ -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)

View File

@ -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,

View File

@ -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()

View File

@ -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

View File

@ -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