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()))
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)
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)
def inhibit(self): msg = self.call("Inhibit", "DMovie", "Video Playing!") reply = QDBusReply(msg) if reply.isValid(): self._inhibit_cookie = reply.value() return self._inhibit_cookie else: return None
def inhibit(self): msg = self.call("Inhibit", "DMovie", "Video Playing!") reply = QDBusReply(msg) if reply.isValid(): self._inhibit_cookie = reply.value() return self._inhibit_cookie else: return None
def __rescan(self): self._connections.clear() for interfaces in self._networkInterfaces: carrier = QDBusReply( interfaces['Device property interface'].call('Get', 'org.freedesktop.NetworkManager.Device.Wired', 'Carrier' ) ) #Interface, Property if carrier.isValid() and carrier.value(): try: addr = IPv4Address( QDBusReply( interfaces['Device property interface'].call( 'Get', #Method 'org.freedesktop.NetworkManager.Device', #Interface 'Ip4Address', #Property ) ).value() ) addr = IPv4Address('.'.join(reversed(str(addr).split('.')))) #So close. Truly, if there's two ways of representing information… (note: This is actually Python's fault here, the number parses fine in a browser address bar.) except AddressValueError: try: #"Array of tuples of IPv4 address/prefix/gateway. All 3 elements of each tuple are in network byte order. Essentially: [(addr, prefix, gateway), (addr, prefix, gateway), ...]" # -- https://developer.gnome.org/NetworkManager/0.9/spec.html addr = IPv6Address(bytes( QDBusReply( interfaces['Ip6Config property interface'].call( 'Get', #Method 'org.freedesktop.NetworkManager.IP6Config', #Interface 'Addresses', #Property ) ).value()[-1][0] )) except (AddressValueError, IndexError): addr = None interface = QDBusReply( interfaces['Device property interface'].call( 'Get', #Method 'org.freedesktop.NetworkManager.Device', #Interface 'Interface', #Property ) ).value() if addr: self._connections.append({ 'path': interfaces['Device'].path(), 'name': defaultdict( lambda: 'generic connection', {'e': 'ethernet', 'u': 'usb'} )[interface[0]], 'address': addr, }) log.info(f'conns: {self._connections}') for callback in self._callbacks: callback(self._connections)
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()
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()
def notify(self, summary, body): replaceId = QVariant(0) replaceId.convert(QVariant.UInt) actions = QVariant([]) actions.convert(QVariant.StringList) msg = self.call("Notify", "Deepin Movie", replaceId, "deepin-movie", summary, body, actions, {}, -1) reply = QDBusReply(msg) return reply.value if reply.isValid() else None
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()
def notify(self, summary, body, actions=[]): varRPlaceId = QVariant(0) varRPlaceId.convert(QVariant.UInt) varActions = QVariant(actions) varActions.convert(QVariant.StringList) msg = self.call("Notify", "Deepin Screenshot", varRPlaceId, "deepin-screenshot", summary, body, varActions, {}, -1) reply = QDBusReply(msg) if reply.isValid(): return reply.value() else: return None
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())
def notify(self, summary, body): replaceId = QVariant(0) replaceId.convert(QVariant.UInt) actions = QVariant([]) actions.convert(QVariant.StringList) msg = self.call( "Notify", "Deepin Movie", replaceId, "deepin-movie", summary, body, actions, {}, -1) reply = QDBusReply(msg) return reply.value if reply.isValid() else None
def notify(self, summary, body, actions=[]): varRPlaceId = QVariant(0) varRPlaceId.convert(QVariant.UInt) varActions = QVariant(actions) varActions.convert(QVariant.StringList) msg = self.call("Notify", "Deepin Screenshot", varRPlaceId, "deepin-screenshot", summary, body, varActions, {}, -1) reply = QDBusReply(msg) if reply.isValid(): return reply.value() else: return None
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)
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)