Debian Codegraph @

xl/metadata/ogg.py

raw
1 # Copyright (C) 2008-2010 Adam Olsen
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2, or (at your option)
6 # any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 #
18 # The developers of the Exaile media player hereby grant permission
19 # for non-GPL compatible GStreamer and Exaile plugins to be used and
20 # distributed together with GStreamer and Exaile. This permission is
21 # above and beyond the permissions granted by the GPL license by which
22 # Exaile is covered. If you modify this code, you may extend this
23 # exception to your version of the code, but you are not obligated to
24 # do so. If you do not wish to do so, delete this exception statement
25 # from your version.
26
27 import xl.unicode
28 from xl.metadata._base import CaseInsensitiveBaseFormat, CoverImage
29 from xl import settings
30 from mutagen import oggvorbis, oggopus
31 from mutagen.flac import Picture
32 import base64
33
34
35 class OggFormat(CaseInsensitiveBaseFormat):
36 MutagenType = oggvorbis.OggVorbis
37 tag_mapping = {
38 'cover': 'metadata_block_picture',
39 '__rating': 'rating',
40 }
41 writable = True
42
43 def _get_tag(self, raw, tag):
44 value = CaseInsensitiveBaseFormat._get_tag(self, raw, tag)
45 if value and tag == 'metadata_block_picture':
46 new_value = []
47 for v in value:
48 picture = Picture(base64.b64decode(v))
49 new_value.append(
50 CoverImage(
51 type=picture.type,
52 desc=picture.desc,
53 mime=picture.mime,
54 data=picture.data,
55 )
56 )
57 value = new_value
58 if value and tag == 'rating':
59 value = [str(self._rating_to_stars(int(value[0])))]
60
61 elif tag == 'bpm':
62 if (
63 settings.get_option('collection/use_legacy_metadata_mapping', False)
64 and 'tempo' in raw
65 ):
66 value = CaseInsensitiveBaseFormat._get_tag(self, raw, 'tempo')
67
68 elif tag == 'comment':
69 if (
70 settings.get_option('collection/use_legacy_metadata_mapping', False)
71 and 'description' in raw
72 ):
73 value = CaseInsensitiveBaseFormat._get_tag(self, raw, 'description')
74
75 return value
76
77 def _set_tag(self, raw, tag, value):
78 if tag == 'metadata_block_picture':
79 new_value = []
80 for v in value:
81 picture = Picture()
82 picture.type = v.type
83 picture.desc = v.desc
84 picture.mime = v.mime
85 picture.data = v.data
86 tmp = base64.b64encode(picture.write())
87 tmp = tmp.decode('ascii') # needs to be a str
88 new_value.append(tmp)
89 value = new_value
90 elif tag == 'rating':
91 rating = self._stars_to_rating(value[0])
92 value = [str(rating)]
93 elif tag == 'bpm':
94 if settings.get_option('collection/use_legacy_metadata_mapping', False):
95 tag = 'tempo'
96 value = [xl.unicode.to_unicode(v) for v in value]
97 elif tag == 'comment':
98 if settings.get_option('collection/use_legacy_metadata_mapping', False):
99 tag = 'description'
100 value = [xl.unicode.to_unicode(v) for v in value]
101 else:
102 # vorbis has text based attributes, so convert everything to unicode
103 value = [xl.unicode.to_unicode(v) for v in value]
104 CaseInsensitiveBaseFormat._set_tag(self, raw, tag, value)
105
106
107 class OggOpusFormat(OggFormat):
108 MutagenType = oggopus.OggOpus