示例#1
0
def test_send_and_get_reply(session_conn):
    ping_call = new_method_call(bus_peer, 'Ping')
    reply = session_conn.send_and_get_reply(ping_call, timeout=5, unwrap=False)
    assert reply.header.message_type == MessageType.method_return
    assert reply.body == ()

    ping_call = new_method_call(bus_peer, 'Ping')
    reply_body = session_conn.send_and_get_reply(ping_call,
                                                 timeout=5,
                                                 unwrap=True)
    assert reply_body == ()
示例#2
0
def test_match_rule_arg():
    rule = MatchRule(type='method_call')
    rule.add_arg_condition(0, 'foo')

    assert rule.matches(
        new_method_call(portal_req_iface, 'Boo', signature='s',
                        body=('foo', )))

    assert not rule.matches(
        new_method_call(
            portal_req_iface, 'Boo', signature='s', body=('foobar', )))

    # No such argument
    assert not rule.matches(new_method_call(portal_req_iface, 'Boo'))
示例#3
0
    def portal_notify(self, body, summary=None, replaces_id=None, timeout=0):
        from jeepney import new_method_call
        _, body, summary = self.get_msg_parms(timeout, body, summary)
        # Note: This backend does not natively support the notion of timeouts
        #
        # While the effect may be emulated by manually withdrawing the notifi-
        # cation from the Calibre side, this resulted in a less than optimal
        # User Experience. Based on this, KG decided it to be better to not
        # support timeouts at all for this backend.
        #
        # See discussion at https://github.com/kovidgoyal/calibre/pull/1268.

        # For the icon: This should instead just send the themable icon name
        #
        # Doing that however, requires Calibre to first be converted to use
        # its AppID everywhere and then we still need a fallback for portable
        # installations.
        msg = new_method_call(self.address, 'AddNotification', 'sa{sv}',
                              (str(replaces_id or 0), {
                                  "title": ('s', summary),
                                  "body": ('s', body),
                                  "icon":
                                  ('(sv)', ("bytes", ('ay', icon(data=True)))),
                              }))
        try:
            self.connection.send(msg)
        except Exception:
            import traceback
            traceback.print_exc()
示例#4
0
    def __init__(self):
        from jeepney import DBusAddress, Properties, new_method_call
        # Prefer desktop portal interface here since it can theoretically
        # work with network management solutions other than NetworkManager
        # and is controlled by the current desktop session
        #
        # There is no difference in terms of “features” provided between
        # the two APIs from our point of view.
        self.xdp_call = lambda: new_method_call(
            DBusAddress('/org/freedesktop/portal/desktop',
                        bus_name='org.freedesktop.portal.Desktop',
                        interface="org.freedesktop.portal.NetworkMonitor"),
            'GetConnectivity')
        self.nm_call = lambda: Properties(
            DBusAddress('/org/freedesktop/NetworkManager',
                        bus_name='org.freedesktop.NetworkManager',
                        interface="org.freedesktop.NetworkManager")).get(
                            'Connectivity')

        if self.xdp() is not None:
            self.get_connectivity = self.xdp
        elif self.nm() is not None:
            self.get_connectivity = self.nm
        else:
            self.get_connectivity = lambda: 4
示例#5
0
 def filesystem_operation_message(self, device_node_path, function_name,
                                  **kw):
     from jeepney import new_method_call
     devname = self.device(device_node_path)
     a = self.address(f'block_devices/{devname}', self.FILESYSTEM)
     kw['auth.no_user_interaction'] = ('b', True)
     return new_method_call(a, function_name, 'a{sv}', (kw, ))
示例#6
0
文件: app.py 项目: m4hi2/routEar
def notify(title='title', body='text', icon='python'):
    """Notifier function."""
    if PLT == "win32":
        toaster.show_toast(title,
                           body,
                           threaded=True,
                           icon_path=path.join(path.dirname(__file__), 'data',
                                               '%s.ico' % icon))
    elif PLT == "linux":
        reply = None

        with closing(open_dbus_connection(bus='SESSION')) as bus:
            msg = new_method_call(NOTIFIER, 'Notify', 'susssasa{sv}i', (
                "routEar",
                NOTIF_ID or 0,
                'file://%s' %
                path.join(path.dirname(__file__), 'data', '%s.png' % icon),
                title,
                body,
                [],
                {},
                -1,
            ))
            reply = bus.send_and_get_reply(msg)
        globals()['NOTIF_ID'] = reply.body[0] if reply else None
    elif PLT == "darwin":
        pync.notify(f"{title} \n {body}", title="routEar")

    else:
        return False
    return True
示例#7
0
async def test_send_and_get_reply_error():
    ping_call = new_method_call(bus_peer, 'Snart')  # No such method
    async with open_dbus_router(bus='SESSION') as req:
        with trio.fail_after(5):
            reply = await req.send_and_get_reply(ping_call)

    assert reply.header.message_type == MessageType.error
示例#8
0
def dbus_get_metadata(path, bus_name, interface=None):
    address = DBusAddress(path, bus_name, interface)
    conn = connect_and_authenticate()
    metadata = conn.send_and_get_reply(new_method_call(address, 'GetMetadata'))
    metadata = dict(metadata[0])
    keys = ['artist', 'title', 'album']
    metadata = {k: v[1] for k, v in metadata.items() if k in keys}
    return Song(**metadata)
示例#9
0
def test_recv_fd(respond_with_fd):
    getfd_call = new_method_call(respond_with_fd, 'GetFD')
    with open_dbus_connection(bus='SESSION', enable_fds=True) as conn:
        reply = conn.send_and_get_reply(getfd_call, timeout=5)

    assert reply.header.message_type is MessageType.method_return
    with reply.body[0].to_file('w+') as f:
        assert f.read() == 'readme'
示例#10
0
def test_send_fd(temp_file_and_contents, read_from_fd):
    temp_file, data = temp_file_and_contents
    readfd_call = new_method_call(read_from_fd, 'ReadFD', 'h', (temp_file, ))
    with open_dbus_connection(bus='SESSION', enable_fds=True) as conn:
        reply = conn.send_and_get_reply(readfd_call, timeout=5)

    assert reply.header.message_type is MessageType.method_return
    assert reply.body[0] == data
示例#11
0
 def eject(self, device_node_path):
     from jeepney import new_method_call
     drive = self.drive_for_device(device_node_path)
     a = self.address(drive, self.DRIVE)
     msg = new_method_call(a, 'Eject', 'a{sv}', ({
         'auth.no_user_interaction': ('b', True),
     }, ))
     self.send(msg)
示例#12
0
async def test_send_and_get_reply():
    ping_call = new_method_call(bus_peer, 'Ping')
    async with open_dbus_router(bus='SESSION') as req:
        with trio.fail_after(5):
            reply = await req.send_and_get_reply(ping_call)

    assert reply.header.message_type == MessageType.method_return
    assert reply.body == ()
示例#13
0
async def test_recv_fd(respond_with_fd):
    getfd_call = new_method_call(respond_with_fd, 'GetFD')
    with trio.fail_after(5):
        async with open_dbus_router(bus='SESSION', enable_fds=True) as router:
            reply = await router.send_and_get_reply(getfd_call)

    assert reply.header.message_type is MessageType.method_return
    with reply.body[0].to_file('w+') as f:
        assert f.read() == 'readme'
示例#14
0
async def test_send_fd(temp_file_and_contents, read_from_fd):
    temp_file, data = temp_file_and_contents
    readfd_call = new_method_call(read_from_fd, 'ReadFD', 'h', (temp_file,))
    with trio.fail_after(5):
        async with open_dbus_router(bus='SESSION', enable_fds=True) as router:
            reply = await router.send_and_get_reply(readfd_call)

    assert reply.header.message_type is MessageType.method_return
    assert reply.body[0] == data
示例#15
0
    def __init__(self, **kwargs) -> None:
        super().__init__(**kwargs)

        address = DBusAddress(
            "/org/gnome/Mutter/IdleMonitor/Core",
            bus_name="org.gnome.Mutter.IdleMonitor",
            interface="org.gnome.Mutter.IdleMonitor",
        )
        self.connection = connect_and_authenticate(bus="SESSION")
        self.message = new_method_call(address, "GetIdletime")
示例#16
0
    def initialize_fdo(self):
        from jeepney import DBusAddress, MessageType, new_method_call
        self.address = DBusAddress('/org/freedesktop/Notifications',
                                   bus_name='org.freedesktop.Notifications',
                                   interface='org.freedesktop.Notifications')

        msg = new_method_call(self.address, 'GetCapabilities')
        reply = self.connection.send_and_get_reply(msg)
        return bool(reply
                    and reply.header.message_type is MessageType.method_return)
示例#17
0
def dbus_notify(title, body, timeout):
    msg = new_method_call(notifier, 'Notify', 'susssasa{sv}i',
                          (
                              APP_NAME,
                              0,  # do not replace notif
                              'dialog-information',
                              title,
                              body,
                              [], {},  # actions, hints
                              timeout,
                          ))
    dbus_connection.send_and_get_reply(msg)
示例#18
0
 def inhibit(self, app_name: str, reason: str) -> PowerManagementCookie:
     return new_method_call(
         self,
         "Inhibit",
         "susu",
         (
             app_name,
             GNOMESessionManager.TOPLEVEL_XID,
             reason,
             GNOMESessionManager.INHIBIT_SUSPEND,
         ),
     )
示例#19
0
def test_match_rule_simple():
    rule = MatchRule(
        type='signal',
        interface='org.freedesktop.portal.Request',
    )
    assert rule.matches(new_signal(portal_req_iface, 'Response'))

    # Wrong message type
    assert not rule.matches(new_method_call(portal_req_iface, 'Boo'))

    # Wrong interface
    assert not rule.matches(
        new_signal(portal.with_interface('org.freedesktop.portal.FileChooser'),
                   'Response'))
示例#20
0
def dbus_notify(title, body, timeout):
    global notif_id
    connection = open_dbus_connection(bus='SESSION')
    msg = new_method_call(notifier, 'Notify', 'susssasa{sv}i',
                          (
                              APP_NAME,
                              notif_id,
                              'dialog-information',
                              title,
                              body,
                              [], {},
                              timeout,
                          ))
    reply = connection.send_and_get_reply(msg)
    connection.close()
    notif_id = reply.body[0]
示例#21
0
def suspend_with_dbus():
    suspend = DBusAddress('/org/freedesktop/login1',
                          bus_name='org.freedesktop.login1',
                          interface='org.freedesktop.login1.Manager')

    connection = connect_and_authenticate(bus='SYSTEM')

    # Construct a new D-Bus message. new_method_call takes the address, the
    # method name, the signature string, and a tuple of arguments.
    # 'signature string' is not documented well, it's bascally the argument types. More info here
    #    https://dbus.freedesktop.org/doc/dbus-specification.html#type-system
    msg = new_method_call(suspend, 'Suspend', 'b', (True, ))

    # Send the message and wait for the reply
    reply = connection.send_and_get_reply(msg)
    print('Reply: ', reply)

    connection.close()
示例#22
0
 def fdo_notify(self, body, summary=None, replaces_id=None, timeout=0):
     from jeepney import new_method_call
     timeout, body, summary = self.get_msg_parms(timeout, body, summary)
     msg = new_method_call(
         self.address,
         'Notify',
         'susssasa{sv}i',
         (
             __appname__,
             replaces_id or 0,
             icon(),
             summary,
             body,
             [],
             {},  # Actions, hints
             timeout,
         ))
     try:
         self.connection.send(msg)
     except Exception:
         import traceback
         traceback.print_exc()
示例#23
0
def test_match_rule_arg_path():
    rule = MatchRule(type='method_call')
    rule.add_arg_condition(0, '/aa/bb/', kind='path')

    # Exact match
    assert rule.matches(
        new_method_call(portal_req_iface,
                        'Boo',
                        signature='s',
                        body=('/aa/bb/', )))

    # Match a prefix
    assert rule.matches(
        new_method_call(portal_req_iface,
                        'Boo',
                        signature='s',
                        body=('/aa/bb/cc', )))

    # Argument is a prefix, ending with /
    assert rule.matches(
        new_method_call(portal_req_iface,
                        'Boo',
                        signature='s',
                        body=('/aa/', )))

    # Argument is a prefix, but NOT ending with /
    assert not rule.matches(
        new_method_call(portal_req_iface, 'Boo', signature='s',
                        body=('/aa', )))

    assert not rule.matches(
        new_method_call(
            portal_req_iface, 'Boo', signature='s', body=('/aa/bb', )))

    # Not a string
    assert not rule.matches(
        new_method_call(portal_req_iface, 'Boo', signature='u', body=(12, )))
示例#24
0
async def test_send_and_get_reply_old(session_proto):
    ping_call = new_method_call(bus_peer, 'Ping')
    reply_body = await asyncio.wait_for(session_proto.send_message(ping_call),
                                        timeout=5)
    assert reply_body == ()
示例#25
0
 def is_inhibited(self) -> bool:
     return new_method_call(self, "IsInhibited", "u",
                            (GNOMESessionManager.INHIBIT_SUSPEND, ))
示例#26
0
async def test_send_and_get_reply(router):
    ping_call = new_method_call(bus_peer, 'Ping')
    reply = await asyncio.wait_for(router.send_and_get_reply(ping_call),
                                   timeout=5)
    assert reply.body == ()
示例#27
0
def test_send_and_get_reply(router):
    ping_call = new_method_call(bus_peer, 'Ping')
    reply = router.send_and_get_reply(ping_call, timeout=5)
    assert reply.header.message_type == MessageType.method_return
    assert reply.body == ()
示例#28
0
 def call(self, addr, method, sig='', *args):
     if sig:
         return self.send(new_method_call(addr, method, sig, args))
     return self.send(new_method_call(addr, method))
示例#29
0
 def call(self, method: str, signature: str, *body: Any) -> Any:
     msg = new_method_call(self, method, signature, body)
     return self.send_and_get_reply(msg)
示例#30
0
def test_listen(jeepney_one_time_server, jeepney_client, jeepney_connection):
    msg = jeepney.new_method_call(jeepney_client, 'Ping', '', tuple())
    reply = jeepney_connection.send_and_get_reply(msg)
    assert reply.body[0] == 'Pong!'