def test_filter(session_conn):
    bus = Proxy(message_bus, session_conn)
    name = "io.gitlab.takluyver.jeepney.tests.blocking_test_filter"

    match_rule = MatchRule(
        type="signal",
        sender=message_bus.bus_name,
        interface=message_bus.interface,
        member="NameOwnerChanged",
        path=message_bus.object_path,
    )
    match_rule.add_arg_condition(0, name)

    # Ask the message bus to subscribe us to this signal
    bus.AddMatch(match_rule)

    with session_conn.filter(match_rule) as matches:
        res, = bus.RequestName(name)
        assert res == 1  # 1: got the name

        for _ in range(5):
            if len(matches):
                break
            session_conn.recv_messages(timeout=1.0)
        else:
            raise AssertionError("Expected signal message not received")

        signal_msg = matches.popleft()
        assert signal_msg.body == (name, '', session_conn.unique_name)
Example #2
0
def test_proxy(session_conn):
    proxy = Proxy(message_bus, session_conn, timeout=5)
    name = "io.gitlab.takluyver.jeepney.examples.Server"
    res = proxy.RequestName(name)
    assert res in {(1, ), (2, )}  # 1: got the name, 2: queued

    has_owner, = proxy.NameHasOwner(name, _timeout=3)
    assert has_owner is True
Example #3
0
def generate(path, name, output_file, bus='SESSION'):
    conn = open_dbus_connection(bus)
    introspectable = Proxy(Introspectable(path, name), conn)
    xml, = introspectable.Introspect()
    # print(xml)

    n_interfaces = code_from_xml(xml, path, name, output_file)
    print("Written {} interface wrappers to {}".format(n_interfaces,
                                                       output_file))
Example #4
0
def check_service_availability(connection: DBusConnection) -> bool:
	"""Returns True if the Secret Service daemon is either running or
	available for activation via D-Bus, False otherwise.

	.. versionadded:: 3.2
	"""
	from secretstorage.util import BUS_NAME
	proxy = Proxy(message_bus, connection)
	return (proxy.NameHasOwner(BUS_NAME)[0] == 1
	        or BUS_NAME in proxy.ListActivatableNames()[0])
Example #5
0
def _enter(*, display: bool, app_name: str, reason: str) -> None:
    global _connection, _disposers, _interfaces, _interface_candidates

    if _connection is None:
        try:
            _connection = open_dbus_connection()
        except Exception:
            raise NotSupportedError(
                "Cannot establish connection to D-Bus") from None

    success = False
    try:
        for cls in _interface_candidates:
            interface = cls()
            try:
                (cookie, ) = Proxy(interface,
                                   _connection).inhibit(app_name, reason)
                success = True
                break
            except DBusErrorResponse:
                pass
        else:
            raise NotSupportedError(
                "No supported power management DBus interface is available")
    finally:
        if not success and not _disposers and _connection is not None:
            # connection is not needed any more
            _connection.close()
            _connection = None

    def disposer(bus: Any) -> None:
        Proxy(interface, bus).uninhibit(cookie)

    _disposers.append(disposer)
    _interfaces.append(interface)
Example #6
0
def test_filter(session_conn):
    bus = Proxy(message_bus, session_conn)
    name = "io.gitlab.takluyver.jeepney.tests.blocking_test_filter"

    match_rule = MatchRule(
        type="signal",
        sender=message_bus.bus_name,
        interface=message_bus.interface,
        member="NameOwnerChanged",
        path=message_bus.object_path,
    )
    match_rule.add_arg_condition(0, name)

    # Ask the message bus to subscribe us to this signal
    bus.AddMatch(match_rule)

    with session_conn.filter(match_rule) as matches:
        res, = bus.RequestName(name)
        assert res == 1  # 1: got the name

        signal_msg = session_conn.recv_until_filtered(matches, timeout=2)

        assert signal_msg.body == (name, '', session_conn.unique_name)
Example #7
0
def _verify() -> bool:
    global _interfaces

    if not _interfaces:
        return False

    interface = _interfaces[-1]
    if hasattr(interface, "is_inhibited"):
        with closing(open_dbus_connection()) as bus:
            try:
                (result, ) = Proxy(interface, bus).is_inhibited()
                return result
            except DBusErrorResponse:
                pass

    global _disposers
    return bool(_disposers)
Example #8
0
def grab_full_desktop() -> QtGui.QImage:
    """Capture rect of screen on gnome systems using wayland."""
    logger.debug("Use capture method: DBUS portal")

    image = QtGui.QImage()

    _, temp_name = tempfile.mkstemp(prefix="normcap")
    try:
        connection = open_dbus_connection(bus="SESSION")

        token = f"normcap_{secrets.token_hex(8)}"
        sender_name = connection.unique_name[1:].replace(".", "_")
        handle = f"/org/freedesktop/portal/desktop/request/{sender_name}/{token}"

        response_rule = MatchRule(type="signal",
                                  interface="org.freedesktop.portal.Request",
                                  path=handle)
        Proxy(message_bus, connection).AddMatch(response_rule)

        with connection.filter(response_rule) as responses:
            msg = FreedesktopPortalScreenshot().grab("", {
                "handle_token": ("s", token),
                "interactive": ("b", False)
            })
            connection.send_and_get_reply(msg)
            response = connection.recv_until_filtered(responses)

        response_code, response_body = response.body
        assert response_code == 0 and "uri" in response_body

        image = QtGui.QImage(urlparse(response_body["uri"][1]).path)

    except AssertionError as e:
        logger.warning("Couldn't take screenshot with DBUS. Got cancelled?")
        raise e from e
    except DBusErrorResponse as e:
        if "invalid params" in [d.lower() for d in e.data]:
            logger.info("ScreenShot with DBUS failed with 'invalid params'")
        else:
            logger.exception("ScreenShot with DBUS through exception")
    finally:
        Path(temp_name).unlink()

    return image
Example #9
0
 def disposer(bus: Any) -> None:
     Proxy(interface, bus).uninhibit(cookie)