+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2009 Efrim <efrim@azylum.org> {{{
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# }}}
+
+"""
+DOC:
+"""
+
+import sys
+
+from os import (access, F_OK, W_OK)
+from os.path import (dirname, isfile, join, abspath)
+
+from lib.mpdutils import mconnect
+
+USAGE = """Usage:
+
+mtopls [<path-to-playlist>] [--help | -h | help]
+
+ If no playlist is specifed a new one is created in /var/lib/mpd/playlist/
+ named after genre of current playlist.
+
+"""
+
+class MtoPls(object):
+ """
+ """
+
+ def __init__(self):
+ """"""
+ self.pls_path = None
+ self.cli = mconnect()
+ self.current = self.cli.currentsong()
+ self.cli.disconnect()
+ print self.current
+ self._consume_sopt()
+ self._create_playlist()
+ self._controls_perm()
+ self._run()
+
+ def _consume_sopt(self):
+ """"""
+ if len(sys.argv) > 2 or \
+ len(sys.argv) == 2 and \
+ sys.argv[1] in ['-h', '--help', 'help']:
+ sys.stdout.write(USAGE)
+ sys.exit(1)
+ if len(sys.argv) == 2:
+ self.pls_path = sys.argv[1]
+ if not dirname(self.pls_path):
+ self.pls_path = abspath(sys.argv[1])
+ sys.stdout.write('Playlist set to "%s"\n' % self.pls_path)
+ return
+ if len(sys.argv) == 1:
+ self._set_playlist()
+ return
+ sys.stdout.write(USAGE)
+ sys.exit(1)
+
+ def _set_playlist(self):
+ """Set playlist is none is given"""
+
+ mpd_playlists = '/var/lib/mpd/playlists/'
+ if not access(mpd_playlists, F_OK):
+ sys.stderr.write('Error: No access to "%s"' % self.pls_path)
+ sys.exit(1)
+ genre = self.current.get('genre', None)
+ if not genre:
+ sys.stderr.write('Error: No genre set in %s' %
+ self.current.get('file'))
+ sys.stdout.write('Please provide a playlist.')
+ sys.exit(1)
+ genre += '.m3u'
+ self.pls_path = join('/var/lib/mpd/playlists/', genre)
+
+ def _create_playlist(self):
+ if not isfile(self.pls_path):
+ sys.stdout.write('Create new playlist: %s\n' % self.pls_path)
+ open(self.pls_path, 'a').close()
+
+ def _controls_perm(self):
+ if not access(dirname(self.pls_path), F_OK):
+ sys.stderr.write('Error: Not existing path: "%s"' % self.pls_path)
+ sys.exit(1)
+ if not access(self.pls_path, W_OK):
+ sys.stderr.write('Error: No write access to path: "%s"' % self.pls_path)
+ sys.exit(1)
+ if not isfile(self.pls_path):
+ sys.stderr.write('Error: Not a regular file: "%s"' % self.pls_path)
+ sys.exit(1)
+ return True
+
+ def _run(self):
+ """"""
+ fd = open(self.pls_path, 'a')
+ fd.write(self.current.get('file'))
+ fd.close()
+
+# Script starts here
+if __name__ == '__main__':
+ MtoPls()
+
+# VIM MODLINE
+# vim: ai ts=4 sw=4 sts=4 expandtab