OSDN Git Service

feat(handle transaction): complete handle transaction function
[bytom/bytom-spanner.git] / btmvalidation.py
1 import os
2 import sys
3 import getopt
4 import httprequest
5
6
7 def validate_address(line, address):
8     parameter = {'address': address}
9     data = httprequest.post('validate-address', parameter)
10     if not data['valid']:
11         print(line + 'This line transaction address is not valid')
12         sys.exit(1)
13
14
15 def validate_amount(line, amount):
16     try:
17         int(amount)
18     except ValueError:
19         print(line + 'This line transaction amount is not int.')
20         sys.exit(1)
21
22
23 def get_input(argv):
24     global _input_path, _account_id, _password
25     try:
26         opts, args = getopt.getopt(argv[1:], 'i:a:p:', ['input=', 'account=', 'password='])
27         if len(opts) < 3:
28             raise getopt.GetoptError('lose command option parameter')
29     except getopt.GetoptError as err:
30         print('Input error:' + str(err) +
31               '\n' + 'Example usage:' +
32               '\n' + '\t./main.py -i input.txt -a 0ETRPAV900A02 -p 123456')
33         sys.exit(1)
34     for opt, arg in opts:
35         if opt in ('-a', '--account'):
36             _account_id = arg
37         if opt in ('-i', '--input'):
38             _input_path = arg
39         if opt in ('-p', '--password'):
40             _password = arg
41     return _input_path, _account_id, _password
42
43
44 def validate_input(argv):
45     input_path, account_id, password = get_input(argv)
46     # relative path
47     file_path = os.path.abspath('.') + os.path.sep + input_path
48     if not os.path.exists(file_path):
49         # absolute path
50         file_path = input_path
51     # read file
52     with open(file_path, 'r', encoding='utf-8') as file:
53         for line in file:
54             splits = line.strip().split(',')
55             validate_address(line, splits[0])
56             validate_amount(line, splits[1])
57     return file_path, account_id, password