/usr/share/pyshared/quodlibet/library/librarians.py is in exfalso 3.0.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | # Copyright 2006 Joe Wreschnig
# 2012 Nick Boultbee
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation
"""
Librarians for libraries.
"""
import itertools
from gi.repository import GObject
from quodlibet.util.dprint import print_d
class Librarian(GObject.GObject):
"""The librarian is a nice interface to all active libraries.
Librarians are a kind of meta-library. When any of their
registered libraries fire a signal, they fire the same
signal. Likewise, they provide various methods equivalent to the
ones found in libraries that group the results of the real
libraries.
Attributes:
libraries -- a dict mapping library names to libraries
"""
__gsignals__ = {
'changed': (GObject.SignalFlags.RUN_LAST, None, (object,)),
'removed': (GObject.SignalFlags.RUN_LAST, None, (object,)),
'added': (GObject.SignalFlags.RUN_LAST, None, (object,)),
}
def __init__(self):
super(Librarian, self).__init__()
self.libraries = {}
self.__signals = {}
def destroy(self):
pass
def register(self, library, name):
"""Register a library with this librarian."""
if name in self.libraries or name in self.__signals:
raise ValueError("library %r is already active" % name)
added_sig = library.connect('added', self.__added)
removed_sig = library.connect('removed', self.__removed)
changed_sig = library.connect('changed', self.__changed)
self.libraries[name] = library
self.__signals[library] = [added_sig, removed_sig, changed_sig]
def _unregister(self, library, name):
# This function, unlike register, should be private.
# Libraries get unregistered at the discretion of the
# librarian, not the libraries.
del(self.libraries[name])
map(library.disconnect, self.__signals[library])
del(self.__signals[library])
# FIXME: We can be smarter about this -- queue a list of items
# and fire the signal after a short wait, to take advantage of
# a case where many libraries fire a signal at the same time (or
# one fires a signal often).
def __changed(self, library, items):
self.emit('changed', items)
def __added(self, library, items):
self.emit('added', items)
def __removed(self, library, items):
self.emit('removed', items)
def changed(self, items):
"""Triage the items and inform their real libraries."""
for library in self.libraries.itervalues():
in_library = filter(library.__contains__, items)
if in_library:
library._changed(in_library)
def __getitem__(self, key):
"""Find a item given its key."""
for library in self.libraries.itervalues():
try:
return library[key]
except KeyError:
pass
else:
raise KeyError(key)
def get(self, key, default=None):
try:
return self[key]
except KeyError:
return default
def remove(self, items):
"""Remove items from all libraries."""
for library in self.libraries.itervalues():
library.remove(filter(library.__contains__, items))
def __contains__(self, item):
"""Check if a key or item is in the library."""
for library in self.libraries.itervalues():
if item in library:
return True
else:
return False
def __iter__(self):
"""Iterate over all items in all libraries."""
return itertools.chain(*self.libraries.itervalues())
def move(self, items, from_, to):
"""Move items from one library to another.
This causes 'removed' signals on the from library, and 'added'
signals on the 'to' library, but will not cause any signals
to be emitted via this librarian.
"""
try:
from_.handler_block(self.__signals[from_][1])
to.handler_block(self.__signals[to][0])
from_.remove(items)
to.add(items)
finally:
from_.handler_unblock(self.__signals[from_][1])
to.handler_unblock(self.__signals[to][0])
class SongLibrarian(Librarian):
"""A librarian for SongLibraries."""
def tag_values(self, tag):
"""Return a list of all values for the given tag."""
tags = set()
for library in self.libraries.itervalues():
tags.update(library.tag_values(tag))
return list(tags)
def rename(self, song, newname, changed=None):
"""Rename the song in all libraries it belongs to.
The 'changed' signal will fire for any library the song is in.
"""
# This needs to poke around inside the library directly. If
# it uses add/remove to handle the songs it fires incorrect
# signals. If it uses the library's rename method, it breaks
# the call for future libraries because the item's key has
# changed. So, it needs to reimplement the method.
re_add = []
print_d("Renaming %r to %r" % (song.key, newname), self)
for library in self.libraries.itervalues():
try:
del library._contents[song.key]
except KeyError:
pass
else:
re_add.append(library)
song.rename(newname)
for library in re_add:
library._contents[song.key] = song
if changed is None:
library._changed([song])
else:
print_d("Delaying changed signal for %r." % library, self)
changed.append(song)
def reload(self, item, changed=None, removed=None):
"""Reload a song."""
re_add = []
print_d("Reloading %r" % item.key, self)
for library in self.libraries.itervalues():
try:
del library._contents[item.key]
except KeyError:
pass
else:
re_add.append(library)
try:
library = re_add[0]
except IndexError:
return
# Rely on the first library in the list to do the actual
# load, then just inform the other libraries what happened.
was_changed, was_removed = library._load_item(item)
if was_removed:
for library in re_add:
library.emit('removed', [item])
elif was_changed:
for library in re_add:
library._contents[item.key] = item
library.emit('changed', [item])
|