]> kaliko git repositories - mpd-sima.git/blob - sima/plugins/internal/crop.py
3f987ce7482190941e4c199a1fa9eb6f28e88a9a
[mpd-sima.git] / sima / plugins / internal / crop.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2013-2015, 2020 kaliko <kaliko@azylum.org>
3 #
4 #  This file is part of sima
5 #
6 #  sima 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 #  sima 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 sima.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 #
20 """Crops playlist
21 """
22
23 # standard library import
24 #from select import select
25
26 # third parties components
27
28 # local import
29 from ...lib.plugin import Plugin
30
31 class Crop(Plugin):
32     """
33     Crop playlist on next track
34     """
35     def __init__(self, daemon):
36         super().__init__(daemon)
37         self.daemon = daemon
38         self.target = None
39         if not self.plugin_conf:
40             return
41         target = self.plugin_conf.get('consume')
42         if not target:
43             return
44         try:
45             if int(target) < 0:
46                 self.log.info('Negative value for consume, not cropping')
47                 return
48         except ValueError:
49             self.log.warning('Bad value for consume, '
50                              'expecting an integer, not "%s"', target)
51         else:
52             self.target = int(target)
53             self.log.debug('Cropping at %s', self.target)
54
55     def callback_next_song(self):
56         if not self.target:
57             return
58         if not self.daemon.enabled:
59             self.log.debug('Queueing disabled, not cropping')
60             return False
61         player = self.daemon.player
62         if player.currentsong().pos > self.target:
63             self.log.debug('cropping playlist')
64         while player.currentsong().pos > self.target:
65             player.delete(0)
66
67
68 # VIM MODLINE
69 # vim: ai ts=4 sw=4 sts=4 expandtab