-
Notifications
You must be signed in to change notification settings - Fork 19
/
coverart_utils.py
820 lines (620 loc) · 26 KB
/
coverart_utils.py
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
# -*- Mode: python; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; -*-
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
from bisect import bisect_left, bisect_right
import collections
import re
import logging
import sys
from collections import namedtuple
from gi.repository import GdkPixbuf
from gi.repository import Gdk
from gi.repository import Gtk
from gi.repository import GLib
from gi.repository import RB
from gi.repository import GObject
from gi.repository import Gio
import lxml.etree as ET
import rb
from coverart_browser_prefs import CoverLocale
from coverart_browser_prefs import GSetting
import coverart_rb3compat as rb3compat
from coverart_search_providers import lastfm_connected
from coverart_search_providers import get_search_providers
class FauxTb(object):
def __init__(self, tb_frame, tb_lineno, tb_next):
self.tb_frame = tb_frame
self.tb_lineno = tb_lineno
self.tb_next = tb_next
def current_stack(skip=0):
try:
1 / 0
except ZeroDivisionError:
f = sys.exc_info()[2].tb_frame
for i in range(skip + 2):
f = f.f_back
lst = []
while f is not None:
lst.append((f, f.f_lineno))
f = f.f_back
return lst
def extend_traceback(tb, stack):
"""Extend traceback with stack info."""
head = tb
for tb_frame, tb_lineno in stack:
head = FauxTb(tb_frame, tb_lineno, head)
return head
def full_exc_info():
"""Like sys.exc_info, but includes the full traceback."""
t, v, tb = sys.exc_info()
full_tb = extend_traceback(tb, current_stack(1))
return t, v, full_tb
def dumpstack(message):
''' dumps the current stack - useful of debugging
'''
logging.error(message, exc_info=full_exc_info())
def uniquify_and_sort(iterable):
''' Removes duplicates of an iterables and returns a list of unique
elements.
'''
uniques = []
for element in iterable:
if element not in uniques:
uniques.append(element)
return sorted(uniques)
GenreType = namedtuple("GenreType", ["name", "genre_type"])
class NaturalString(str):
'''
this class implements an object that can naturally compare
strings
i.e. "15 album" < "100 album"
'''
def __init__(self, string):
super(NaturalString, self).__init__()
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)',
key)]
self._string_elements = alphanum_key(string)
def __lt__(self, other):
if type(other) is str:
return super(NaturalString, self).__lt__(other)
else:
return self._string_elements < other._string_elements
def __le__(self, other):
if type(other) is str:
return super(NaturalString, self).__le__(other)
else:
return self._string_elements <= other._string_elements
def __gt__(self, other):
if type(other) is str:
return super(NaturalString, self).__gt__(other)
else:
return self._string_elements > other._string_elements
def __ge__(self, other):
if type(other) is str:
return super(NaturalString, self).__ge__(other)
else:
return self._string_elements >= other._string_elements
class SortedCollection(object):
'''Sequence sorted by a key function.
SortedCollection() is much easier to work with than using bisect() directly.
It supports key functions like those use in sorted(), min(), and max().
The result of the key function call is saved so that keys can be searched
efficiently.
Instead of returning an insertion-point which can be hard to interpret, the
five find-methods return a specific item in the sequence. They can scan for
exact matches, the last item less-than-or-equal to a key, or the first item
greater-than-or-equal to a key.
Once found, an item's ordinal position can be located with the index() method.
New items can be added with the insert() and insert_right() methods.
Old items can be deleted with the remove() method.
The usual sequence methods are provided to support indexing, slicing,
length lookup, clearing, copying, forward and reverse iteration, contains
checking, item counts, item removal, and a nice looking repr.
Finding and indexing are O(log n) operations while iteration and insertion
are O(n). The initial sort is O(n log n).
The key function is stored in the 'key' attibute for easy introspection or
so that you can assign a new key function (triggering an automatic re-sort).
In short, the class was designed to handle all of the common use cases for
bisect but with a simpler API and support for key functions.
>>> from pprint import pprint
>>> from operator import itemgetter
>>> s = SortedCollection(key=itemgetter(2))
>>> for record in [
... ('roger', 'young', 30),
... ('angela', 'jones', 28),
... ('bill', 'smith', 22),
... ('david', 'thomas', 32)]:
... s.insert(record)
>>> pprint(list(s)) # show records sorted by age
[('bill', 'smith', 22),
('angela', 'jones', 28),
('roger', 'young', 30),
('david', 'thomas', 32)]
>>> s.find_le(29) # find oldest person aged 29 or younger
('angela', 'jones', 28)
>>> s.find_lt(28) # find oldest person under 28
('bill', 'smith', 22)
>>> s.find_gt(28) # find youngest person over 28
('roger', 'young', 30)
>>> r = s.find_ge(32) # find youngest person aged 32 or older
>>> s.index(r) # get the index of their record
3
>>> s[3] # fetch the record at that index
('david', 'thomas', 32)
>>> s.key = itemgetter(0) # now sort by first name
>>> pprint(list(s))
[('angela', 'jones', 28),
('bill', 'smith', 22),
('david', 'thomas', 32),
('roger', 'young', 30)]
'''
def __init__(self, iterable=(), key=None):
self._given_key = key
key = (lambda x: x) if key is None else key
decorated = sorted((key(item), item) for item in iterable)
self._keys = [k for k, item in decorated]
self._items = [item for k, item in decorated]
self._key = key
def _getkey(self):
return self._key
def _setkey(self, key):
if key is not self._key:
self.__init__(self._items, key=key)
def _delkey(self):
self._setkey(None)
key = property(_getkey, _setkey, _delkey, 'key function')
def clear(self):
self.__init__([], self._key)
def copy(self):
return self.__class__(self, self._key)
def __len__(self):
return len(self._items)
def __getitem__(self, i):
return self._items[i]
def __iter__(self):
return iter(self._items)
def __reversed__(self):
return ReversedSortedCollection(self)
def __repr__(self):
return '%s(%r, key=%s)' % (
self.__class__.__name__,
self._items,
getattr(self._given_key, '__name__', repr(self._given_key))
)
def __reduce__(self):
return self.__class__, (self._items, self._given_key)
def __contains__(self, item):
k = self._key(item)
i = bisect_left(self._keys, k)
j = bisect_right(self._keys, k)
return item in self._items[i:j]
def index(self, item):
'Find the position of an item. Raise ValueError if not found.'
k = self._key(item)
i = bisect_left(self._keys, k)
j = bisect_right(self._keys, k)
return self._items[i:j].index(item) + i
def count(self, item):
'Return number of occurrences of item'
k = self._key(item)
i = bisect_left(self._keys, k)
j = bisect_right(self._keys, k)
return self._items[i:j].count(item)
def insert(self, item):
'Insert a new item. If equal keys are found, add to the left'
k = self._key(item)
i = bisect_left(self._keys, k)
self._keys.insert(i, k)
self._items.insert(i, item)
return i
def reorder(self, item):
'''Reorder an item. If its key changed, then the item is
repositioned, otherwise the item stays untouched'''
index = self._items.index(item)
new_index = -1
if self._keys[index] != self._key(item):
del self._keys[index]
del self._items[index]
new_index = self.insert(item)
return new_index
def insert_all(self, items):
for item in items:
self.insert(item)
def remove(self, item):
'Remove first occurence of item. Raise ValueError if not found'
i = self.index(item)
del self._keys[i]
del self._items[i]
class ReversedSortedCollection(object):
def __init__(self, sorted_collection):
self._sorted_collection = sorted_collection
def __getattr__(self, name):
return getattr(self._sorted_collection, name)
def copy(self):
return self.__class__(self._sorted_collection)
def _getkey(self):
return self._key
def _setkey(self, key):
if key is not self._key:
self.__init__(SortedCollection(self._items, key=key))
def _delkey(self):
self._setkey(None)
key = property(_getkey, _setkey, _delkey, 'key function')
def __len__(self):
return len(self._sorted_collection)
def __getitem__(self, i):
return self._items[len(self) - i - 1]
def __iter__(self):
return iter(reversed(self._items))
def __reversed__(self):
return self._sorted_collection
def __repr__(self):
return '%s(%r, key=%s)' % (
self.__class__.__name__,
reversed(self._items),
getattr(self._given_key, '__name__', repr(self._given_key))
)
def __reduce__(self):
return self.__class__, (reversed(self._items), self._given_key)
def insert(self, item):
'Insert a new item. If equal keys are found, add to the left'
i = self._sorted_collection.insert(item)
return len(self) - i - 1
def index(self, item):
'Find the position of an item. Raise ValueError if not found.'
return len(self) - self._sorted_collection.index(item) - 1
class IdleCallIterator(object):
def __init__(self, chunk, process, after=None, error=None, finish=None):
default = lambda *_: None
self._chunk = chunk
self._process = process
self._after = after if after else default
self._error = error if error else default
self._finish = finish if finish else default
self._stop = False
def __call__(self, iterator, **data):
self._iter = iterator
Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, self._idle_call, data)
def _idle_call(self, data):
if self._stop:
return False
for i in range(self._chunk):
try:
next_elem = next(self._iter)
self._process(next_elem, data)
except StopIteration:
self._finish(data)
return False
except Exception as e:
self._error(e)
self._after(data)
return True
def stop(self):
self._stop = True
def idle_iterator(func):
def iter_function(obj, iterator, **data):
idle_call = IdleCallIterator(*func(obj))
idle_call(iterator, **data)
return idle_call
return iter_function
class Theme:
'''
This class manages the theme details
'''
# storage for the instance reference
__instance = None
class _impl(GObject.Object):
""" Implementation of the singleton interface """
# properties
theme = GObject.property(type=str, default="standard")
# signals
'''
changed = signal emitted when a theme has changed
'''
__gsignals__ = {
'theme_changed': (GObject.SIGNAL_RUN_LAST, None, ())
}
# below public variables and methods that can be called for Theme
def __init__(self, plugin):
'''
Initializes the singleton interface, assigning all the constants
used to access the plugin's settings.
'''
super(Theme._impl, self).__init__()
self.plugin = plugin
popups = rb.find_plugin_file(plugin, 'img/popups.xml')
root = ET.parse(open(popups)).getroot()
base = 'theme/theme'
self.themes = []
for elem in root.xpath(base):
self.themes.append(elem.attrib['folder_name'])
self.gs = GSetting()
self.setting = self.gs.get_setting(self.gs.Path.PLUGIN)
# connect properties and signals
self._connect_properties()
self._connect_signals()
@property
def current(self):
return self.setting[self.gs.PluginKey.THEME]
def _connect_properties(self):
self.setting.bind(self.gs.PluginKey.THEME, self,
'theme', Gio.SettingsBindFlags.GET)
def _connect_signals(self):
self.connect('notify::theme', self._on_theme_changed,
None)
def _on_theme_changed(self, *args):
self.emit('theme_changed')
def __init__(self, plugin):
""" Create singleton instance """
# Check whether we already have an instance
if Theme.__instance is None:
# Create and remember instance
Theme.__instance = Theme._impl(plugin)
# Store instance reference as the only member in the handle
self.__dict__['_Theme__instance'] = Theme.__instance
def __getattr__(self, attr):
""" Delegate access to implementation """
return getattr(self.__instance, attr)
def __setattr__(self, attr, value):
""" Delegate access to implementation """
return setattr(self.__instance, attr, value)
class SpriteSheet(object):
def __init__(self, image, icon_width, icon_height, x_spacing, y_spacing,
x_start, y_start, across_dimension, down_dimension,
alpha_color=None, size=None):
# load the image
base_image = GdkPixbuf.Pixbuf.new_from_file(image)
if alpha_color:
base_image = base_image.add_alpha(True, *alpha_color)
delta_y = icon_height + y_spacing
delta_x = icon_width + x_spacing
self._sprites = []
for y in range(0, down_dimension):
for x in range(0, across_dimension):
sprite = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB, True,
8, icon_width, icon_height)
base_image.copy_area(x_start + (x * delta_x),
y_start + (y * delta_y), icon_width, icon_height,
sprite, 0, 0)
if size:
sprite = sprite.scale_simple(size[0], size[1],
GdkPixbuf.InterpType.BILINEAR)
self._sprites.append(sprite)
def __len__(self):
return len(self._sprites)
def __getitem__(self, index):
return self._sprites[index]
class ConfiguredSpriteSheet(object):
def __init__(self, plugin, sprite_name, size=None):
popups = rb.find_plugin_file(plugin, 'img/popups.xml')
root = ET.parse(open(popups)).getroot()
base = 'theme/theme[@folder_name="' + Theme(plugin).current \
+ '"]/spritesheet[@name="' + sprite_name + '"]/'
image = rb.find_plugin_file(plugin, 'img/' + Theme(plugin).current \
+ '/' + root.xpath(base + 'image')[0].text)
icon_width = int(root.xpath(base + 'icon')[0].attrib['width'])
icon_height = int(root.xpath(base + 'icon')[0].attrib['height'])
x_spacing = int(root.xpath(base + 'spacing')[0].attrib['x'])
y_spacing = int(root.xpath(base + 'spacing')[0].attrib['y'])
x_start = int(root.xpath(base + 'start-position')[0].attrib['x'])
y_start = int(root.xpath(base + 'start-position')[0].attrib['y'])
across_dimension = int(root.xpath(base + 'dimension')[0].attrib['across'])
down_dimension = int(root.xpath(base + 'dimension')[0].attrib['down'])
try:
alpha_color = list(map(int,
root.xpath(base + 'alpha')[0].text.split(' ')))
except:
alpha_color = None
self.names = []
self.locale_names = {}
cl = CoverLocale()
lang = cl.get_locale()
base = sprite_name + '/' + sprite_name + \
'[@spritesheet="' + sprite_name + '"]'
for elem in root.xpath(base + '[not(@xml:lang)]'):
self.names.append(elem.text)
for elem in root.xpath(base + '[@xml:lang="' + lang + '"]'):
self.locale_names[elem.text] = elem.attrib['name']
if (not self.locale_names) and len(lang) > 2:
for elem in root.xpath(base + '[@xml:lang="' + \
lang[0:2] + '"]'):
self.locale_names[elem.text] = elem.attrib['name']
self._sheet = SpriteSheet(image, icon_width, icon_height, x_spacing,
y_spacing, x_start, y_start, across_dimension, down_dimension,
alpha_color, size)
self._genre_db = RB.ExtDB(name='cb_genre')
def __len__(self):
return len(self._sheet)
def __getitem__(self, name):
try:
return self._sheet[self.names.index(name)]
except:
return None
def __contains__(self, name):
return name in self.names
def keys(self):
return self.names
class GenreConfiguredSpriteSheet(ConfiguredSpriteSheet):
'''
A sprite-sheet of genres. Creates a pixbuf representation of a picture
that has several icons in a regular pattern. This uses the file
'popups.xml' for its definition
:plugin: rhythmbox plugin
:sprite_name: `str` containing name of the spritesheet pattern in
popups.xml
:size: `int` array dimension of the final sprite which is to be used.
output:
:names: `str` array of sprite names
'''
# types of genre
GENRE_USER = 1
GENRE_SYSTEM = 2
GENRE_LOCALE = 3
def __init__(self, plugin, sprite_name, size=None):
super(GenreConfiguredSpriteSheet, self).__init__(plugin, sprite_name,
size)
self.genre_alternate = {} # contains GenreType tuples
self._alt_icons = {}
self._sprite_name = sprite_name
self._size = size
popups = rb.find_plugin_file(plugin, 'img/popups.xml')
root = ET.parse(open(popups)).getroot()
self._parse_popups(plugin, root, self.GENRE_SYSTEM)
try:
# self._user_popups = RB.find_user_data_file('plugins/coverart_browser/img/usericons/popups.xml')
self._user_popups = RB.user_cache_dir() + "/coverart_browser/usericons/popups.xml"
root = ET.parse(open(self._user_popups)).getroot()
self._parse_popups(plugin, root, self.GENRE_USER)
elem = root.xpath(self._sprite_name + '/index')
curr_index = int(elem[0].text)
for index in range(0, curr_index + 1):
key = RB.ExtDBKey.create_lookup('icon', str(index))
icon_location = self._genre_db.lookup(key)
sprite = GdkPixbuf.Pixbuf.new_from_file(icon_location)
if self._size:
sprite = sprite.scale_simple(self._size[0], self._size[1],
GdkPixbuf.InterpType.BILINEAR)
self._alt_icons[str(index)] = sprite
self.names.append(str(index))
except:
pass
def __getitem__(self, name):
try:
return self._alt_icons[name]
except:
return self._sheet[self.names.index(name)]
def _parse_popups(self, plugin, root, genre_type):
icon_names = {}
cl = CoverLocale()
lang = cl.get_locale()
base = self._sprite_name + '/alt'
for elem in root.xpath(base + '[not(@xml:lang)]/alt'):
self.genre_alternate[GenreType(name=elem.text, genre_type=genre_type)] = elem.attrib['genre']
for elem in root.xpath(base + '[@xml:lang="' + lang + '"]/alt'):
self.genre_alternate[GenreType(name=elem.text, genre_type=self.GENRE_LOCALE)] = elem.attrib['genre']
# if (not self.locale_alternate) and len(lang) > 2:
if len(lang) > 2:
for elem in root.xpath(base + '[@xml:lang="' + \
lang[0:2] + '"]/alt'):
self.genre_alternate[GenreType(name=elem.text, genre_type=self.GENRE_LOCALE)] = elem.attrib['genre']
def add_genre_icon(self, filename):
root = ET.parse(open(self._user_popups)).getroot()
elem = root.xpath(self._sprite_name + '/index')
next_index = int(elem[0].text)
elem[0].text = str(next_index + 1)
tree = ET.ElementTree(root)
tree.write(self._user_popups, pretty_print=True, xml_declaration=True)
key = RB.ExtDBKey.create_storage('icon', str(next_index))
uri = "file://" + rb3compat.pathname2url(filename)
self._genre_db.store_uri(key, RB.ExtDBSourceType.USER_EXPLICIT, uri)
pixbuf = GdkPixbuf.Pixbuf.new_from_file(filename)
new_genre = GenreType(name=str(next_index), genre_type=self.GENRE_USER)
if self._size:
pixbuf = pixbuf.scale_simple(self._size[0], self._size[1],
GdkPixbuf.InterpType.BILINEAR)
self._alt_icons[new_genre.name] = pixbuf
self.names.append(new_genre.name)
return new_genre
def delete_genre(self, current_genre):
root = ET.parse(open(self._user_popups)).getroot()
base = self._sprite_name + '/alt/alt'
found = False
for elem in root.xpath(base):
if RB.search_fold(elem.text) == RB.search_fold(current_genre):
found = True
break
if found:
elem.getparent().remove(elem)
tree = ET.ElementTree(root)
tree.write(self._user_popups, pretty_print=True, xml_declaration=True)
else:
print("not found to delete")
def amend_genre_info(self, current_genre, new_genre, icon_name):
root = ET.parse(open(self._user_popups)).getroot()
base = self._sprite_name + '/alt/alt'
found = False
if current_genre != "":
for elem in root.xpath(base):
if RB.search_fold(elem.text) == RB.search_fold(current_genre):
found = True
del self.genre_alternate[GenreType(name=elem.text, genre_type=self.GENRE_USER)]
break
else:
elem = ET.SubElement(root.xpath(self._sprite_name + '/alt')[0], "alt")
if elem != None:
found = True
if found:
elem.text = rb3compat.unicodestr(new_genre, 'utf-8')
elem.attrib['genre'] = icon_name
tree = ET.ElementTree(root)
tree.write(self._user_popups, pretty_print=True, xml_declaration=True)
self.genre_alternate[GenreType(name=elem.text, genre_type=self.GENRE_USER)] = icon_name
return GenreType(name=elem.text, genre_type=self.GENRE_USER)
else:
print("nothing found to amend")
return None
def get_stock_size():
what, width, height = Gtk.icon_size_lookup(Gtk.IconSize.BUTTON)
return width, height
def create_pixbuf_from_file_at_size(filename, width, height):
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(filename, width, height)
if pixbuf.get_width() != width or pixbuf.get_height() != height:
pixbuf = pixbuf.scale_simple(width, height,
GdkPixbuf.InterpType.BILINEAR)
return pixbuf
'''
class to search through a dict without case-sensitivity nor
unicode vs string issues
'''
class CaseInsensitiveDict(collections.abc.Mapping):
def __init__(self, d):
self._d = d
self._s = dict((RB.search_fold(k), k) for k in d)
def __contains__(self, k):
return RB.search_fold(k) in self._s
def __len__(self):
return len(self._s)
def __iter__(self):
return iter(self._s)
def __getitem__(self, k):
return self._d[self._s[RB.search_fold(k)]]
def actual_key_case(self, k):
return self._s.get(RB.search_fold(k))
def check_lastfm(force_check=False):
'''
check validity of lastfm connection
returns True if connected with an account
Also returns True if lastFM is not in the list of search providers
'''
providers = get_search_providers()
print(providers)
print(force_check)
if force_check or 'lastfm-search' in providers:
connected = lastfm_connected()
print(connected)
return connected
elif not 'lastfm-search' in providers:
print("not lastm-search")
return True
else:
print("returning default")
return False
def create_button_image(plugin, icon_name):
'create a pixbuf for the given icon_name sized according to the stock icon size'
path = 'img/'
return create_pixbuf_from_file_at_size(
rb.find_plugin_file(plugin, path + icon_name),
*get_stock_size())