Cleaned up hooke.playlist.load() and its use in `load playlist`.
authorW. Trevor King <wking@drexel.edu>
Sun, 22 Aug 2010 05:03:28 +0000 (01:03 -0400)
committerW. Trevor King <wking@drexel.edu>
Sun, 22 Aug 2010 05:03:28 +0000 (01:03 -0400)
hooke/playlist.py
hooke/plugin/playlist.py

index fd7ac4bf81107795bb3811f2ee36877b6b7b17c4..a4dd0abcf525858587c8e6bb24d4020fb0c20554 100644 (file)
@@ -201,6 +201,24 @@ class Playlist (NoteIndexList):
                 oldest = self._loaded.pop(0)
                 oldest.unload()
 
+def playlist_path(path):
+    """Normalize playlist path extensions.
+
+    Examples
+    --------
+    >>> print playlist_path('playlist')
+    playlist.hkp
+    >>> print playlist_path('playlist.hkp')
+    playlist.hkp
+    >>> print playlist_path(None)
+    None
+    """
+    if path == None:
+        return None
+    if not path.endswith('.hkp'):
+        path += '.hkp'
+    return path
+
 
 class FilePlaylist (Playlist):
     """A file-backed :class:`Playlist`.
@@ -272,8 +290,7 @@ class FilePlaylist (Playlist):
             if self._base_path == None:
                 self._base_path = os.getcwd()
         else:
-            if not path.endswith('.hkp'):
-                path += '.hkp'
+            path = playlist_path(path)
             self.path = path
             self._base_path = os.path.dirname(os.path.abspath(
                 os.path.expanduser(self.path)))
@@ -537,13 +554,19 @@ def from_string(string):
     """
     return yaml.load(string)
 
-def load(path=None, identify=True, hooke=None):
+def load(path=None, drivers=None, identify=True, hooke=None):
     """Load a playlist from a file.
     """
-    with open(self.path, 'r') as f:
+    path = playlist_path(path)
+    with open(path, 'r') as f:
         text = f.read()
     playlist = from_string(text)
+    playlist.set_path(path)
+    print type(playlist)
     playlist._digest = playlist.digest()
+    if drivers != None:
+        playlist.drivers = drivers
+    playlist.set_path(path)
     for curve in playlist:
         curve.set_hooke(hooke)
         if identify == True:
index 3b1d83754b16d9dc5d050ea8f1c5662fc8129e15..36993017a4344113c9a61c6261c190eef021ffe7 100644 (file)
@@ -27,7 +27,7 @@ import os.path
 
 from ..command import Command, Argument, Failure
 from ..curve import NotRecognized
-from ..playlist import FilePlaylist
+from ..playlist import load
 from ..util.itertools import reverse_enumerate
 from . import Builtin
 
@@ -250,8 +250,7 @@ Drivers for loading curves.
             help=self.__doc__, plugin=plugin)
 
     def _run(self, hooke, inqueue, outqueue, params):
-        p = FilePlaylist(drivers=params['drivers'], path=params['input'])
-        p.load(hooke=hooke)
+        p = load(path=params['input'], drivers=params['drivers'])
         self._set_playlist(hooke, params, p)
        outqueue.put(p)