Python 如何在节奏盒插件中列出所有艺术家

Python 如何在节奏盒插件中列出所有艺术家,python,rhythmbox,Python,Rhythmbox,我试图从一个RhythBox python插件中列出rhythmbox数据库中的所有艺术家。我找到的唯一解决方案是让UI选择所有艺术家和所有歌曲,循环浏览每首歌曲,并将该歌曲的艺术家名称添加到集合中 问题是(除了效率低下之外),我不想仅仅因为需要数据库中所有艺术家的列表而更改选定的艺术家。我之前曾尝试保存选定的艺术家,以便在完成后可以将其还原,但这会导致一些问题,因为用户界面需要一些时间来更新新信息,而且信息越多(即数据库中的歌曲越多),所需时间就越长 代码可以用 git克隆git@github

我试图从一个RhythBox python插件中列出rhythmbox数据库中的所有艺术家。我找到的唯一解决方案是让UI选择所有艺术家和所有歌曲,循环浏览每首歌曲,并将该歌曲的艺术家名称添加到集合中

问题是(除了效率低下之外),我不想仅仅因为需要数据库中所有艺术家的列表而更改选定的艺术家。我之前曾尝试保存选定的艺术家,以便在完成后可以将其还原,但这会导致一些问题,因为用户界面需要一些时间来更新新信息,而且信息越多(即数据库中的歌曲越多),所需时间就越长

代码可以用 git克隆git@github.com:sameltvom/dblister.git

代码如下:

import rb
import rhythmdb
import gtk

class DblisterPlugin (rb.Plugin):
    def __init__(self):
        rb.Plugin.__init__(self)
    def activate(self, shell):
        self.shell = shell
        print '##### dblister #####'

        # choose all artists, this will choose all albums and songs as well

        # get the lock for rhythmbox ui
        gtk.gdk.threads_enter()
        for p in self.shell.props.library_source.get_property_views():
            if p.props.prop == rhythmdb.PROP_ARTIST:
                p.set_selection([""])
                break
        gtk.gdk.threads_leave()

        ##################### Print all artists in database ######################

        # loop through all songs currently selected (i.e. all songs since we did p.set_selection([""]) above
        # for each song, try to add the artist name to the 'artists' set
        artists = set() # unique keys, no duplicates
        for row in self.shell.props.selected_source.props.query_model:
             entry = row[0]
             artist = self.shell.props.db.entry_get(entry, rhythmdb.PROP_ARTIST)
            artists.add(artist)

        print '--- artists ---'
        for artist in artists:
            print artist


        ##################### Print all songs in database ######################

        print '--- songs ---'
        # loop through all songs currently selected (i.e. all songs since we did p.set_selection([""]) above
        # for each song, print artist name and title
        for row in self.shell.props.selected_source.props.query_model:
             entry = row[0]
             artist = self.shell.props.db.entry_get(entry, rhythmdb.PROP_ARTIST)
             song = self.shell.props.db.entry_get(entry, rhythmdb.PROP_TITLE)
            print artist + ' - ' + song

    def deactivate(self, shell):
        del self.shell
        print 'Bye world'
我想这样做的原因是因为我正在开发一个到rhythmbox的telnet接口

欢迎您的输入

亲切问候,,
塞缪尔,我找到了!如果我想列出所有条目,无论在UI中选择了什么,我都应该使用属性基查询模型

代码现在看起来像:

import rb
import rhythmdb
import gtk

class DblisterPlugin (rb.Plugin):
    def __init__(self):
        rb.Plugin.__init__(self)
    def activate(self, shell):
        self.shell = shell
        print '##### dblister #####'


        #################### Print all artists in the library ####################
        artists = set() # unique keys, no duplicates
        for row in self.shell.props.library_source.props.base_query_model:
             entry = row[0]
             artist = self.shell.props.db.entry_get(entry, rhythmdb.PROP_ARTIST)
            artists.add(artist)

        print '--- artists using library_source---'
        for artist in artists:
            print artist

        del artists


        ##################### Print all artists in database ######################

        artists = set() # unique keys, no duplicates
        for row in self.shell.props.selected_source.props.base_query_model:
             entry = row[0]
             artist = self.shell.props.db.entry_get(entry, rhythmdb.PROP_ARTIST)
            artists.add(artist)

        print '--- artists ---'
        for artist in artists:
            print artist


        ##################### Print all songs in database ######################

        print '--- songs ---'
        for row in self.shell.props.selected_source.props.base_query_model:
             entry = row[0]
             artist = self.shell.props.db.entry_get(entry, rhythmdb.PROP_ARTIST)
             song = self.shell.props.db.entry_get(entry, rhythmdb.PROP_TITLE)
            print artist + ' - ' + song

    def deactivate(self, shell):
        del self.shell
        print 'Bye world'
我还发现了另一件好事。如果我使用elf.shell.props.library\u source.props.base\u query\u model而不是self.shell.props.selected\u source.props.base\u query\u model,即使我可能在左侧窗格中将视图更改为例如Last.FM或Radio,我仍将获得输出

然而,我仍然必须遍历所有歌曲以找到所有的艺术家。但主要问题已经过去了