]> kaliko git repositories - mpd-goodies.git/blobdiff - mtopls
* fixes some bugs on mtopls
[mpd-goodies.git] / mtopls
diff --git a/mtopls b/mtopls
index d44788920dc61d33cb898d3057e143022a66d5ca..78476c60b72b0bc18da9a44d63612b3f97eb143a 100755 (executable)
--- a/mtopls
+++ b/mtopls
@@ -1,7 +1,7 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
-# Copyright (c) 2009 Efrim <efrim@azylum.org> {{{
+# Copyright (c) 2009, 2010 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
@@ -27,14 +27,21 @@ import sys
 from os import (access, F_OK, W_OK)
 from os.path import (dirname, isfile, join, abspath)
 
-from lib.mpdutils import mconnect
+from lib.mpdutils import (mconnect, collapse_tags)
 
 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.
+    Add the current track to playlist.
+
+    The default playlist, if none specified, is named after the current track's
+    genre within /var/lib/mpd/playlists/ (creating it if not existing).
+
+    Obviously the script is meant to be executed on hosts where
+    /var/lib/mpd/playlists/ makes sense or where file entries in the playlist
+    make sense for you (cf. "man 5 mpd.conf" especially
+    save_absolute_paths_in_playlists option).
 
 """
 
@@ -48,16 +55,14 @@ class MtoPls(object):
         self.cli = mconnect()
         self.current = self.cli.currentsong()
         self.cli.disconnect()
-        print self.current
         self._consume_sopt()
-        self._create_playlist()
         self._controls_perm()
+        self._create_playlist()
         self._run()
 
     def _consume_sopt(self):
         """"""
-        if len(sys.argv) > 2 or \
-                len(sys.argv) == 2 and \
+        if len(sys.argv) >= 2 and \
                 sys.argv[1] in ['-h', '--help', 'help']:
             sys.stdout.write(USAGE)
             sys.exit(1)
@@ -65,7 +70,7 @@ class MtoPls(object):
             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)
+            print >> sys.stdout, ('Playlist set to "%s"' % self.pls_path)
             return
         if len(sys.argv) == 1:
             self._set_playlist()
@@ -80,18 +85,19 @@ class MtoPls(object):
         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)
+        genre = collapse_tags(self.current.get('genre', None))
         if not genre:
-            sys.stderr.write('Error: No genre set in %s' %
+            sys.stderr.write('Error: No genre set in %s\n' %
                     self.current.get('file'))
-            sys.stdout.write('Please provide a playlist.')
+            print >> sys.stdout, ('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)
+            # TODO: add M3U header
+            print >> sys.stdout, ('Create new playlist: %s' % self.pls_path)
             open(self.pls_path, 'a').close()
 
     def _controls_perm(self):
@@ -108,9 +114,12 @@ class MtoPls(object):
 
     def _run(self):
         """"""
+        # TODO: controls either file is already in playlist or not
+        print >> sys.stdout, ('Writing to %s' % self.pls_path)
         fd = open(self.pls_path, 'a')
-        fd.write(self.current.get('file'))
+        fd.write(self.current.get('file') + '\n')
         fd.close()
+        pass
 
 # Script starts here
 if __name__ == '__main__':