aboutsummaryrefslogtreecommitdiffstats
path: root/examples/dbus/pingpong/ping.py
blob: d61f2549911a7c87c820c39023f69d9064da742d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

"""PySide6 port of the QtDBus pingpong example from Qt v6.x (ping client)"""

import sys
from PySide6.QtCore import QCoreApplication
from PySide6.QtDBus import QDBusConnection, QDBusInterface, QDBusReply


SERVICE_NAME = 'org.example.QtDBus.PingExample'


if __name__ == "__main__":
    app = QCoreApplication()
    session_bus = QDBusConnection.sessionBus()
    if not session_bus.isConnected():
        print("Cannot connect to the D-Bus session bus.\n"
              "To start it, run:\n"
              "\teval `dbus-launch --auto-syntax`\n")
        sys.exit(-1)

    iface = QDBusInterface(SERVICE_NAME, '/', '', session_bus)
    if not iface.isValid():
        print(session_bus.lastError().message())
        sys.exit(-1)

    argument = sys.argv[1] if len(sys.argv) > 1 else 'Hello'
    message = iface.call('ping', argument)
    reply = QDBusReply(message)
    if not reply.isValid():
        error = reply.error().message()
        print(f'ping: Call failed: {error}')
        sys.exit(-1)

    value = reply.value()
    print(f'ping: Reply was: {value}')
    sys.exit(0)