]> kaliko git repositories - mpd-goodies.git/blob - src/crop
* add Goodie class (wraps mpdclass and startop together)
[mpd-goodies.git] / src / crop
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2009, 2010, 2012 Kaliko Jack <efrim@azylum.org>
5 #
6 #   This program 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 #   This program 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 sys
21
22 from lib.goodies import Goodie
23
24 NAME = 'crop'
25 VERSION = '0.1'
26 USAGE = 'USAGE:  %prog [--help] | [ <n> ]'
27
28
29 class Crop(Goodie):
30     """
31     """
32     script_info = dict({
33         'version': VERSION,
34         'prog_name': NAME,
35         'description': 'Keep <n> tracks before currently played, removed others. Default is to keep 6 tracks.',
36         'usage': USAGE,
37         })
38
39     def __init__(self):
40         """"""
41         Goodie.__init__(self, self.__class__.script_info)
42         self.nb_tracks = 6
43         self._get_arg()
44         self._run()
45
46     def _get_arg(self):
47         """"""
48         if not self.cli_args:
49             return True
50         try:
51             self.nb_tracks = int(self.cli_args[0])
52         except ValueError, err:
53             self.parser.error('invalid argument, not a natural number? (%s)' % err)
54
55     def _run(self):
56         """"""
57         print 'Connecting %s:%i' % (self.cli_options.host, self.cli_options.port)
58         print 'Keeping %i tracks' % self.nb_tracks
59         self.mpdConnect()
60         current_pos = int(self.client.currentsong().get('pos', 0))
61         if current_pos <= self.nb_tracks:
62             self.client.disconnect()
63             sys.exit(0)
64         while current_pos > self.nb_tracks:
65             self.client.delete(0)
66             current_pos = int(self.client.currentsong().get('pos', 0))
67         self.client.disconnect()
68         sys.exit(0)
69
70
71 # Script starts here
72 if __name__ == '__main__':
73     try:
74         Crop()
75     except KeyboardInterrupt:
76         sys.stdout.write('exit')
77
78 # VIM MODLINE
79 # vim: ai ts=4 sw=4 sts=4 expandtab