Python Forum
How to transmit a dict with dbus and QtDBus
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to transmit a dict with dbus and QtDBus
#2
I could get it to work by converting the dict to a string, send it through dbus, then convert it back to a dict. Is there a better way to do this?

#!/usr/bin/python3
from PyQt5 import QtCore, QtDBus
from PyQt5.QtCore import QObject, pyqtSlot
import argparse
import dbus
import ast
import sys


class QDBusServer(QObject):
    def __init__(self):
        QObject.__init__(self)
        self.__dbusAdaptor = QDBusServerAdapter(self)


class QDBusServerAdapter(QtDBus.QDBusAbstractAdaptor):
    QtCore.Q_CLASSINFO("D-Bus Interface", "com.qtpad.dbus")
    QtCore.Q_CLASSINFO("D-Bus Introspection",
    '  <interface name="com.qtpad.dbus">\n'
    '    <property name="name" type="s" access="read"/>\n'
    '    <method name="parse">\n'
    '      <arg direction="in" type="s" name="cmd"/>\n'
    '    </method>\n'
    '  </interface>\n')

    def __init__(self, parent):
        super().__init__(parent)

    @pyqtSlot(str, result=str)
    def parse(self, cmd):
        parseCli(cmd)


def parseCli(args):
    # Convert the string back to a dict
    args = ast.literal_eval(args)
    for key in args:
        if args[key]:
            print(key + "=" + str(args[key]))

if __name__ == '__main__':
    # Handle command line input
    parser = argparse.ArgumentParser()
    parser.add_argument("-a", "--action", help="execute one of the predefined action, as shown in the preferences menu", metavar='')
    args = parser.parse_args()

    # Convert namespace to dict, then dict to string
    cmd = vars(args)
    cmd = str(cmd)

    # Verify if an instance is already running
    try:
        bus = dbus.SessionBus().get_object("com.qtpad.dbus", "/cli")
        busIO = dbus.Interface(bus, "com.qtpad.dbus")
        # Send the command to the running instance, then quit
        busIO.parse(cmd)
        sys.exit(0)
    except dbus.exceptions.DBusException:
        print("Init of a new instance")

    # Start a new instance
    app = QtCore.QCoreApplication([])
    bus = QtDBus.QDBusConnection.sessionBus()
    server = QDBusServer()
    bus.registerObject('/cli', server)
    bus.registerService('com.qtpad.dbus')
    parseCli(cmd)
    app.exec()
Reply


Messages In This Thread
RE: How to transmit a dict with dbus and QtDBus - by Alfalfa - Dec-21-2017, 06:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a dict in dict cherry_cherry 4 80,378 Apr-08-2020, 12:25 PM
Last Post: perfringo
  Can't transmit serial fast Python to Arduino pyserial mRKlean 0 2,389 Mar-29-2020, 08:12 PM
Last Post: mRKlean

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020