frappy/bin/frappy-cli
Markus Zolliker 748ea1400a improve interactive client
- remove irrelevant traceback on remote errors
- add run() function to execute scripts
- when started with bin/frappy-cli, use separate namespace

Change-Id: Ic808a76fa76ecd8d814d52b15a6d7d2203c6a2f3
Reviewed-on: https://forge.frm2.tum.de/review/c/secop/frappy/+/30957
Tested-by: Jenkins Automated Tests <pedersen+jenkins@frm2.tum.de>
Reviewed-by: Markus Zolliker <markus.zolliker@psi.ch>
2023-05-02 15:54:32 +02:00

103 lines
3.5 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# *****************************************************************************
# Copyright (c) 2015-2016 by the authors, see LICENSE
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Module authors:
# Alexander Lenz <alexander.lenz@frm2.tum.de>
# Markus Zolliker <markus.zolliker@psi.ch>
#
# *****************************************************************************
import sys
import argparse
from os import path
# Add import path for inplace usage
sys.path.insert(0, path.abspath(path.join(path.dirname(__file__), '..')))
from frappy.client.interactive import Client, Console, clientenv, run
def parseArgv(argv):
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--include',
help='file to execute after connecting to the clients', metavar='file',
type=str, action='append', default=[])
parser.add_argument('-o', '--only-execute',
help='Do not go into interactive mode after executing files. \
Has no effect without --include.', action='store_true')
parser.add_argument('node',
help='Nodes the client should connect to.\n', metavar='host:port',
nargs='*', type=str, default=[])
return parser.parse_args(argv)
USAGE = """
Usage:
{client_assign}
# for all SECoP modules objects are created in the main namespace
<module> # list all parameters
<module>.<param> = <value> # change parameter
<module>(<target>) # set target and wait until not busy
# 'status' and 'value' changes are shown every 1 sec
{client_name}.mininterval = 0.2 # change minimal update interval to 0.2 s (default is 1 s)
watch(T) # watch changes of T.status and T.value (stop with ctrl-C)
watch(T='status target') # watch status and target parameters
watch(io, T=True) # watch io and all parameters of T
{tail}"""
args = parseArgv(sys.argv[1:])
if not args.node:
usage_args = {
'client_assign': "\ncli = Client('localhost:5000')\n",
'client_name': 'cli'}
success = True
else:
usage_args = {
'client_assign': '',
'client_name': '_c0'}
success = False
clientenv.init()
for idx, node in enumerate(args.node):
client_name = '_c%d' % idx
try:
clientenv.namespace[client_name] = Client(node, name=client_name)
success = True
except Exception as e:
print(repr(e))
run_error = ''
file_success = False
try:
for file in args.include:
run(file)
file_success = True
except Exception as e:
run_error = f'\n{clientenv.short_traceback()}'
if success:
if args.include and file_success and args.only_execute:
print('skipping interactive mode')
exit()
print(USAGE.format(tail=run_error, **usage_args))
Console()