| 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 flac |
| 31 |
from mutagen.flac import Picture |
| 32 |
|
| 33 |
|
| 34 |
class FlacFormat(CaseInsensitiveBaseFormat): |
| 35 |
MutagenType = flac.FLAC |
| 36 |
tag_mapping = { |
| 37 |
'cover': '__cover', |
| 38 |
'language': "Language", |
| 39 |
'__rating': 'rating', |
| 40 |
} |
| 41 |
writable = True |
| 42 |
case_sensitive = False |
| 43 |
|
| 44 |
def get_bitrate(self): |
| 45 |
return -1 |
| 46 |
|
| 47 |
def get_keys_disk(self): |
| 48 |
keys = CaseInsensitiveBaseFormat.get_keys_disk(self) |
| 49 |
if self.mutagen.pictures: |
| 50 |
keys.append('cover') |
| 51 |
return keys |
| 52 |
|
| 53 |
def _get_tag(self, raw, tag): |
| 54 |
if tag == '__cover': |
| 55 |
return [ |
| 56 |
CoverImage(type=p.type, desc=p.desc, mime=p.mime, data=p.data) |
| 57 |
for p in raw.pictures |
| 58 |
] |
| 59 |
|
| 60 |
elif tag == 'rating': |
| 61 |
if 'rating' not in raw: |
| 62 |
return [] |
| 63 |
# Rating Stars |
| 64 |
data = int(raw['rating'][0]) |
| 65 |
return [str(self._rating_to_stars(data))] |
| 66 |
|
| 67 |
elif tag == 'bpm': |
| 68 |
if ( |
| 69 |
settings.get_option('collection/use_legacy_metadata_mapping', False) |
| 70 |
and 'tempo' in raw |
| 71 |
): |
| 72 |
tag = 'tempo' |
| 73 |
|
| 74 |
elif tag == 'comment': |
| 75 |
if ( |
| 76 |
settings.get_option('collection/use_legacy_metadata_mapping', False) |
| 77 |
and 'description' in raw |
| 78 |
): |
| 79 |
tag = 'description' |
| 80 |
|
| 81 |
return CaseInsensitiveBaseFormat._get_tag(self, raw, tag) |
| 82 |
|
| 83 |
def _set_tag(self, raw, tag, value): |
| 84 |
if tag == '__cover': |
| 85 |
raw.clear_pictures() |
| 86 |
for v in value: |
| 87 |
picture = Picture() |
| 88 |
picture.type = v.type |
| 89 |
picture.desc = v.desc |
| 90 |
picture.mime = v.mime |
| 91 |
picture.data = v.data |
| 92 |
raw.add_picture(picture) |
| 93 |
return |
| 94 |
|
| 95 |
elif tag == 'rating': |
| 96 |
# Rating Stars |
| 97 |
value = [str(self._stars_to_rating(int(value[0])))] |
| 98 |
|
| 99 |
elif tag == 'bpm': |
| 100 |
if settings.get_option('collection/use_legacy_metadata_mapping', False): |
| 101 |
tag = 'tempo' |
| 102 |
value = [xl.unicode.to_unicode(v) for v in value] |
| 103 |
|
| 104 |
elif tag == 'comment': |
| 105 |
if settings.get_option('collection/use_legacy_metadata_mapping', False): |
| 106 |
tag = 'description' |
| 107 |
value = [xl.unicode.to_unicode(v) for v in value] |
| 108 |
|
| 109 |
else: |
| 110 |
# flac has text based attributes, so convert everything to unicode |
| 111 |
value = [xl.unicode.to_unicode(v) for v in value] |
| 112 |
CaseInsensitiveBaseFormat._set_tag(self, raw, tag, value) |
| 113 |
|
| 114 |
def _del_tag(self, raw, tag): |
| 115 |
if tag == '__cover': |
| 116 |
raw.clear_pictures() |
| 117 |
elif tag in raw: |
| 118 |
del raw[tag] |
| 119 |
|
| 120 |
|
| 121 |
# vim: et sts=4 sw=4 |