From 4b329132854bc4f07bdd70a837e4553c6d106591 Mon Sep 17 00:00:00 2001 From: Jonathan Weth Date: Mon, 28 Jun 2021 11:48:57 +0200 Subject: Rewrite test_client.py using click --- test_client.py | 109 +++++++++++++++++++++++---------------------------------- 1 file changed, 44 insertions(+), 65 deletions(-) (limited to 'test_client.py') diff --git a/test_client.py b/test_client.py index deeb67b..9e2f15c 100755 --- a/test_client.py +++ b/test_client.py @@ -25,75 +25,54 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import argparse -from typing import Union +import click import dbus +bus = dbus.SessionBus() +req = bus.get_object("org.ArcticaProject.RWASupportSessionService", "/RWASupportSessionService") -def str2bool(v: Union[str, bool, int]) -> bool: - """Return true or false if the given string can be interpreted as a boolean. - - If it fails, raise an exception. - """ - if isinstance(v, bool): - return v - if v.lower() in ("yes", "true", "t", "y", "1", 1): - return True - elif v.lower() in ("no", "false", "f", "n", "0", 0): - return False - else: - raise argparse.ArgumentTypeError("Boolean value expected.") - - -def main(): - parser = argparse.ArgumentParser(description="D-Bus Session Service API test client") - arguments = parser.add_mutually_exclusive_group() - arguments.add_argument( - "-s", - "--start", - const=True, - nargs="?", - default=True, - help="Starts a new support session by calling the D-Bus Session Service (default)", - ) - arguments.add_argument("-x", "--stop", help="Stops , an existing support session.") - arguments.add_argument( - "-r", "--refresh_status", help="Refreshes the status of , an existing support session." - ) - arguments.add_argument( - "-i", "--status", help="Gets the status of , an existing support session." - ) - args = parser.parse_args() - - start = args.start - stop_pid = args.stop - status_pid = args.status - refresh_pid = args.refresh_status - - bus = dbus.SessionBus() - req = bus.get_object("org.ArcticaProject.RWASupportSessionService", "/RWASupportSessionService") - - if stop_pid: - print(f"Sending D-Bus request 'stop': {stop_pid}") - response = req.stop(stop_pid) - print(f"Your response is: {response}") - elif status_pid: - print(f"Sending D-Bus request 'status': {status_pid}") - response = req.status(status_pid) - print(f"Your response is: {response}") - elif refresh_pid: - print(f"Sending D-Bus request 'refresh_status': {refresh_pid}") - response = req.refresh_status(refresh_pid) - print(f"Your response is: {response}") - elif start: - print("Sending D-Bus request 'start'") - response = req.start() - print(f"Your response is: {response}") - else: - print("No valid input!") - return + +@click.group() +def cli(): + pass + + +@cli.command() +@click.argument("host") +def start(host: int): + """Start a session on the RWA.Support.WebApp host with index HOST.""" + click.echo(f"Sending D-Bus request 'start': {host}") + response = req.start() + click.echo(f"Your response is: {response}") + + +@cli.command() +@click.argument("pid") +def stop(pid: int): + """Stop the session with the pid PID.""" + click.echo(f"Sending D-Bus request 'stop' with PID {pid}") + response = req.stop(pid) + click.echo(f"Your response is: {response}") + + +@cli.command() +@click.argument("pid") +def status(pid: int): + """Get the status of the session with the pid PID.""" + click.echo(f"Sending D-Bus request 'status' with PID {pid}") + response = req.status(pid) + click.echo(f"Your response is: {response}") + + +@cli.command() +@click.argument("pid") +def refresh_status(pid: int): + """Refresh and get the status of the session with the pid PID.""" + click.echo(f"Sending D-Bus request 'refresh_status' with PID {pid}") + response = req.refresh_status(pid) + click.echo(f"Your response is: {response}") if __name__ == "__main__": - main() + cli() -- cgit v1.2.3