Don't load curves becore clearing their command stack.
[hooke.git] / hooke / playlist.py
index fd7ac4bf81107795bb3811f2ee36877b6b7b17c4..79f785c5305b35415af5651da7e4f2d25ae0b86c 100644 (file)
@@ -91,11 +91,12 @@ class NoteIndexList (list):
             return self._index
         return super(NoteIndexList, self).index(value, *args, **kwargs)
 
-    def current(self):
+    def current(self, load=True):
         if len(self) == 0:
             return None
         item = self[self._index]
-        self._setup_item(item)
+        if load == True:
+            self._setup_item(item)
         return item
 
     def jump(self, index):
@@ -160,14 +161,13 @@ class Playlist (NoteIndexList):
     def __init__(self, drivers, name=None):
         super(Playlist, self).__init__(name=name)
         self.drivers = drivers
-        self._max_loaded = 100 # curves to hold in memory simultaneously.
 
     def _set_default_attrs(self):
         super(Playlist, self)._set_default_attrs()
         self._default_attrs['drivers'] = []
         # List of loaded curves, see :meth:`._setup_item`.
         self._default_attrs['_loaded'] = []
-        self._default_attrs['_max_loaded'] = 100
+        self._default_attrs['_max_loaded'] = 100  # curves to hold in memory simultaneously.
 
     def __setstate__(self, state):
         super(Playlist, self).__setstate__(state)
@@ -201,6 +201,33 @@ class Playlist (NoteIndexList):
                 oldest = self._loaded.pop(0)
                 oldest.unload()
 
+    def unload(self, curve):
+        "Inverse of .`_setup_item`."
+        curve.unload()
+        try:
+            self._loaded.remove(curve)
+        except ValueError:
+            pass
+
+
+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 +299,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 +563,18 @@ 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)
     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: