Commit 9aed8149 authored by di68kap's avatar di68kap

examples/EBNF/EBNFServer.py: now uses argparse from standard-library

parent 9f257b04
......@@ -24,7 +24,7 @@ import asyncio
import os
import sys
assert sys.version_info >= (3,5,7), "DHParser requires at least Python-Version 3.5.7"
assert sys.version_info >= (3, 5, 7), "DHParser requires at least Python-Version 3.5.7"
scriptpath = os.path.dirname(__file__)
......@@ -305,7 +305,7 @@ def run_server(host, port, log_path=None):
from EBNFParser import compile_src
except ModuleNotFoundError:
from tst_EBNF_grammar import recompile_grammar
recompile_grammar(os.path.join(scriptpath, 'EBNF.ebnf'), force=False)
recompile_grammar(grammar_src, force=False)
from EBNFParser import compile_src
from DHParser.server import Server, gen_lsp_table
config_filename = get_config_filename()
......@@ -377,106 +377,70 @@ def start_server_daemon(host, port):
print(result)
def print_usage_and_exit():
print('Usages:\n'
+ ' python EBNFServer.py --startserver [--host host] [--port port] [--logging [ON|LOG_PATH|OFF]]\n'
+ ' python EBNFServer.py --startdaemon [--host host] [--port port] [--logging [ON|LOG_PATH|OFF]]\n'
+ ' python EBNFServer.py --stopserver\n'
+ ' python EBNFServer.py --status\n'
+ ' python EBNFServer.py --logging [ON|LOG_PATH|OFF]\n'
+ ' python EBNFServer.py FILENAME.dsl [--host host] [--port port] [--logging [ON|LOG_PATH|OFF]]')
sys.exit(1)
def assert_if(cond: bool, message: str):
if not cond:
if message:
print(message)
print_usage_and_exit()
def parse_logging_args(argv):
try:
i = argv.index('--logging')
echo = repr('ECHO_ON') if argv[1].lower() == '--startserver' else repr('ECHO_OFF')
del argv[i]
if i < len(argv):
arg = argv[i].upper()
if arg in ('OFF', 'STOP', 'NO', 'FALSE'):
log_path = repr(None)
echo = repr('ECHO_OFF')
elif arg in ('ON', 'START', 'YES', 'TRUE'):
log_path = repr('')
else:
log_path = repr(argv[i])
del argv[i]
else:
def parse_logging_args(args):
if args.log:
echo = repr('ECHO_ON') if args.start else repr('ECHO_OFF')
if args.log in ('OFF', 'STOP', 'NO', 'FALSE'):
log_path = repr(None)
echo = repr('ECHO_OFF')
elif args.log in ('ON', 'START', 'YES', 'TRUE'):
log_path = repr('')
args = log_path, echo
request = LOGGING_REQUEST.replace('""', ", ".join(args))
else:
log_path = args.log
request = LOGGING_REQUEST.replace('""', ", ".join((log_path, echo)))
print('Logging to file %s with call %s' % (repr(log_path), request))
return log_path, request
except ValueError:
else:
return None, ''
if __name__ == "__main__":
host, port = '', -1
# read and remove "--host ..." and "--port ..." parameters from sys.argv
argv = []
i = 0
while i < len(sys.argv):
if sys.argv[i] in ('--host', '-h'):
assert_if(i < len(sys.argv) - 1, 'host missing!')
host = sys.argv[i+1]
i += 1
elif sys.argv[i] in ('--port', '-p'):
assert_if(i < len(sys.argv) - 1, 'port number missing!')
try:
port = int(sys.argv[i+1])
except ValueError:
assert_if(False, 'invalid port number: ' + sys.argv[i+1])
i += 1
else:
argv.append(sys.argv[i])
i += 1
if len(argv) < 2:
print_usage_and_exit()
from argparse import ArgumentParser
parser = ArgumentParser(description="Setup and Control of a Server for processing EBNF-files.")
parser.add_argument('files', nargs='?')
parser.add_argument('-h', '--host', action='store_const', const='host', default='',
help='host name or IP-address of the server (default: 127.0.0.1)')
parser.add_argument('-p', '--port', action='store_const', const='port', default=-1, type=int,
help='port number of the server (default:8888)')
parser.add_argument('-l', '--logging', action='store_cont', const='log', default='')
group = parser.add_mutually_exclusive_group()
group.add_argument('-t', '--status', action='store_true', const='status',
help="displays the server's status, e.g. whether it is running")
group.add_argument('-s', '--startserver', action='store_true', const='start',
help="starts the server")
group.add_argument('-d', '--startdaemon', action='store_true', const='daemon',
help="starts the server in the background")
group.add_argument('-k', '--stopserver', action='store_true', const='stop',
help="starts the server")
args = parser.parse_args()
host = args.host
port = args.port
if port < 0 or not host:
# if host and port have not been specified explicitly on the command
# line, try to retrieve them from (temporary) config file or use
# hard coded default values
host, port = retrieve_host_and_port()
if sys.argv[1] == "--status":
if args.status:
try:
result = asyncio_run(send_request(IDENTIFY_REQUEST, host, port))
print('Server ' + str(result) + ' running on ' + host + ' ' + str(port))
except ConnectionRefusedError:
print('No server running on: ' + host + ' ' + str(port))
elif argv[1] == "--startserver":
log_path, _ = parse_logging_args(argv)
if len(argv) == 2:
argv.append(host)
if len(argv) == 3:
argv.append(str(port))
sys.exit(run_server(argv[2], int(argv[3]), log_path))
elif argv[1] == "--startdaemon":
log_path, log_request = parse_logging_args(argv)
if len(argv) == 2:
argv.append(host)
if len(argv) == 3:
argv.append(str(port))
elif args.start:
log_path, _ = parse_logging_args(args)
sys.exit(run_server(host, port, log_path))
elif args.daemon:
log_path, log_request = parse_logging_args(args)
start_server_daemon(host, port)
if log_request:
print(asyncio_run(send_request(log_request, host, port)))
elif argv[1] in ("--stopserver", "--killserver", "--stopdaemon", "--killdaemon"):
elif args.stop:
try:
result = asyncio_run(send_request(STOP_SERVER_REQUEST, host, port))
except ConnectionRefusedError as e:
......@@ -484,31 +448,37 @@ if __name__ == "__main__":
sys.exit(1)
print(result)
elif argv[1] == "--logging":
log_path, request = parse_logging_args(argv)
elif args.log:
log_path, request = parse_logging_args(args)
print(asyncio_run(send_request(request, host, port)))
elif argv[1].startswith('-'):
print_usage_and_exit()
elif argv[1]:
if not argv[1].endswith(')'):
elif args.files:
file_name = args.files[0]
if not file_name.endswith(')'):
# argv does not seem to be a command (e.g. "identify()") but a file name or path
argv[1] = os.path.abspath(argv[1])
# print(argv[1])
log_path, log_request = parse_logging_args(argv)
file_name = os.path.abspath(file_name)
# print(file_name)
log_path, log_request = parse_logging_args(args)
try:
if log_request:
print(asyncio_run(send_request(log_request, host, port)))
result = asyncio_run(send_request(argv[1], host, port))
result = asyncio_run(send_request(file_name, host, port))
except ConnectionRefusedError:
start_server_daemon(host, port) # start server first
if log_request:
print(asyncio_run(send_request(log_request, host, port)))
result = asyncio_run(send_request(argv[1], host, port))
result = asyncio_run(send_request(file_name, host, port))
if len(result) >= DATA_RECEIVE_LIMIT:
print(result, '...')
else:
print(result)
else:
print_usage_and_exit()
print('Usages:\n'
+ ' python EBNFServer.py --startserver [--host host] [--port port] [--logging [ON|LOG_PATH|OFF]]\n'
+ ' python EBNFServer.py --startdaemon [--host host] [--port port] [--logging [ON|LOG_PATH|OFF]]\n'
+ ' python EBNFServer.py --stopserver\n'
+ ' python EBNFServer.py --status\n'
+ ' python EBNFServer.py --logging [ON|LOG_PATH|OFF]\n'
+ ' python EBNFServer.py FILENAME.dsl [--host host] [--port port] [--logging [ON|LOG_PATH|OFF]]')
sys.exit(1)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment