将状态设置为同意dbus时出错

时间:2011-10-06 08:18:38

标签: python ubuntu dbus

当我尝试使用python设置状态以同心使用dbus时,我收到错误, 这是我从不同来源获得的代码

## getting status from clementine music player

import dbus

# Clementine lives on the Session bus
session_bus = dbus.SessionBus()

# Get Clementine's player object, and then get an interface from that object,
# otherwise we'd have to type out the full interface name on every method call.
player = session_bus.get_object('org.mpris.clementine', '/Player')
iface = dbus.Interface(player, dbus_interface='org.freedesktop.MediaPlayer')

# Call a method on the interface
metadata = iface.GetMetadata()
print metadata["title"]+' - '+metadata["artist"]
status = metadata["title"]+' - '+metadata["artist"]

## the below code is from https://github.com/engla/kupfer/blob/master/kupfer/plugin/empathy.py
import os
import subprocess
import sys
import time

import pynotify as pn

# it takes a long time before empathy is willing to accept statuses
EMPATHY_STARTUP_SECONDS = 20

def show_usage():
    print "\nUsage:"
    print sys.argv[0], "|".join(_STATUSES.keys())

def set_status(status):
    try:

        activate(status)
        notify_set_status(status)
    except IndexError:
        print "Missing required parameter."
        show_usage()
    except ValueError as err:
        print err
        show_usage()

def notify_set_status(status):
    success = pn.init("icon-summary-body")
    if not success:
        raise Error()

    # I like this icon, even if it's not relevant
    icon = 'notification-keyboard-brightness-low'
    pn.Notification("Empathy", "Tried to set status to "+ status, icon).show()

def main():# give empathy some time to start up
    set_status(status)


def _(text):
    return text

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# All code below was derived from https://github.com/engla/kupfer/blob/master/kupfer/plugin/empathy.py
ACCOUNTMANAGER_PATH = "/org/freedesktop/Telepathy/AccountManager"
ACCOUNTMANAGER_IFACE = "org.freedesktop.Telepathy.AccountManager"
ACCOUNT_IFACE = "org.freedesktop.Telepathy.Account"
CHANNEL_GROUP_IFACE = "org.freedesktop.Telepathy.Channel.Interface.Group"
CONTACT_IFACE = "org.freedesktop.Telepathy.Connection.Interface.Contacts"
SIMPLE_PRESENCE_IFACE = "org.freedesktop.Telepathy.Connection.Interface.SimplePresence"
DBUS_PROPS_IFACE = "org.freedesktop.DBus.Properties"
CHANNELDISPATCHER_IFACE = "org.freedesktop.Telepathy.ChannelDispatcher"
CHANNELDISPATCHER_PATH = "/org/freedesktop/Telepathy/ChannelDispatcher"
CHANNEL_TYPE = "org.freedesktop.Telepathy.Channel.ChannelType"
CHANNEL_TYPE_TEXT = "org.freedesktop.Telepathy.Channel.Type.Text"
CHANNEL_TARGETHANDLE = "org.freedesktop.Telepathy.Channel.TargetHandle"
CHANNEL_TARGETHANDLETYPE = "org.freedesktop.Telepathy.Channel.TargetHandleType"
EMPATHY_CLIENT_IFACE = "org.freedesktop.Telepathy.Client.Empathy"

EMPATHY_ACCOUNT_KEY = "EMPATHY_ACCOUNT"
EMPATHY_CONTACT_ID = "EMPATHY_CONTACT_ID"


_ATTRIBUTES = {
    'alias': 'org.freedesktop.Telepathy.Connection.Interface.Aliasing/alias',
    'presence': 'org.freedesktop.Telepathy.Connection.Interface.SimplePresence/presence',
    'contact_caps': 'org.freedesktop.Telepathy.Connection.Interface.ContactCapabilities.DRAFT/caps',
    'jid': 'org.freedesktop.Telepathy.Connection/contact-id',
    'caps': 'org.freedesktop.Telepathy.Connection.Interface.Capabilities/caps',
}
def _create_dbus_connection():
        sbus = dbus.SessionBus()
        proxy_obj = sbus.get_object(ACCOUNTMANAGER_IFACE, ACCOUNTMANAGER_PATH)
        dbus_iface = dbus.Interface(proxy_obj, DBUS_PROPS_IFACE)
        return dbus_iface

def activate(status):

    bus = dbus.SessionBus()
    interface = _create_dbus_connection()
    for valid_account in interface.Get(ACCOUNTMANAGER_IFACE, "ValidAccounts"):
        account = bus.get_object(ACCOUNTMANAGER_IFACE, valid_account)
        connection_status = account.Get(ACCOUNT_IFACE, "ConnectionStatus")
        if connection_status != 0:
            continue

        connection_path = account.Get(ACCOUNT_IFACE, "Connection")
        connection_iface = connection_path.replace("/", ".")[1:]
        connection = bus.get_object(connection_iface, connection_path)
        simple_presence = dbus.Interface(connection, SIMPLE_PRESENCE_IFACE)
        try:
            simple_presence.SetPresence(status, _(status))
        except dbus.exceptions.DBusException:
            print(status + ' is not supported by ' + valid_account)
            print simple_presence

main()

当我运行此脚本时, 我收到以下错误。

phanindra@phanindra:~$ python clementine.py 
onelove - Blue

(clementine.py:6142): Gtk-WARNING **: Unable to locate theme engine in module_path: "pixmap",
onelove - Blue is not supported by /org/freedesktop/Telepathy/Account/gabble/jabber/abcd_40gmail_2ecom0

我做错了什么?或者不推荐使用这些功能?

2 个答案:

答案 0 :(得分:4)

尝试使用Clementine中播放的当前曲目更新Empathy的状态:

import dbus
session_bus = dbus.SessionBus()
player = session_bus.get_object('org.mpris.clementine', '/Player')
iface = dbus.Interface(player, dbus_interface='org.freedesktop.MediaPlayer')
metadata = iface.GetMetadata()
status = "♫ ".decode('utf8')+metadata["title"]+' - '+metadata["album"]+" ♫".decode('utf8')
print status
from gi.repository import TelepathyGLib as Tp
from gi.repository import GObject
loop = GObject.MainLoop()
am = Tp.AccountManager.dup()
am.prepare_async(None, lambda *args: loop.quit(), None)
loop.run()
am.set_all_requested_presences(Tp.ConnectionPresenceType.AVAILABLE,
'available', status)

感谢您更新原始帖子中的格式! - 哈尔沙

答案 1 :(得分:1)

以下是SimplePresence API的SetPresence调用的API规范:http://telepathy.freedesktop.org/spec/Connection_Interface_Simple_Presence.html#Method:SetPresence

我认为问题在于您尝试将状态设置为无效状态 - 您只允许将状态设置为连接管理器识别的状态 - 例如可用。您可以为消息设置任何您喜欢的内容。

所以你的代码应该是这样的:

simple_presence.SetPresence("Available", status)