~ubuntu-branches/ubuntu/vivid/gpodder/vivid

« back to all changes in this revision

Viewing changes to src/gpodder/gtkui/desktop/deviceplaylist.py

  • Committer: Package Import Robot
  • Author(s): tony mancill
  • Date: 2013-04-12 22:07:02 UTC
  • mfrom: (5.2.27 sid)
  • Revision ID: package-import@ubuntu.com-20130412220702-mux8v9r8zt2jin0x
Tags: 3.5.1-1
* New upstream release.
* d/control: declare versioned dependency on python-gtk2 (>= 2.16)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# gPodder - A media aggregator and podcast client
 
4
# Copyright (c) 2005-2011 Thomas Perl and the gPodder Team
 
5
#
 
6
# gPodder is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 3 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# gPodder is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
#
 
19
 
 
20
import os
 
21
import gpodder
 
22
 
 
23
_ = gpodder.gettext
 
24
 
 
25
from gpodder import util
 
26
 
 
27
import logging
 
28
logger = logging.getLogger(__name__)
 
29
 
 
30
class gPodderDevicePlaylist(object):
 
31
    def __init__(self, config, playlist_name):
 
32
        self._config=config
 
33
        self.linebreak = '\r\n'
 
34
        self.playlist_file=util.sanitize_filename(playlist_name + '.m3u')
 
35
        self.playlist_folder = os.path.join(self._config.device_sync.device_folder, self._config.device_sync.playlists.folder)
 
36
        self.mountpoint = util.find_mount_point(util.sanitize_encoding(self.playlist_folder))
 
37
        if self.mountpoint == '/':
 
38
            self.mountpoint = self.playlist_folder
 
39
            logger.warning('MP3 player resides on / - using %s as MP3 player root', self.mountpoint)
 
40
        self.playlist_absolute_filename=os.path.join(self.playlist_folder, self.playlist_file)
 
41
 
 
42
    def build_extinf(self, filename):
 
43
#TO DO: Windows playlists
 
44
#        if self._config.mp3_player_playlist_win_path:
 
45
#            filename = filename.replace('\\', os.sep)
 
46
 
 
47
#        # rebuild the whole filename including the mountpoint
 
48
#        if self._config.device_sync.playlist_absolute_path:
 
49
#            absfile = os.path.join(self.mountpoint,filename)
 
50
#        else: #TODO: Test rel filenames
 
51
#            absfile = util.rel2abs(filename, os.path.dirname(self.playlist_file))
 
52
 
 
53
        # fallback: use the basename of the file
 
54
        (title, extension) = os.path.splitext(os.path.basename(filename))
 
55
 
 
56
        return "#EXTINF:0,%s%s" % (title.strip(), self.linebreak)
 
57
 
 
58
    def read_m3u(self):
 
59
        """
 
60
        read all files from the existing playlist
 
61
        """
 
62
        tracks = []
 
63
        logger.info("Read data from the playlistfile %s" % self.playlist_absolute_filename)
 
64
        if os.path.exists(self.playlist_absolute_filename):
 
65
            for line in open(self.playlist_absolute_filename, 'r'):
 
66
                if not line.startswith('#EXT'):
 
67
                    tracks.append(line.rstrip('\r\n'))
 
68
        return tracks
 
69
 
 
70
    def get_filename_for_playlist(self, episode):
 
71
        """
 
72
        get the filename for the given episode for the playlist
 
73
        """
 
74
        filename_base = util.sanitize_filename(episode.sync_filename(
 
75
            self._config.device_sync.custom_sync_name_enabled,
 
76
            self._config.device_sync.custom_sync_name),
 
77
            self._config.device_sync.max_filename_length)
 
78
        filename = filename_base + os.path.splitext(episode.local_filename(create=False))[1].lower()
 
79
        return filename
 
80
 
 
81
    def get_absolute_filename_for_playlist(self, episode):
 
82
        """
 
83
        get the filename including full path for the given episode for the playlist
 
84
        """
 
85
        filename = self.get_filename_for_playlist(episode)
 
86
        if self._config.device_sync.one_folder_per_podcast:
 
87
            filename = os.path.join(util.sanitize_filename(episode.channel.title), filename)
 
88
        if self._config.device_sync.playlist.absolute_path:
 
89
            filename = os.path.join(util.relpath(self.mountpoint, self._config.device_sync.device_folder), filename)
 
90
        return filename
 
91
 
 
92
    def write_m3u(self, episodes):
 
93
        """
 
94
        write the list into the playlist on the device
 
95
        """
 
96
        logger.info('Writing playlist file: %s', self.playlist_file)
 
97
        if not util.make_directory(self.playlist_folder):
 
98
            raise IOError(_('Folder %s could not be created.') % self.playlist_folder, _('Error writing playlist'))
 
99
        else:
 
100
            fp = open(os.path.join(self.playlist_folder, self.playlist_file), 'w')
 
101
            fp.write('#EXTM3U%s' % self.linebreak)
 
102
            for current_episode in episodes:
 
103
                filename_base = util.sanitize_filename(current_episode.sync_filename(
 
104
                    self._config.device_sync.custom_sync_name_enabled,
 
105
                    self._config.device_sync.custom_sync_name),
 
106
                    self._config.device_sync.max_filename_length)
 
107
                filename = filename_base + os.path.splitext(current_episode.local_filename(create=False))[1].lower()
 
108
                filename = self.get_filename_for_playlist(current_episode)
 
109
                fp.write(self.build_extinf(filename))
 
110
                filename = self.get_absolute_filename_for_playlist(current_episode)
 
111
                fp.write(filename)
 
112
                fp.write(self.linebreak)
 
113
            fp.close()
 
114