1# -*- coding: utf-8 -*-
2# This file is part of beets.
3#
4# Permission is hereby granted, free of charge, to any person obtaining
5# a copy of this software and associated documentation files (the
6# "Software"), to deal in the Software without restriction, including
7# without limitation the rights to use, copy, modify, merge, publish,
8# distribute, sublicense, and/or sell copies of the Software, and to
9# permit persons to whom the Software is furnished to do so, subject to
10# the following conditions:
11#
12# The above copyright notice and this permission notice shall be
13# included in all copies or substantial portions of the Software.
14
15from __future__ import division, absolute_import, print_function
16
17import os
18import fnmatch
19import tempfile
20import beets
21
22
23class PlaylistQuery(beets.dbcore.Query):
24    """Matches files listed by a playlist file.
25    """
26    def __init__(self, pattern):
27        self.pattern = pattern
28        config = beets.config['playlist']
29
30        # Get the full path to the playlist
31        playlist_paths = (
32            pattern,
33            os.path.abspath(os.path.join(
34                config['playlist_dir'].as_filename(),
35                '{0}.m3u'.format(pattern),
36            )),
37        )
38
39        self.paths = []
40        for playlist_path in playlist_paths:
41            if not fnmatch.fnmatch(playlist_path, '*.[mM]3[uU]'):
42                # This is not am M3U playlist, skip this candidate
43                continue
44
45            try:
46                f = open(beets.util.syspath(playlist_path), mode='rb')
47            except (OSError, IOError):
48                continue
49
50            if config['relative_to'].get() == 'library':
51                relative_to = beets.config['directory'].as_filename()
52            elif config['relative_to'].get() == 'playlist':
53                relative_to = os.path.dirname(playlist_path)
54            else:
55                relative_to = config['relative_to'].as_filename()
56            relative_to = beets.util.bytestring_path(relative_to)
57
58            for line in f:
59                if line[0] == '#':
60                    # ignore comments, and extm3u extension
61                    continue
62
63                self.paths.append(beets.util.normpath(
64                    os.path.join(relative_to, line.rstrip())
65                ))
66            f.close()
67            break
68
69    def col_clause(self):
70        if not self.paths:
71            # Playlist is empty
72            return '0', ()
73        clause  = 'path IN ({0})'.format(', '.join('?' for path in self.paths))
74        return clause, (beets.library.BLOB_TYPE(p) for p in self.paths)
75
76    def match(self, item):
77        return item.path in self.paths
78
79
80class PlaylistPlugin(beets.plugins.BeetsPlugin):
81    item_queries = {'playlist': PlaylistQuery}
82
83    def __init__(self):
84        super(PlaylistPlugin, self).__init__()
85        self.config.add({
86            'auto': False,
87            'playlist_dir': '.',
88            'relative_to': 'library',
89        })
90
91        self.playlist_dir = self.config['playlist_dir'].as_filename()
92        self.changes = {}
93
94        if self.config['relative_to'].get() == 'library':
95            self.relative_to = beets.util.bytestring_path(
96                beets.config['directory'].as_filename())
97        elif self.config['relative_to'].get() != 'playlist':
98            self.relative_to = beets.util.bytestring_path(
99                self.config['relative_to'].as_filename())
100        else:
101            self.relative_to = None
102
103        if self.config['auto']:
104            self.register_listener('item_moved', self.item_moved)
105            self.register_listener('item_removed', self.item_removed)
106            self.register_listener('cli_exit', self.cli_exit)
107
108    def item_moved(self, item, source, destination):
109        self.changes[source] = destination
110
111    def item_removed(self, item):
112        if not os.path.exists(beets.util.syspath(item.path)):
113            self.changes[item.path] = None
114
115    def cli_exit(self, lib):
116        for playlist in self.find_playlists():
117            self._log.info('Updating playlist: {0}'.format(playlist))
118            base_dir = beets.util.bytestring_path(
119                self.relative_to if self.relative_to
120                else os.path.dirname(playlist)
121            )
122
123            try:
124                self.update_playlist(playlist, base_dir)
125            except beets.util.FilesystemError:
126                self._log.error('Failed to update playlist: {0}'.format(
127                    beets.util.displayable_path(playlist)))
128
129    def find_playlists(self):
130        """Find M3U playlists in the playlist directory."""
131        try:
132            dir_contents = os.listdir(beets.util.syspath(self.playlist_dir))
133        except OSError:
134            self._log.warning('Unable to open playlist directory {0}'.format(
135                beets.util.displayable_path(self.playlist_dir)))
136            return
137
138        for filename in dir_contents:
139            if fnmatch.fnmatch(filename, '*.[mM]3[uU]'):
140                yield os.path.join(self.playlist_dir, filename)
141
142    def update_playlist(self, filename, base_dir):
143        """Find M3U playlists in the specified directory."""
144        changes = 0
145        deletions = 0
146
147        with tempfile.NamedTemporaryFile(mode='w+b', delete=False) as tempfp:
148            new_playlist = tempfp.name
149            with open(filename, mode='rb') as fp:
150                for line in fp:
151                    original_path = line.rstrip(b'\r\n')
152
153                    # Ensure that path from playlist is absolute
154                    is_relative = not os.path.isabs(line)
155                    if is_relative:
156                        lookup = os.path.join(base_dir, original_path)
157                    else:
158                        lookup = original_path
159
160                    try:
161                        new_path = self.changes[beets.util.normpath(lookup)]
162                    except KeyError:
163                        tempfp.write(line)
164                    else:
165                        if new_path is None:
166                            # Item has been deleted
167                            deletions += 1
168                            continue
169
170                        changes += 1
171                        if is_relative:
172                            new_path = os.path.relpath(new_path, base_dir)
173
174                        tempfp.write(line.replace(original_path, new_path))
175
176        if changes or deletions:
177            self._log.info(
178                'Updated playlist {0} ({1} changes, {2} deletions)'.format(
179                    filename, changes, deletions))
180            beets.util.copy(new_playlist, filename, replace=True)
181        beets.util.remove(new_playlist)
182