mirror of
https://github.com/aljazceru/python-teos.git
synced 2025-12-18 14:44:21 +01:00
Improves cli commands and usage message
This commit is contained in:
@@ -2,7 +2,7 @@ import requests
|
|||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
from getopt import getopt
|
from getopt import getopt, GetoptError
|
||||||
from sys import argv
|
from sys import argv
|
||||||
import logging
|
import logging
|
||||||
from conf import CLIENT_LOG_FILE
|
from conf import CLIENT_LOG_FILE
|
||||||
@@ -13,9 +13,6 @@ from requests import ConnectTimeout
|
|||||||
from apps import DEFAULT_PISA_API_SERVER, DEFAULT_PISA_API_PORT
|
from apps import DEFAULT_PISA_API_SERVER, DEFAULT_PISA_API_PORT
|
||||||
|
|
||||||
|
|
||||||
commands = ['add_appointment']
|
|
||||||
|
|
||||||
|
|
||||||
def build_appointment(tx, tx_id, start_block, end_block, dispute_delta, debug, logging):
|
def build_appointment(tx, tx_id, start_block, end_block, dispute_delta, debug, logging):
|
||||||
locator = sha256(unhexlify(tx_id)).hexdigest()
|
locator = sha256(unhexlify(tx_id)).hexdigest()
|
||||||
|
|
||||||
@@ -42,40 +39,69 @@ def check_txid_format(txid):
|
|||||||
return re.search(r'^[0-9A-Fa-f]+$', txid) is not None
|
return re.search(r'^[0-9A-Fa-f]+$', txid) is not None
|
||||||
|
|
||||||
|
|
||||||
def show_usage():
|
def show_usage(show_and_quit=False):
|
||||||
print("usage: python pisa-cli.py argument [additional_arguments]."
|
print("usage: python pisa-cli.py arguments [additional_arguments]."
|
||||||
"\nArguments:"
|
"\nArguments:"
|
||||||
"\nadd_appointment appointment: \tregisters a json formatted appointment "
|
"\n-a, --add_appointment appointment: \tregisters a json formatted appointment"
|
||||||
"\nhelp: \t\tshows this message.")
|
"\n-f, --file appointment_file: \tregisters an appointment from a an json formatted file"
|
||||||
|
"\n-s, --server: \tAPI server where to send the requests. Defaults to localhost (modifiable in __init__.py)"
|
||||||
|
"\n-p, --port: \tAPI port where to send the requests. Defaults to 9814 (modifiable in __init__.py)"
|
||||||
|
"\n-d, --debug: \tshows debug information and stores it in pisa.log"
|
||||||
|
"\n-h --help: \tshows this message.")
|
||||||
|
|
||||||
|
if show_and_quit:
|
||||||
|
exit(-1)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
debug = False
|
debug = False
|
||||||
|
help_command = False
|
||||||
|
command = None
|
||||||
|
appointment_data = None
|
||||||
pisa_api_server = DEFAULT_PISA_API_SERVER
|
pisa_api_server = DEFAULT_PISA_API_SERVER
|
||||||
pisa_api_port = DEFAULT_PISA_API_PORT
|
pisa_api_port = DEFAULT_PISA_API_PORT
|
||||||
command = None
|
commands = ['add_appointment']
|
||||||
|
|
||||||
opts, args = getopt(argv[1:], 'a:dh:p:', ['add_appointment, debug, host, port'])
|
try:
|
||||||
for opt, arg in opts:
|
opts, args = getopt(argv[1:], 'a:-f:s:p:dh', ['add_appointment, file, server, port, debug, help'])
|
||||||
if opt in ['-a', '--add_appointment']:
|
|
||||||
if arg:
|
for opt, arg in opts:
|
||||||
if not os.path.isfile(arg):
|
if opt in ['-a', '--add_appointment']:
|
||||||
raise Exception("Can't find file " + arg)
|
if arg:
|
||||||
else:
|
|
||||||
command = 'add_appointment'
|
command = 'add_appointment'
|
||||||
json_file = arg
|
appointment_data = json.loads(arg)
|
||||||
else:
|
else:
|
||||||
raise Exception("Path to appointment_data.json missing.")
|
print("No appointment provided.")
|
||||||
if opt in ['-d', '--debug']:
|
show_usage(show_and_quit=True)
|
||||||
debug = True
|
|
||||||
|
|
||||||
if opt in ['-h', 'host']:
|
elif opt in ['-f', '--file']:
|
||||||
if arg:
|
if arg:
|
||||||
pisa_api_server = arg
|
if not os.path.isfile(arg):
|
||||||
|
print("Can't find file " + arg)
|
||||||
|
show_usage(show_and_quit=True)
|
||||||
|
else:
|
||||||
|
command = 'add_appointment'
|
||||||
|
appointment_data = json.load(open(arg))
|
||||||
|
else:
|
||||||
|
raise Exception("Path to appointment_data.json missing.")
|
||||||
|
if opt in ['-s', 'server']:
|
||||||
|
if arg:
|
||||||
|
pisa_api_server = arg
|
||||||
|
|
||||||
if opt in ['-p', '--port']:
|
if opt in ['-p', '--port']:
|
||||||
if arg:
|
if arg:
|
||||||
pisa_api_port = int(arg)
|
pisa_api_port = int(arg)
|
||||||
|
|
||||||
|
if opt in ['-d', '--debug']:
|
||||||
|
debug = True
|
||||||
|
|
||||||
|
if opt in ['-h', '--help']:
|
||||||
|
help_command = True
|
||||||
|
|
||||||
|
except GetoptError as e:
|
||||||
|
print(e)
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
print('Non-JSON encoded appointment passed as parameter.')
|
||||||
|
|
||||||
# Configure logging
|
# Configure logging
|
||||||
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO, handlers=[
|
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO, handlers=[
|
||||||
@@ -83,9 +109,10 @@ if __name__ == '__main__':
|
|||||||
logging.StreamHandler()
|
logging.StreamHandler()
|
||||||
])
|
])
|
||||||
|
|
||||||
if command in commands:
|
if help_command:
|
||||||
if command == 'add_appointment':
|
show_usage()
|
||||||
appointment_data = json.load(open(json_file))
|
elif command in commands:
|
||||||
|
if command == 'add_appointment' and appointment_data:
|
||||||
valid_locator = check_txid_format(appointment_data.get('tx_id'))
|
valid_locator = check_txid_format(appointment_data.get('tx_id'))
|
||||||
|
|
||||||
if valid_locator:
|
if valid_locator:
|
||||||
@@ -102,6 +129,8 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
if debug:
|
if debug:
|
||||||
logging.info("[Client] {} (code: {})".format(r.text, r.status_code))
|
logging.info("[Client] {} (code: {})".format(r.text, r.status_code))
|
||||||
|
else:
|
||||||
|
print("[Client] {} (code: {})".format(r.text, r.status_code))
|
||||||
|
|
||||||
except ConnectTimeout:
|
except ConnectTimeout:
|
||||||
if debug:
|
if debug:
|
||||||
@@ -111,5 +140,6 @@ if __name__ == '__main__':
|
|||||||
raise ValueError("The provided locator is not valid.")
|
raise ValueError("The provided locator is not valid.")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
show_usage()
|
print("No valid appointment data provided.")
|
||||||
|
show_usage(show_and_quit=True)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user