Ejemplo n.º 1
0
	def callSync(self, *args, warnWhenCallIsSlow=True, **kwargs):
		"""Call a camera DBus API. First arg is the function name.
			
			This is the synchronous version of the call() method. It
			is much slower to call synchronously than asynchronously!
		
			See http://doc.qt.io/qt-5/qdbusabstractinterface.html#call for details about calling.
			See https://github.com/krontech/chronos-cli/tree/master/src/api for implementation details about the API being called.
			See README.md at https://github.com/krontech/chronos-cli/tree/master/src/daemon for API documentation.
		"""
		
		#Unwrap D-Bus errors from message.
		log.debug("%s.callSync %s", self.name, tuple(args))
		
		start = perf_counter()
		msg = QDBusReply(self.iface.call(*args, **kwargs))
		end = perf_counter()
		if warnWhenCallIsSlow and (end - start > API_SLOW_WARN_MS / 1000):
			log.warn(f'slow call: {self.name}.callSync{tuple(args)} took {(end-start)*1000:.0f}ms/{API_SLOW_WARN_MS}ms.')
		
		if msg.isValid():
			return msg.value()
		else:
			if msg.error().name() == 'org.freedesktop.DBus.Error.NoReply':
				raise DBusException(f"{self.name}.callSync{tuple(args)} timed out ({API_TIMEOUT_MS}ms)")
			else:
				raise DBusException("%s: %s" % (msg.error().name(), msg.error().message()))
Ejemplo n.º 2
0
 def _call(self, method, *args):
     msg = self.cache.call(method, *args)
     reply = QDBusReply(msg)
     if not reply.isValid():
         raise Exception("Error on method call '{}': {}: {}".format(
             method,
             reply.error().name(),
             reply.error().message()))
     else:
         return msg.arguments()
Ejemplo n.º 3
0
def call(obj, method, *args):
    msg = obj.call(method, *args)
    reply = QDBusReply(msg)
    if not reply.isValid():
        raise Exception("Error on method call '{}': {}: {}".format(
            method,
            reply.error().name(),
            reply.error().message()))
    else:
        return msg.arguments()
Ejemplo n.º 4
0
def video(*args, **kwargs):
    """
	Call the camera video DBus API. First arg is the function name.
	
	See http://doc.qt.io/qt-5/qdbusabstractinterface.html#call for details about calling.
	See https://github.com/krontech/chronos-cli/tree/master/src/api for implementation details about the API being called.
	See README.md at https://github.com/krontech/chronos-cli/tree/master/src/daemon for API documentation.
	"""
    msg = QDBusReply(cameraVideoAPI.call(*args, **kwargs))
    if not msg.isValid():
        raise DBusException("%s: %s" %
                            (msg.error().name(), msg.error().message()))
    return msg.value()
Ejemplo n.º 5
0
def main():
    import sys

    app = QCoreApplication(sys.argv)  # noqa: F841

    if not QDBusConnection.sessionBus().isConnected():
        sys.stderr.write("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, "/", "", QDBusConnection.sessionBus())

    if iface.isValid():
        msg = iface.call("ping", sys.argv[1] if len(sys.argv) > 1 else "")
        reply = QDBusReply(msg)

        if reply.isValid():
            sys.stdout.write("Reply was: %s\n" % reply.value())
            sys.exit()

        sys.stderr.write("Call failed: %s\n" % reply.error().message())
        sys.exit(1)

    sys.stderr.write("%s\n" %
                     QDBusConnection.sessionBus().lastError().message())
    sys.exit(1)
Ejemplo n.º 6
0
    def callMethod(cls,
                   method_name: str,
                   signature: str,
                   *args,
                   service_path: str = DefaultServicePath,
                   object_path: str = DefaultObjectPath,
                   interface: str = DefaultInterface) -> Any:
        cls.__ensureDBusSetup()
        assert cls.__connection is not None

        if cls.__use_qt:
            message = QDBusMessage.createMethodCall(service_path, object_path,
                                                    interface, method_name)
            message.setArguments(args)
            result = QDBusReply(cls.__connection.call(message))
            if result.isValid():
                return result.value()
            else:
                log.warning("Did not receive a valid reply for method call %s",
                            method_name)
                log.warning(result.error().message())
                return None

        else:
            return cls.__connection.call_blocking(service_path, object_path,
                                                  interface, method_name,
                                                  signature, args)
Ejemplo n.º 7
0
        def __onAsyncCallFinished(self, watcher):
            assert watcher in self.__pending_async_calls

            success_callback = self.__pending_async_calls[watcher][0]
            error_callback = self.__pending_async_calls[watcher][1]
            del self.__pending_async_calls[watcher]

            reply = QDBusReply(watcher)
            if reply.isValid():
                if success_callback:
                    success_callback(reply.value())
            else:
                if error_callback:
                    error_callback(reply.error().message())
Ejemplo n.º 8
0
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtDBus import QDBusConnection, QDBusInterface, QDBusReply

if __name__ == "__main__":
    app = QCoreApplication(sys.argv)

    if not QDBusConnection.sessionBus().isConnected():
        sys.stderr.write("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("org.example.QtDBus.PingExample", "/", "",
                           QDBusConnection.sessionBus())

    if iface.isValid():
        msg = iface.call("ping", sys.argv[1] if len(sys.argv) > 1 else "")
        reply = QDBusReply(msg)

        if reply.isValid():
            sys.stdout.write("Reply was: %s\n" % reply.value())
            sys.exit()

        sys.stderr.write("Call failed: %s\n" % reply.error().message())
        sys.exit(1)

    sys.stderr.write("%s\n" %
                     QDBusConnection.sessionBus().lastError().message())
    sys.exit(1)
Ejemplo n.º 9
0
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtDBus import QDBusConnection, QDBusInterface, QDBusReply


if __name__ == '__main__':
    app = QCoreApplication(sys.argv)

    if not QDBusConnection.sessionBus().isConnected():
        sys.stderr.write("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('org.example.QtDBus.PingExample', '/', '',
            QDBusConnection.sessionBus())

    if iface.isValid():
        msg = iface.call('ping', sys.argv[1] if len(sys.argv) > 1 else "")
        reply = QDBusReply(msg)

        if reply.isValid():
            sys.stdout.write("Reply was: %s\n" % reply.value())
            sys.exit()

        sys.stderr.write("Call failed: %s\n" % reply.error().message())
        sys.exit(1)

    sys.stderr.write("%s\n" % QDBusConnection.sessionBus().lastError().message())
    sys.exit(1)