| 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 |
|
| 28 |
import os, logging |
| 29 |
from typing import Optional |
| 30 |
import urllib.parse |
| 31 |
|
| 32 |
from gi.repository import Gio |
| 33 |
|
| 34 |
from xl.metadata._base import BaseFormat, CoverImage, NotWritable, NotReadable |
| 35 |
|
| 36 |
from xl.metadata import ( |
| 37 |
aac, |
| 38 |
aiff, |
| 39 |
ape, |
| 40 |
asf, |
| 41 |
au, |
| 42 |
flac, |
| 43 |
mka, |
| 44 |
mod, |
| 45 |
mp3, |
| 46 |
mp4, |
| 47 |
mpc, |
| 48 |
ogg, |
| 49 |
sid, |
| 50 |
speex, |
| 51 |
tta, |
| 52 |
wav, |
| 53 |
wv, |
| 54 |
) |
| 55 |
|
| 56 |
logger = logging.getLogger(__name__) |
| 57 |
|
| 58 |
#: dictionary mapping extensions to Format classes. |
| 59 |
formats = { |
| 60 |
# fmt: off |
| 61 |
'669' : mod.ModFormat, |
| 62 |
'aac' : [mp4.MP4Format, aac.AACFormat], |
| 63 |
'ac3' : None, |
| 64 |
'aif' : aiff.AIFFFormat, |
| 65 |
'aifc' : aiff.AIFFFormat, |
| 66 |
'aiff' : aiff.AIFFFormat, |
| 67 |
'ape' : ape.MonkeysFormat, |
| 68 |
'amf' : mod.ModFormat, |
| 69 |
'asf' : asf.AsfFormat, |
| 70 |
'au' : au.AuFormat, |
| 71 |
'dsm' : mod.ModFormat, |
| 72 |
'far' : mod.ModFormat, |
| 73 |
'flac' : flac.FlacFormat, |
| 74 |
'it' : mod.ModFormat, |
| 75 |
'm4a' : mp4.MP4Format, |
| 76 |
'med' : mod.ModFormat, |
| 77 |
'mka' : mka.MkaFormat, |
| 78 |
'mp2' : mp3.MP3Format, |
| 79 |
'mp3' : mp3.MP3Format, |
| 80 |
'mp4' : mp4.MP4Format, |
| 81 |
'mpc' : mpc.MpcFormat, |
| 82 |
'mid' : None, |
| 83 |
'midi' : None, |
| 84 |
'mod' : mod.ModFormat, |
| 85 |
'mtm' : mod.ModFormat, |
| 86 |
'oga' : ogg.OggFormat, |
| 87 |
'ogg' : ogg.OggFormat, |
| 88 |
'ogx' : ogg.OggFormat, |
| 89 |
'okt' : mod.ModFormat, |
| 90 |
'opus' : ogg.OggOpusFormat, |
| 91 |
'ra' : None, |
| 92 |
'ram' : None, |
| 93 |
's3m' : mod.ModFormat, |
| 94 |
'sid' : sid.SidFormat, |
| 95 |
'shn' : None, |
| 96 |
'snd' : wav.WavFormat, |
| 97 |
'spc' : None, |
| 98 |
'spx' : speex.SpeexFormat, |
| 99 |
'stm' : mod.ModFormat, |
| 100 |
'tta' : tta.TTAFormat, |
| 101 |
'ult' : mod.ModFormat, |
| 102 |
'wav' : wav.WavFormat, |
| 103 |
'webm' : None, |
| 104 |
'wma' : asf.AsfFormat, |
| 105 |
'wv' : wv.WavpackFormat, |
| 106 |
'xm' : mod.ModFormat, |
| 107 |
# fmt: on |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
# Dummy format class to use with unsupported format instead of directly |
| 112 |
# instantiating BaseFormat, which may cause issues with tag mapping |
| 113 |
# initialization in subclasses (see issue #539) |
| 114 |
class DummyFormat(BaseFormat): |
| 115 |
pass |
| 116 |
|
| 117 |
|
| 118 |
def get_format(loc: str) -> Optional[BaseFormat]: |
| 119 |
""" |
| 120 |
get a Format object appropriate for the file at loc. |
| 121 |
if no suitable object can be found, None is returned. |
| 122 |
|
| 123 |
:param loc: The location to read from as a Gio URI |
| 124 |
(from Track.get_loc_for_io()) |
| 125 |
""" |
| 126 |
loc = Gio.File.new_for_uri(loc).get_path() |
| 127 |
if not loc: |
| 128 |
return None |
| 129 |
|
| 130 |
ext = os.path.splitext(loc)[1] |
| 131 |
ext = ext[1:] # remove the pesky . |
| 132 |
ext = ext.lower() |
| 133 |
|
| 134 |
try: |
| 135 |
formatclass = formats[ext] |
| 136 |
except KeyError: |
| 137 |
return None # not supported |
| 138 |
|
| 139 |
if formatclass is None: |
| 140 |
formatclass = DummyFormat |
| 141 |
|
| 142 |
# Some file types can be more than one format |
| 143 |
# So check each possible format |
| 144 |
if type(formatclass) is list: |
| 145 |
for klass in formatclass: |
| 146 |
try: |
| 147 |
return klass(loc) |
| 148 |
except: |
| 149 |
continue |
| 150 |
return None |
| 151 |
|
| 152 |
try: |
| 153 |
return formatclass(loc) |
| 154 |
except NotReadable: |
| 155 |
logger.info("Unable to read file: %r", loc) |
| 156 |
return None |
| 157 |
|
| 158 |
|
| 159 |
# vim: et sts=4 sw=4 |