错误设置状态与DBUS

问题描述:

我得到的错误,当我尝试设置状态与使用python, 这DBUS换位思考换位思考是我从不同的来源错误设置状态与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() 

当我运行了这个代码脚本, 我收到以下错误。

[email protected]:~$ 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 

我做错了什么?或者这些功能已被弃用?

试试这与当前曲目在Clementine玩到更新同情的状态:

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) 

感谢您更新格式我原帖! - Harsha

+0

太棒了! thanx – pahnin

+0

@pahnin:不客气! – Harsha

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

我认为问题是,你要存在设置为无效状态 - 你只允许状态设置为一连接管理器可以识别 - 例如可用。您可以设置任何您喜欢的消息。

所以,你的代码应该读的东西,如:

simple_presence.SetPresence("Available", status) 
+0

我试着用** simple_presence.SetPresence(“可用”,状态)** 但它不工作..:| – pahnin

+0

从链接我了解到,状态不能设置为与dbus自定义之一,但我揣摩为什么它仍然不工作,我使用可用。 thnx fr的帮助 – pahnin