1from os.path import basename, splitext
2from fnmatch import fnmatch
3from ..id3.frames import ImageFrame
4
5
6FRONT_COVER = "FRONT_COVER"
7"""Album front cover."""
8BACK_COVER = "BACK_COVER"
9"""Album back cover."""
10MISC_COVER = "MISC_COVER"
11"""Other part of the album cover; liner notes, gate-fold, etc."""
12LOGO = "LOGO"
13"""Artist/band logo."""
14ARTIST = "ARTIST"
15"""Artist/band images."""
16LIVE = "LIVE"
17"""Artist/band images."""
18
19FILENAMES = {
20        FRONT_COVER: ["cover-front", "cover-alternate*", "cover",
21                      "folder", "front", "cover-front_*", "flier"],
22        BACK_COVER: ["cover-back", "back", "cover-back_*"],
23        MISC_COVER: ["cover-insert*", "cover-liner*", "cover-disc",
24                     "cover-media*"],
25        LOGO: ["logo*"],
26        ARTIST: ["artist*"],
27        LIVE: ["live*"],
28}
29"""A mapping of art types to lists of filename patterns (excluding file
30extension): type -> [file_pattern, ..]."""
31
32TO_ID3_ART_TYPES = {
33        FRONT_COVER: [ImageFrame.FRONT_COVER, ImageFrame.OTHER, ImageFrame.ICON,
34                      ImageFrame.LEAFLET],
35        BACK_COVER: [ImageFrame.BACK_COVER],
36        MISC_COVER: [ImageFrame.MEDIA],
37        LOGO: [ImageFrame.BAND_LOGO],
38        ARTIST: [ImageFrame.LEAD_ARTIST, ImageFrame.ARTIST, ImageFrame.BAND],
39        LIVE: [ImageFrame.DURING_PERFORMANCE, ImageFrame.DURING_RECORDING]
40}
41"""A mapping of art types to ID3 APIC (image) types: type -> [apic_type, ..]"""
42# ID3 image types not mapped above:
43#    OTHER_ICON          = 0x02
44#    CONDUCTOR           = 0x09
45#    COMPOSER            = 0x0B
46#    LYRICIST            = 0x0C
47#    RECORDING_LOCATION  = 0x0D
48#    VIDEO               = 0x10
49#    BRIGHT_COLORED_FISH = 0x11
50#    ILLUSTRATION        = 0x12
51#    PUBLISHER_LOGO      = 0x14
52
53FROM_ID3_ART_TYPES = {}
54"""A mapping of ID3 art types to eyeD3 art types; the opposite of
55TO_ID3_ART_TYPES."""
56for _type in TO_ID3_ART_TYPES:
57    for _id3_type in TO_ID3_ART_TYPES[_type]:
58        FROM_ID3_ART_TYPES[_id3_type] = _type
59
60
61def matchArtFile(filename):
62    """Compares ``filename`` (case insensitive) with lists of common art file
63    names and returns the type of art that was matched, or None if no types
64    were matched."""
65    base = splitext(basename(filename))[0]
66    for type_ in FILENAMES.keys():
67        if True in [fnmatch(base.lower(), fname) for fname in FILENAMES[type_]]:
68            return type_
69    return None
70
71
72def getArtFromTag(tag, type_=None):
73    """Returns a list of eyed3.id3.frames.ImageFrame objects matching ``type_``,
74    all if ``type_`` is None, or empty if tag does not contain art."""
75    art = []
76    for img in tag.images:
77        if not type_ or type_ == img.picture_type:
78            art.append(img)
79    return art
80