aboutsummaryrefslogtreecommitdiff
path: root/test_client.py
diff options
context:
space:
mode:
authorJonathan Weth <git@jonathanweth.de>2021-06-28 11:48:57 +0200
committerJonathan Weth <git@jonathanweth.de>2021-06-28 11:48:57 +0200
commit4b329132854bc4f07bdd70a837e4553c6d106591 (patch)
treeb30675c3dd638c3b071611b997dd3a89113162ca /test_client.py
parent3ef18eda773948cdb12ac6ddaaa3e6d87dd6edbd (diff)
downloadRWA.Support.SessionService-4b329132854bc4f07bdd70a837e4553c6d106591.tar.gz
RWA.Support.SessionService-4b329132854bc4f07bdd70a837e4553c6d106591.tar.bz2
RWA.Support.SessionService-4b329132854bc4f07bdd70a837e4553c6d106591.zip
Rewrite test_client.py using click
Diffstat (limited to 'test_client.py')
-rwxr-xr-xtest_client.py109
1 files changed, 44 insertions, 65 deletions
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 <https://www.gnu.org/licenses/>.
-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 <PID>, an existing support session.")
- arguments.add_argument(
- "-r", "--refresh_status", help="Refreshes the status of <PID>, an existing support session."
- )
- arguments.add_argument(
- "-i", "--status", help="Gets the status of <PID>, 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()