Debian Codegraph @

xl/migrations/database/covers_1to2.py

raw
1 # Copyright (C) 2009 Aren Olson (for the old_get_track_key function)
2 # Copyright (C) 2018 Johannes Sasongko <[email protected]>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
7 # any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 #
18 #
19 # The developers of the Exaile media player hereby grant permission
20 # for non-GPL compatible GStreamer and Exaile plugins to be used and
21 # distributed together with GStreamer and Exaile. This permission is
22 # above and beyond the permissions granted by the GPL license by which
23 # Exaile is covered. If you modify this code, you may extend this
24 # exception to your version of the code, but you are not obligated to
25 # do so. If you do not wish to do so, delete this exception statement
26 # from your version.
27
28 __all__ = ['migrate']
29
30 import logging
31 import os
32
33 import xl.collection
34 import xl.covers
35
36 logger = logging.getLogger(__name__)
37
38
39 def old_get_track_key(track):
40 """
41 Get the db mapping key for a track
42 """
43 album = track.get_tag_raw("album", join=True)
44 compilation = track.get_tag_raw("__compilation")
45
46 if compilation:
47 value = track.get_tag_raw('albumartist')
48 if value:
49 tag = 'albumartist'
50 else:
51 tag = 'compilation'
52 value = compilation
53 elif album:
54 tag = 'album'
55 value = album
56 else:
57 # no album info, can't store it
58 return None
59 return (tag, tuple(value))
60
61
62 def migrate():
63 """Migrate covers.db version 1 to 2 (Exaile 4.0)."""
64
65 man = xl.covers.MANAGER
66 if man.db.get('version', 1) != 1:
67 return
68 logger.info("Upgrading covers.db to version 2")
69
70 valid_cachefiles = set()
71
72 old_db = man.db
73 new_db = {'version': 2}
74 for coll in xl.collection.COLLECTIONS:
75 for tr in coll.tracks.values():
76 key = old_get_track_key(tr._track)
77 value = old_db.get(key)
78 if value:
79 new_key = man._get_track_key(tr)
80 new_db[new_key] = value
81 if value.startswith('cache:'):
82 valid_cachefiles.add(value[6:])
83 man.db = new_db
84 man.save()
85
86 cachedir = os.path.join(man.location, 'cache')
87 for cachefile in frozenset(os.listdir(cachedir)) - valid_cachefiles:
88 os.remove(os.path.join(cachedir, cachefile))